Back to Repositories

Testing Bourbon Setting Retrieval Implementation in thoughtbot/bourbon

This test suite validates the fetch-bourbon-setting utility in the Bourbon Sass framework, focusing on configuration retrieval and font-face handling. The tests ensure proper default values, user overrides, and font file format processing.

Test Coverage Overview

The test suite provides comprehensive coverage of the fetch-bourbon-setting utility functionality:

  • Default value retrieval for modular-scale-base setting
  • User-overridden value handling for rails-asset-pipeline
  • Font-face mixin integration with custom font formats
  • Edge case validation for configuration inheritance

Implementation Analysis

The testing approach utilizes RSpec’s context-based structure to organize related test cases. The implementation leverages custom matchers (have_rule and have_ruleset) to verify CSS output patterns and formatting. Parser support ensures proper preprocessing of Sass utilities.

Technical Details

  • Testing Framework: RSpec
  • Custom Support: ParserSupport for file parsing
  • Matchers: Custom CSS rule validators
  • Setup: Before hooks for parser initialization
  • File Format: Ruby spec file

Best Practices Demonstrated

The test suite exemplifies several testing best practices including contextual organization, clear test descriptions, and isolated test cases. It demonstrates proper setup handling, custom matcher implementation, and comprehensive coverage of both default and override scenarios.

thoughtbot/bourbon

spec/bourbon/utilities/fetch_bourbon_setting_spec.rb

            
require "spec_helper"

describe "fetch-bourbon-setting" do
  before(:all) do
    ParserSupport.parse_file("utilities/fetch-bourbon-setting")
  end

  context "fetches the modular-scale-base setting" do
    it "and returns the default value" do
      expect(".test-1").to have_rule("content: 1em")
    end
  end

  context "fetches the rails-asset-pipeline setting" do
    it "and returns the user-overridden value" do
      expect(".test-2").to have_rule("content: true")
    end
  end

  context "called from the font-face mixin" do
    it "outputs user-overridden font file formats" do
      ruleset = 'font-family: "source-sans-pro"; ' +
                'src: font-url("source-sans-pro-regular.woff2") ' +
                     'format("woff2"), ' +
                     'font-url("source-sans-pro-regular.woff") ' +
                     'format("woff");'

      expect("@font-face").to have_ruleset(ruleset)
    end
  end
end