mobilify 1.2.0 → 1.2.1

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.
@@ -0,0 +1,6 @@
1
+ # Change Log
2
+
3
+ ### v1.2.1 (Nov 17, 2014)
4
+ * `Mobilify#context` returns the page object instance's contexts (as an array)
5
+ * `Mobilify#context?` is an alias of `Mobilify#context`
6
+
@@ -4,37 +4,73 @@ require 'page-object'
4
4
  module Mobilify
5
5
  include PageObject
6
6
 
7
+ #
8
+ # Build page object with context(s) for method replacement
9
+ #
7
10
  def initialize(browser, opts = {})
8
11
  super(browser, opts[:visit] || false)
9
- @context = opts[:context]
12
+ @context = set_context opts[:context]
10
13
  mobilify! if @context
11
14
  end
12
15
 
13
- def self.included(klass)
16
+ #
17
+ # Returns context(s) passed to page object instance construction
18
+ #
19
+ # @return [Array]
20
+ #
21
+ def context
22
+ @context
23
+ end
24
+ alias :context? :context
25
+
26
+ private
27
+
28
+ #
29
+ # Include page-object too
30
+ #
31
+ def self.included klass
14
32
  klass.send :include, PageObject
15
33
  end
16
34
 
17
- def context?
18
- @context
35
+ #
36
+ # Format given context(s) to Mobilify's liking
37
+ #
38
+ def set_context config
39
+ return nil if config.nil?
40
+ config.kind_of?(Array) ? config.flatten.map(&:to_sym) : [config].map(&:to_sym)
19
41
  end
20
42
 
43
+ #
44
+ # Iterate over contexts for method replacement
45
+ #
21
46
  def mobilify!
22
- match = false
23
- (contexts = []) << @context
24
-
25
- contexts.flatten.each do |c|
26
- next if match
27
- context = c.to_s
28
- methods.
29
- select { |m| m.to_s.start_with? "#{context}_" }.
30
- select { |m| respond_to? m.to_s.gsub("#{context}_", '') }.
31
- map { |m| self.method(m) }.each do |method|
32
- (class << self; self; end).class_eval do
33
- define_method(method.name.to_s.gsub("#{context}_", ''), method)
34
- match = true
35
- end
36
- end
47
+ @context.each_with_object([]) do |c, array|
48
+ next unless array.flatten.empty?
49
+ array << match_and_replace(c.to_s)
37
50
  end
38
51
  end
39
52
 
53
+ #
54
+ # Method replacement
55
+ #
56
+ def match_and_replace context
57
+ get_contextual_match(context).map { |m| self.method(m) }.each do |method|
58
+ replace! method, context
59
+ end
60
+ end
61
+
62
+ #
63
+ # Find page object methods matching the given context
64
+ #
65
+ def get_contextual_match context
66
+ methods.select{ |m| m.to_s.start_with? "#{context}_" }.select{ |m| respond_to? m.to_s.gsub("#{context}_","") }
67
+ end
68
+
69
+ #
70
+ # Redefine contextually matching method as original method
71
+ #
72
+ def replace! method, context
73
+ (class << self; self; end).class_eval{ define_method(method.name.to_s.gsub("#{context}_",""), method) }
74
+ end
75
+
40
76
  end
