Back to Repositories

Testing Whitespace Normalization Implementation in teamcapybara/capybara

This test suite validates the whitespace normalization functionality in Capybara’s Node module. It ensures proper handling of special characters, spacing, and text formatting for web elements in automated tests.

Test Coverage Overview

The test suite comprehensively covers whitespace normalization in Capybara’s Node module.

Key areas tested include:
  • Basic text normalization without special characters
  • Compression of excess breaking spaces
  • Handling of non-breaking spaces
  • Processing of directional text marks (RTL/LTR)
  • Management of zero-width spaces

Implementation Analysis

The implementation uses RSpec’s behavior-driven development approach with focused test cases.

Testing patterns include:
  • Subject-let pattern for test setup
  • Heredoc syntax for complex test data
  • Module inclusion testing through anonymous class creation
  • Separate handling of visible and standard spacing normalization

Technical Details

Testing infrastructure includes:
  • RSpec as the testing framework
  • Capybara::Node::WhitespaceNormalizer module
  • Custom test class implementation
  • Special character constants (NON_BREAKING_SPACE, ZERO_WIDTH_SPACE, etc.)
  • Ruby’s frozen_string_literal pragma

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through careful organization and thorough coverage.

Notable practices include:
  • Isolated test scenarios for different normalization cases
  • Clear separation of visible vs. non-visible spacing tests
  • Comprehensive edge case handling
  • Clean and maintainable test structure
  • Effective use of RSpec’s describe/it blocks

teamcapybara/capybara

spec/whitespace_normalizer_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'
require 'capybara/node/whitespace_normalizer'

RSpec.describe Capybara::Node::WhitespaceNormalizer do
  subject(:normalizer) do
    klass = Class.new do
      include Capybara::Node::WhitespaceNormalizer
    end

    klass.new
  end

  let(:text_needing_correction) do
    <<~TEXT
      Some     #{described_class::NON_BREAKING_SPACE}text
      #{described_class::RIGHT_TO_LEFT_MARK}
      #{described_class::ZERO_WIDTH_SPACE * 30}
      #{described_class::LEFT_TO_RIGHT_MARK}
      Here
    TEXT
  end

  describe '#normalize_spacing' do
    it 'does nothing to text not containing special characters' do
      expect(normalizer.normalize_spacing('text')).to eq('text')
    end

    it 'compresses excess breaking spacing' do
      expect(
        normalizer.normalize_spacing(text_needing_correction)
      ).to eq('Some  text Here')
    end
  end

  describe '#normalize_visible_spacing' do
    it 'does nothing to text not containing special characters' do
      expect(normalizer.normalize_visible_spacing('text')).to eq('text')
    end

    it 'compresses excess breaking visible spacing' do
      expect(
        normalizer.normalize_visible_spacing(text_needing_correction)
      ).to eq <<~TEXT.chomp
        Some  text
        #{described_class::RIGHT_TO_LEFT_MARK}
        #{described_class::ZERO_WIDTH_SPACE * 30}
        #{described_class::LEFT_TO_RIGHT_MARK}
        Here
      TEXT
    end
  end
end