Back to Repositories

Testing FilterSet Component Implementation in Capybara

This test suite examines the functionality of Capybara’s Selector::FilterSet class, focusing on filter management and validation. It verifies the implementation of node filters and expression filters, ensuring proper separation and functionality of these distinct filter types.

Test Coverage Overview

The test suite provides comprehensive coverage of FilterSet’s core functionality:

  • Node filter implementation and validation
  • Expression filter handling
  • Filter naming and uniqueness verification
  • Alias functionality for filter methods

Implementation Analysis

The testing approach employs RSpec’s describe/it pattern to systematically verify FilterSet behaviors. Each test case isolates specific filter functionality, using boolean filters as test cases and validating both the presence and isolation of filters in their respective collections.

Technical Details

  • Testing Framework: RSpec
  • Test Environment: Isolated spec environment with cleanup
  • Key Components: Capybara::Selector::FilterSet class
  • Filter Types: Node filters and Expression filters

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Proper test isolation with after hooks
  • Clear test case separation
  • Explicit expectations for filter presence
  • Verification of filter independence

teamcapybara/capybara

spec/filter_set_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Capybara::Selector::FilterSet do
  after do
    described_class.remove(:test)
  end

  it 'allows node filters' do
    fs = described_class.add(:test) do
      node_filter(:node_test, :boolean) { |_node, _value| true }
      expression_filter(:expression_test, :boolean) { |_expr, _value| true }
    end

    expect(fs.node_filters.keys).to include(:node_test)
    expect(fs.node_filters.keys).not_to include(:expression_test)
  end

  it 'allows expression filters' do
    fs = described_class.add(:test) do
      node_filter(:node_test, :boolean) { |_node, _value| true }
      expression_filter(:expression_test, :boolean) { |_expr, _value| true }
    end

    expect(fs.expression_filters.keys).to include(:expression_test)
    expect(fs.expression_filters.keys).not_to include(:node_test)
  end

  it 'allows node filter and expression filter with the same name' do
    fs = described_class.add(:test) do
      node_filter(:test, :boolean) { |_node, _value| true }
      expression_filter(:test, :boolean) { |_expr, _value| true }
    end

    expect(fs.expression_filters[:test]).not_to eq fs.node_filters[:test]
  end

  it 'allows `filter` as an alias of `node_filter`' do
    fs = described_class.add(:test) do
      filter(:node_test, :boolean) { |_node, _value| true }
    end

    expect(fs.node_filters.keys).to include(:node_test)
  end
end