File: has_current_path_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 (151 lines) | stat: -rw-r--r-- 5,097 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# frozen_string_literal: true

Capybara::SpecHelper.spec '#has_current_path?' do
  before do
    @session.visit('/with_js')
  end

  it 'should be true if the page has the given current path' do
    expect(@session).to have_current_path('/with_js')
  end

  it 'should allow regexp matches' do
    expect(@session).to have_current_path(/w[a-z]{3}_js/)
    expect(@session).not_to have_current_path(/monkey/)
  end

  it 'should not raise an error when non-http' do
    @session.reset_session!
    expect(@session.has_current_path?(/monkey/)).to be false
    expect(@session.has_current_path?('/with_js')).to be false
  end

  it 'should handle non-escaped query options' do
    @session.click_link('Non-escaped query options')
    expect(@session).to have_current_path('/with_html?options[]=things')
  end

  it 'should handle escaped query options' do
    @session.click_link('Escaped query options')
    expect(@session).to have_current_path('/with_html?options%5B%5D=things')
  end

  it 'should wait for current_path', requires: [:js] do
    @session.click_link('Change page')
    expect(@session).to have_current_path('/with_html', wait: 3)
  end

  it 'should be false if the page has not the given current_path' do
    expect(@session).not_to have_current_path('/with_html')
  end

  it 'should check query options' do
    @session.visit('/with_js?test=test')
    expect(@session).to have_current_path('/with_js?test=test')
  end

  it 'should compare the full url if url: true is used' do
    expect(@session).to have_current_path(%r{\Ahttp://[^/]*/with_js\Z}, url: true)
    domain_port = if @session.respond_to?(:server) && @session.server
      "#{@session.server.host}:#{@session.server.port}"
    else
      'www.example.com'
    end
    expect(@session).to have_current_path("http://#{domain_port}/with_js", url: true)
  end

  it 'should not compare the full url if url: true is not passed' do
    expect(@session).to have_current_path(%r{^/with_js\Z})
    expect(@session).to have_current_path('/with_js')
  end

  it 'should not compare the full url if url: false is passed' do
    expect(@session).to have_current_path(%r{^/with_js\Z}, url: false)
    expect(@session).to have_current_path('/with_js', url: false)
  end

  it 'should default to full url if value is a url' do
    url = @session.current_url
    expect(url).to match(/with_js$/)
    expect(@session).to have_current_path(url)
    expect(@session).not_to have_current_path('http://www.not_example.com/with_js')
  end

  it 'should ignore the query' do
    @session.visit('/with_js?test=test')
    expect(@session).to have_current_path('/with_js?test=test')
    expect(@session).to have_current_path('/with_js', ignore_query: true)
    uri = Addressable::URI.parse(@session.current_url)
    uri.query = nil
    expect(@session).to have_current_path(uri.to_s, ignore_query: true)
  end

  it 'should not raise an exception if the current_url is nil' do
    allow(@session).to receive(:current_url).and_return(nil)
    allow(@session.page).to receive(:current_url).and_return(nil) if @session.respond_to? :page

    # Without ignore_query option
    expect do
      expect(@session).to have_current_path(nil)
    end.not_to raise_exception

    # With ignore_query option
    expect do
      expect(@session).to have_current_path(nil, ignore_query: true)
    end.not_to raise_exception
  end

  it 'should accept a filter block that receives Addressable::URL' do
    @session.visit('/with_js?a=3&b=defgh')
    expect(@session).to have_current_path('/with_js', ignore_query: true) { |url|
      url.query_values.keys == %w[a b]
    }
  end
end

Capybara::SpecHelper.spec '#has_no_current_path?' do
  before do
    @session.visit('/with_js')
  end

  it 'should be false if the page has the given current_path' do
    expect(@session).not_to have_no_current_path('/with_js')
  end

  it 'should allow regexp matches' do
    expect(@session).not_to have_no_current_path(/w[a-z]{3}_js/)
    expect(@session).to have_no_current_path(/monkey/)
  end

  it 'should wait for current_path to disappear', requires: [:js] do
    Capybara.using_wait_time(3) do
      @session.click_link('Change page')
      expect(@session).to have_no_current_path('/with_js')
    end
  end

  it 'should be true if the page has not the given current_path' do
    expect(@session).to have_no_current_path('/with_html')
  end

  it 'should not raise an exception if the current_url is nil' do
    allow(@session).to receive(:current_url).and_return(nil)
    allow(@session.page).to receive(:current_url).and_return(nil) if @session.respond_to? :page

    # Without ignore_query option
    expect do
      expect(@session).not_to have_current_path('/with_js')
    end.not_to raise_exception

    # With ignore_query option
    expect do
      expect(@session).not_to have_current_path('/with_js', ignore_query: true)
    end.not_to raise_exception
  end

  it 'should accept a filter block that receives Addressable::URL' do
    expect(@session).to have_no_current_path('/with_js', ignore_query: true) { |url|
      !url.query.nil?
    }
  end
end