Back to Repositories

Testing Text Input Selector Handling Implementation in Bourbon

This test suite validates the assign-inputs utility in Bourbon, focusing on handling different types of text input selectors and their pseudo-classes. It ensures proper CSS rule application across various input configurations and selector combinations.

Test Coverage Overview

The test suite comprehensively covers text input selector handling with multiple scenarios:

  • Basic text input selector validation
  • Pseudo-class combinations with input selectors
  • List position testing (first, middle positions)
  • Multiple input type combinations

Implementation Analysis

The testing approach utilizes RSpec’s describe/context/it blocks to organize test cases logically. It leverages before(:all) hooks for setup and employs custom ParserSupport functionality for CSS parsing. The implementation demonstrates effective use of array manipulation and iteration patterns.

Technical Details

  • Testing Framework: RSpec
  • Custom Support: ParserSupport class for CSS parsing
  • Test Data: Predefined array of CSS selectors
  • Assertion Method: Custom have_rule matcher

Best Practices Demonstrated

The test suite exhibits several testing best practices:

  • DRY principle through shared setup and list reuse
  • Structured test organization using context blocks
  • Clear test case isolation
  • Consistent assertion patterns
  • Efficient test data management

thoughtbot/bourbon

spec/bourbon/utilities/assign_inputs_spec.rb

            
require "spec_helper"

describe "assign-inputs" do
  before(:all) do
    ParserSupport.parse_file("utilities/assign-inputs")
    @text_inputs_list = [
      "[type='password']",
      "[type='text']",
      "textarea"
    ]
  end

  context "expands plain text inputs" do
    it "finds selectors" do
      @text_inputs_list.each do |input|
        expect(input).to have_rule("color: #f00")
      end
    end
  end

  context "expands text inputs with pseudo classes" do
    it "finds selectors" do
      list = @text_inputs_list.dup
      list.map! { |input| input + ":active" }
      list.each do |input|
        expect(input).to have_rule("color: #0f0")
      end
    end
  end

  context "expands text inputs when first in list" do
    it "finds selectors" do
      list = @text_inputs_list.dup
      list.push "select"
      list.each do |input|
        expect(input).to have_rule("color: #00f")
      end
    end
  end

  context "expands text inputs when middle of list" do
    it "finds selectors" do
      list = @text_inputs_list.dup
      list.unshift "[type=\"file\"]"
      list.each do |input|
        expect(input).to have_rule("color: #f0f")
      end
    end
  end
end