File: has_sibling_spec.rb

package info (click to toggle)
ruby-capybara 3.40.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,368 kB
  • sloc: ruby: 23,988; javascript: 752; makefile: 11
file content (50 lines) | stat: -rw-r--r-- 1,508 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

Capybara::SpecHelper.spec '#have_sibling' do
  before do
    @session.visit('/with_html')
  end

  it 'should assert a prior sibling element using the given locator' do
    el = @session.find(:css, '#mid_sibling')
    expect(el).to have_sibling(:css, '#pre_sibling')
  end

  it 'should assert a following sibling element using the given locator' do
    el = @session.find(:css, '#mid_sibling')
    expect(el).to have_sibling(:css, '#post_sibling')
  end

  it 'should not raise an error if there are multiple matches' do
    el = @session.find(:css, '#mid_sibling')
    expect(el).to have_sibling(:css, 'div')
  end

  it 'should allow counts to be specified' do
    el = @session.find(:css, '#mid_sibling')

    expect(el).to have_sibling(:css, 'div').exactly(2).times
    expect do
      expect(el).to have_sibling(:css, 'div').once
    end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
  end
end

Capybara::SpecHelper.spec '#have_no_sibling' do
  before do
    @session.visit('/with_html')
  end

  it 'should assert no matching sibling' do
    el = @session.find(:css, '#mid_sibling')
    expect(el).to have_no_sibling(:css, '#not_a_sibling')
    expect(el).not_to have_sibling(:css, '#not_a_sibling')
  end

  it 'should raise if there are matching siblings' do
    el = @session.find(:css, '#mid_sibling')
    expect do
      expect(el).to have_no_sibling(:css, '#pre_sibling')
    end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
  end
end