|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::RSpec::Capybara::SpecificMatcher do |
| 4 | + it 'does not register an offense for abstract matcher when ' \ |
| 5 | + 'first argument is not a replaceable element' do |
| 6 | + expect_no_offenses(<<-RUBY) |
| 7 | + expect(page).to have_selector('article') |
| 8 | + expect(page).to have_no_selector('body') |
| 9 | + expect(page).to have_css('tbody') |
| 10 | + RUBY |
| 11 | + end |
| 12 | + |
| 13 | + it 'does not register an offense for abstract matcher when ' \ |
| 14 | + 'first argument is not an element' do |
| 15 | + expect_no_offenses(<<-RUBY) |
| 16 | + expect(page).to have_no_css('.a') |
| 17 | + expect(page).to have_selector('#button') |
| 18 | + expect(page).to have_no_selector('[table]') |
| 19 | + RUBY |
| 20 | + end |
| 21 | + |
| 22 | + it 'registers an offense when using `have_selector`' do |
| 23 | + expect_offense(<<-RUBY) |
| 24 | + expect(page).to have_selector('button') |
| 25 | + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer `have_button` over `have_selector`. |
| 26 | + expect(page).to have_selector('a') |
| 27 | + ^^^^^^^^^^^^^^^^^^ Prefer `have_link` over `have_selector`. |
| 28 | + expect(page).to have_selector('table') |
| 29 | + ^^^^^^^^^^^^^^^^^^^^^^ Prefer `have_table` over `have_selector`. |
| 30 | + expect(page).to have_selector('select') |
| 31 | + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer `have_select` over `have_selector`. |
| 32 | + RUBY |
| 33 | + end |
| 34 | + |
| 35 | + it 'registers an offense when using `have_no_selector`' do |
| 36 | + expect_offense(<<-RUBY) |
| 37 | + expect(page).to have_no_selector('button') |
| 38 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `have_no_button` over `have_no_selector`. |
| 39 | + RUBY |
| 40 | + end |
| 41 | + |
| 42 | + it 'registers an offense when using `have_css`' do |
| 43 | + expect_offense(<<-RUBY) |
| 44 | + expect(page).to have_css('button') |
| 45 | + ^^^^^^^^^^^^^^^^^^ Prefer `have_button` over `have_css`. |
| 46 | + RUBY |
| 47 | + end |
| 48 | + |
| 49 | + it 'registers an offense when using `have_no_css`' do |
| 50 | + expect_offense(<<-RUBY) |
| 51 | + expect(page).to have_no_css('button') |
| 52 | + ^^^^^^^^^^^^^^^^^^^^^ Prefer `have_no_button` over `have_no_css`. |
| 53 | + RUBY |
| 54 | + end |
| 55 | + |
| 56 | + it 'registers an offense when using abstract matcher and other args' do |
| 57 | + expect_offense(<<-RUBY) |
| 58 | + expect(page).to have_css('button', exact_text: 'foo') |
| 59 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `have_button` over `have_css`. |
| 60 | + RUBY |
| 61 | + end |
| 62 | + |
| 63 | + it 'registers an offense when using abstract matcher with state' do |
| 64 | + expect_offense(<<-RUBY) |
| 65 | + expect(page).to have_css('button[disabled]', exact_text: 'foo') |
| 66 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `have_button` over `have_css`. |
| 67 | + expect(page).to have_css('button:not([disabled])', exact_text: 'bar') |
| 68 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `have_button` over `have_css`. |
| 69 | + RUBY |
| 70 | + end |
| 71 | + |
| 72 | + it 'does not register an offense for abstract matcher when ' \ |
| 73 | + 'first argument is element with nonreplaceable attributes' do |
| 74 | + expect_no_offenses(<<-RUBY) |
| 75 | + expect(page).to have_css('button[foo=bar]') |
| 76 | + expect(page).to have_css('button[foo-bar=baz]', exact_text: 'foo') |
| 77 | + RUBY |
| 78 | + end |
| 79 | + |
| 80 | + it 'does not register an offense for abstract matcher when ' \ |
| 81 | + 'first argument is dstr' do |
| 82 | + expect_no_offenses(<<-'RUBY') |
| 83 | + expect(page).to have_css(%{a[href="#{foo}"]}, text: "bar") |
| 84 | + RUBY |
| 85 | + end |
| 86 | +end |
0 commit comments