@@ -1,3 +1,3 @@
1
1
  module Mobilify
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -0,0 +1,159 @@
1
+ require 'rspec'
2
+ require 'support/page'
3
+
4
+ describe Mobilify do
5
+ before :all do
6
+ @browser = Watir::Browser.new
7
+ @mobilify = ->(context = nil){ Page.new @browser, context: context }
8
+ end
9
+
10
+ it "includes PageObject" do
11
+ expect(Page.included_modules[1]).to eq PageObject
12
+ end
13
+
14
+ context "when given no context" do
15
+ it "does not perform method replacement" do
16
+ page = @mobilify.call
17
+ expect(page.login_element.inspect).to include("log-in-link ellipsis highlight")
18
+ end
19
+ end
20
+
21
+ context "when given a single context" do
22
+ it "does not perform method replacement for an invalid context" do
23
+ page = @mobilify.call 'wrong'
24
+ expect(page.login_element.inspect).to include("log-in-link ellipsis highlight")
25
+ end
26
+
27
+ it "does not perform method replacement without an original method" do
28
+ page = @mobilify.call 'other'
29
+ expect(page.other_button_element.inspect).to include("other-button-class")
30
+ end
31
+
32
+ it "does not perform method replacement without the correct context" do
33
+ page = @mobilify.call 'mobile'
34
+ expect(page.other_button_element.inspect).to include("other-button-class")
35
+ end
36
+
37
+ it "performs method replacement for a valid context" do
38
+ page = @mobilify.call 'mobile'
39
+ expect(page.login_element.inspect).to include("simple-button big-button")
40
+ end
41
+ end
42
+
43
+ context "when given multiple contexts" do
44
+ it "does not perform method replacement for invalid contexts" do
45
+ page = @mobilify.call ['no_match', 'wrong']
46
+ expect(page.login_element.inspect).to include("log-in-link ellipsis highlight")
47
+ end
48
+
49
+ context "and the first context matches" do
50
+ it "performs method replacement with the first matching context" do
51
+ page = @mobilify.call ['mobile', 'wrong']
52
+ expect(page.login_element.inspect).to include("simple-button big-button")
53
+ end
54
+
55
+ it "does not perform method replacement with the second matching context" do
56
+ page = @mobilify.call ['mobile', 'desktop']
57
+ expect(page.login_element.inspect).to include("simple-button big-button")
58
+ end
59
+ end
60
+
61
+ context "and the second context matches" do
62
+ it "performs method replacement with the second context" do
63
+ page = @mobilify.call ['wrong', 'mobile']
64
+ expect(page.login_element.inspect).to include("simple-button big-button")
65
+ end
66
+ end
67
+
68
+ context "and the first and last contexts match" do
69
+ it "performs method replacement with the first matching context" do
70
+ page = @mobilify.call ['mobile', 'wrong', 'ignore', 'nothing', 'desktop']
71
+ expect(page.login_element.inspect).to include("simple-button big-button")
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "#initialize" do
77
+ context "without context" do
78
+ it "returns the page object instance" do
79
+ expect(@mobilify.call).to be_a Page
80
+ end
81
+ end
82
+
83
+ context "with context" do
84
+ it "returns the page object instance" do
85
+ expect(@mobilify.call 'mobile').to be_a Page
86
+ end
87
+ end
88
+ end
89
+
90
+ describe "#context" do
91
+ it "returns nil when for context" do
92
+ page = @mobilify.call
93
+ expect(page.context).to be_nil
94
+ end
95
+
96
+ it "returns an array for one context" do
97
+ page = @mobilify.call 'desktop'
98
+ expect(page.context).to eq [:desktop]
99
+ end
100
+
101
+ it "returns an array for multiple contexts" do
102
+ page = @mobilify.call ['one', 'two', 'three']
103
+ expect(page.context).to eq [:one, :two, :three]
104
+ end
105
+
106
+ it "can be called as #context?" do
107
+ page = @mobilify.call 'desktop'
108
+ expect(page.context?).to eq [:desktop]
109
+ end
110
+ end
111
+
112
+ describe "#set_context" do
113
+ let(:page){ @mobilify.call }
114
+
115
+ it "returns nil when given nil" do
116
+ expect(page.send :set_context, nil).to be_nil
117
+ end
118
+
119
+ context "when given a single context" do
120
+ it "converts context into an array" do
121
+ expect(page.send :set_context, :one).to eq [:one]
122
+ end
123
+
124
+ it "converts strings to symbols" do
125
+ expect(page.send :set_context, "one").to eq [:one]
126
+ end
127
+
128
+ it "flattens a single-element array" do
129
+ expect(page.send :set_context, [:one]).to eq [:one]
130
+ end
131
+ end
132
+
133
+ context "when given multiple contexts" do
134
+ it "converts strings to symbols" do
135
+ expect(page.send :set_context, ["one", "two"]).to eq [:one, :two]
136
+ end
137
+
138
+ it "flattens multiple arrays" do
139
+ expect(page.send :set_context, [[:one, :two], [:three, :four]]).to eq [:one, :two, :three, :four]
140
+ end
141
+ end
142
+ end
143
+
144
+ describe "#get_contextual_match" do
145
+ let(:page){ @mobilify.call }
146
+
147
+ it "returns an array" do
148
+ expect(page.send :get_contextual_match, "one").to be_an Array
149
+ end
150
+
151
+ it "returns an empty array with invalid context" do
152
+ expect(page.send :get_contextual_match, "one").to be_empty
153
+ end
154
+
155
+ it "returns three-item array with valid context" do
156
+ expect(page.send(:get_contextual_match, "mobile").size).to eq 3
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,12 @@
1
+ require 'mobilify'
2
+
3
+ class Page
4
+ include Mobilify
5
+
6
+ link(:login, class: "log-in-link ellipsis highlight")
7
+ link(:desktop_login, class: "log-in-link ellipsis highlight")
8
+ link(:mobile_login, class: "simple-button big-button")
9
+
10
+ button(:other_button, class: "other-button-class")
11
+ end
12
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobilify
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-21 00:00:00.000000000 Z
12
+ date: 2014-11-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: page-object
@@ -135,14 +135,12 @@ files:
135
135
  - LICENSE.txt
136
136
  - README.md
