# frozen_string_literal: true RSpec.describe PageMagic::Element::Selector do describe '.find' do it 'returns the constant with the given name' do expect(described_class.find(:css)).to be(described_class::CSS) end context 'when selector not found' do it 'raises an exception' do expect { described_class.find(:invalid) }.to raise_exception(PageMagic::UnsupportedCriteriaException) end end end describe '#build' do it 'puts the locator and element type in to the result' do expect(described_class.new.build(:field, :locator)).to have_attributes( args: [:locator] ) end context 'when exact matching is required' do it 'is added to options' do expect(described_class.new(exact: true).build(:field, :locator)).to have_attributes( options: { exact: true } ) end end context 'when supports_type true' do it 'includes the element type' do expect(described_class.new(supports_type: true).build(:field, :locator)).to have_attributes( args: %i[field locator] ) end end # TODO: - new class? context 'when selector formatter is provided' do subject(:selector) do described_class.new do |param| "formatted_#{param}".to_sym end end it 'uses the formatter' do expect(selector.build(:field, :locator)).to have_attributes( args: [:formatted_locator] ) end end context 'when type supplied' do it 'is added to the result' do expect(described_class.new(:css).build(:field, :locator)).to have_attributes( args: %i[css locator] ) end end end describe '#initialize' do context 'when exact' do it 'sets the option' do subject = described_class.new(exact: true).build(:element_type, {}) expect(subject.options).to eq({ exact: true }) end end end describe 'predefined selectors' do shared_examples 'a selector' do |named: false, **options| def selector_name self.class.metadata[:parent_example_group][:description].to_sym end subject do described_class.find(selector_name).build(:element_type, :locator) end it 'contains the selector args' do name_arg = named && (named == true ? selector_name : named) expected_args = [name_arg, :locator].compact expect(subject.args).to eq(expected_args) end it 'contains the options' do expect(subject.options).to eq(options) end end let(:capybara_element) do html_source = <<~HTML_SOURCE a link