page_magic 1.0.0.alpha → 1.0.0.alpha2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/page_magic/session.rb +20 -5
- data/spec/page_magic/session_spec.rb +28 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f197806fbf5357bdcf84663abfbf87902c89f835
|
4
|
+
data.tar.gz: da30e148aa6cc6782c0d0c399fc45c43d00ee3d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c3b3277416e23f0767b0e0f4b63879882fd9c1872fedb290fb727be512d91b06b53056dac552c539a65cec17f20e831e4d17cd7e97ef1bdfeee941eb528e6a9
|
7
|
+
data.tar.gz: 8d953ec0f7af0749bedca9396caf556098b7b74b5d09f2c82a4b904bbb41fc604bc7e3b0bae87e10d367e2496546342ac31f07652be996b799ddea4b7c71ec3a
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.0.
|
1
|
+
1.0.0.alpha2
|
data/lib/page_magic/session.rb
CHANGED
@@ -7,20 +7,35 @@ module PageMagic
|
|
7
7
|
@raw_session = browser
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def define_page_mappings transitions
|
11
11
|
@transitions = transitions
|
12
12
|
end
|
13
13
|
|
14
14
|
def current_page
|
15
15
|
if transitions
|
16
|
-
mapping =
|
17
|
-
|
18
|
-
end
|
19
|
-
@current_page = transitions[mapping].new(self) if transitions[mapping]
|
16
|
+
mapping = find_mapped_page(current_path)
|
17
|
+
@current_page = mapping.new(self) if mapping
|
20
18
|
end
|
21
19
|
@current_page
|
22
20
|
end
|
23
21
|
|
22
|
+
def find_mapped_page path
|
23
|
+
mapping = transitions.keys.find do |key|
|
24
|
+
string_matches?(path, key)
|
25
|
+
end
|
26
|
+
transitions[mapping]
|
27
|
+
end
|
28
|
+
|
29
|
+
def string_matches?(string, matcher)
|
30
|
+
if matcher.is_a?(Regexp)
|
31
|
+
string =~ matcher
|
32
|
+
elsif matcher.is_a?(String)
|
33
|
+
string == matcher
|
34
|
+
else
|
35
|
+
false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
24
39
|
def visit page
|
25
40
|
raw_session.visit page.url
|
26
41
|
@current_page = page.new self
|
@@ -3,7 +3,7 @@ describe PageMagic::Session do
|
|
3
3
|
let(:page) do
|
4
4
|
Class.new do
|
5
5
|
include PageMagic
|
6
|
-
url
|
6
|
+
url '/page1'
|
7
7
|
|
8
8
|
def my_method
|
9
9
|
:called
|
@@ -23,26 +23,50 @@ describe PageMagic::Session do
|
|
23
23
|
describe '#current_page' do
|
24
24
|
subject do
|
25
25
|
PageMagic::Session.new(browser).tap do |session|
|
26
|
-
session.
|
26
|
+
session.define_page_mappings '/another_page1' => another_page_class
|
27
27
|
end
|
28
28
|
end
|
29
29
|
context 'page url has not changed' do
|
30
30
|
it 'returns the original page' do
|
31
31
|
browser.should_receive(:visit).with(page.url)
|
32
|
+
allow(browser).to receive(:current_path).and_return('/page1')
|
32
33
|
subject.visit(page)
|
33
34
|
expect(subject.current_page).to be_an_instance_of(page)
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
37
38
|
context 'page url has changed' do
|
38
|
-
|
39
39
|
it 'returns the mapped page object' do
|
40
40
|
browser.should_receive(:visit).with(page.url)
|
41
41
|
subject.visit(page)
|
42
|
-
allow(browser).to receive(:
|
42
|
+
allow(browser).to receive(:current_path).and_return('/another_page1')
|
43
43
|
expect(subject.current_page).to be_an_instance_of(another_page_class)
|
44
44
|
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#find_mapped_page' do
|
49
|
+
subject do
|
50
|
+
described_class.new(nil).tap do |session|
|
51
|
+
session.define_page_mappings '/page' => :mapped_page_using_string, /page\d/ => :mapped_page_using_regex
|
52
|
+
end
|
53
|
+
end
|
45
54
|
|
55
|
+
context 'mapping is string' do
|
56
|
+
it 'returns the page class' do
|
57
|
+
expect(subject.find_mapped_page('/page')).to be(:mapped_page_using_string)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
context 'mapping is regex' do
|
61
|
+
it 'returns the page class' do
|
62
|
+
expect(subject.find_mapped_page('/page2')).to be(:mapped_page_using_regex)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'mapping is not found' do
|
67
|
+
it 'returns nil' do
|
68
|
+
expect(subject.find_mapped_page('/fake_page')).to be(nil)
|
69
|
+
end
|
46
70
|
end
|
47
71
|
end
|
48
72
|
|