File: has_ancestor_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 (46 lines) | stat: -rw-r--r-- 1,324 bytes parent folder | download
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
# frozen_string_literal: true

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

  it 'should assert an ancestor using the given locator' do
    el = @session.find(:css, '#ancestor1')
    expect(el).to have_ancestor(:css, '#ancestor2')
  end

  it 'should assert an ancestor even if not parent' do
    el = @session.find(:css, '#child')
    expect(el).to have_ancestor(:css, '#ancestor3')
  end

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

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

    expect do
      expect(el).to have_ancestor(:css, 'div').once
    end.to raise_error(RSpec::Expectations::ExpectationNotMetError)

    expect(el).to have_ancestor(:css, 'div').exactly(3).times
  end
end

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

  it 'should assert no matching ancestor' do
    el = @session.find(:css, '#ancestor1')
    expect(el).to have_no_ancestor(:css, '#child')
    expect(el).to have_no_ancestor(:css, '#ancestor1_sibling')
    expect(el).not_to have_ancestor(:css, '#child')
    expect(el).not_to have_ancestor(:css, '#ancestor1_sibling')
  end
end