137
137
  - Rakefile
138
+ - changes.md
138
139
  - lib/mobilify.rb
139
140
  - lib/mobilify/version.rb
140
141
  - mobilify.gemspec
141
- - spec/multiple_context_spec.rb
142
- - spec/page_object_spec.rb
143
- - spec/simple_context_spec.rb
144
- - spec/spec_helper.rb
145
- - spec/support/khan_academy.rb
142
+ - spec/mobilify_spec.rb
143
+ - spec/support/page.rb
146
144
  homepage: http://github.com/jdenen/mobilify
147
145
  licenses:
148
146
  - MIT
@@ -164,13 +162,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
162
  version: '0'
165
163
  requirements: []
166
164
  rubyforge_project:
167
- rubygems_version: 1.8.23
165
+ rubygems_version: 1.8.23.2
168
166
  signing_key:
169
167
  specification_version: 3
170
168
  summary: page-object methods invoked with one call but defined contextually
171
169
  test_files:
172
- - spec/multiple_context_spec.rb
173
- - spec/page_object_spec.rb
174
- - spec/simple_context_spec.rb
175
- - spec/spec_helper.rb
176
- - spec/support/khan_academy.rb
170
+ - spec/mobilify_spec.rb
171
+ - spec/support/page.rb
172
+ has_rdoc:
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mobilify do
4
- after :each do
5
- @browser.close
6
- end
7
-
8
- describe "uses default method when given contexts do not apply" do
9
- Given(:desktop) { KhanAcademy.new(desktop_browser, { :visit => true, :context => [:no_match, :wrong] }) }
10
- Then { desktop.login_element.inspect.should include("log-in-link ellipsis highlight") }
11
- end
12
-
13
- describe "uses only applicable context given" do
14
- context "when applicable context is first in the array" do
15
- Given(:mobile) { KhanAcademy.new(mobile_browser, { :visit => true, :context => [:mobile, :wrong] }) }
16
- Then { mobile.login_element.inspect.should include("simple-button big-button") }
17
- end
18
-
19
- context "when applicable context is second in the array" do
20
- Given(:mobile) { KhanAcademy.new(mobile_browser, { :visit => true, :context => [:wrong, :mobile] }) }
21
- Then { mobile.login_element.inspect.should include("simple-button big-button") }
22
- end
23
- end
24
-
25
- describe "uses first context given when multiple applicable contexts are given" do
26
- Given(:mobile) { KhanAcademy.new(mobile_browser, { :visit => true, :context => [:mobile, :ignored] }) }
27
- Then { mobile.login_element.inspect.should include("simple-button big-button") }
28
- end
29
- end
@@ -1,11 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mobilify do
4
-
5
- describe "includes PageObject when included" do
6
- Given { class Page; end }
7
- When { Page.send :include, Mobilify }
8
- Then { Page.included_modules[1].should == PageObject }
9
- end
10
-
11
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mobilify do
4
- after :each do
5
- @browser.close
6
- end
7
-
8
- describe "uses default method when no context is given" do
9
- Given(:desktop) { KhanAcademy.new(desktop_browser, { :visit => true }) }
10
- Then { desktop.login_element.inspect.should include("log-in-link ellipsis highlight") }
11
- end
12
-
13
- describe "uses default method when non-applicable context is given" do
14
- Given(:desktop) { KhanAcademy.new(desktop_browser, { :visit => true, :context => :wrong }) }
15
- Then { desktop.login_element.inspect.should include("log-in-link ellipsis highlight") }
16
- end
17
-
18
- describe "uses contextual method for the given context" do
19
- Given(:mobile) { KhanAcademy.new(mobile_browser, { :context => :mobile, :visit => true }) }
20
- Then { mobile.login_element.inspect.should include("simple-button big-button") }
21
- end
22
- end
23
-
@@ -1,15 +0,0 @@
1
- require 'rspec'
2
- require 'rspec-given'
3
- require 'watir-webdriver'
4
- require 'webdriver-user-agent'
5
- require './lib/mobilify'
6
- require 'support/khan_academy'
7
-
8
- def desktop_browser
9
- @browser = Watir::Browser.new :firefox
10
- end
11
-
12
- def mobile_browser
13
- device = Webdriver::UserAgent.driver(:browser => :firefox, :agent => :iphone)
14
- @browser = Watir::Browser.new device
15
- end
@@ -1,12 +0,0 @@
1
- require "./lib/mobilify"
2
-
3
- class KhanAcademy
4
- include Mobilify
5
-
6
- page_url "http://www.khanacademy.org"
7
-
8
- link(:login, :class => "log-in-link ellipsis highlight")
9
- link(:mobile_login, :class => "simple-button big-button")
10
- link(:ignored_login, :class => "simple-button primary big-button")
11
- end
12
-