forked from nakajima/elementor
/
elementor_spec.rb
266 lines (217 loc) · 8.06 KB
/
elementor_spec.rb
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
require File.dirname(__FILE__) + '/spec_helper'
HTML = Nokogiri::HTML::Builder.new {
html {
head { title("This is the title") }
body {
h1("A header")
div(:class => "tag-cloud", :rel => "other") {
a(:href => '#foo', :class => 'tag even') { text "Foo" }
a(:href => '#bar', :class => 'tag') { text "Bar" }
}
div(:id => "user-links") {
h1("User Link Section")
div(:class => "tag-cloud") {
a(:href => '#fizz', :class => 'tag even') { text "Fizz" }
a(:href => '#buzz', :class => 'tag') { text "Buzz" }
}
}
}
}
}.doc.to_html
describe Elementor do
include Elementor
attr_reader :body, :result
before(:each) do
@body = HTML.dup
end
describe "the html" do
it "has tags" do
Nokogiri(body).search('.tag-cloud a').should have(4).nodes
end
it "has a headers" do
Nokogiri(body).search('h1').should have(2).nodes
end
end
describe "the DSL" do
context "without elements defined" do
it "can not find tags" do
proc {
@result.should have(4).tags
}.should raise_error
end
end
context "with elements defined" do
before(:each) do
@result = elements(:from => :body) do |tag|
tag.headers "h1"
tag.tag_clouds ".tag-cloud"
tag.tags "a.tag"
tag.user_links "#user-links"
end
end
it "can find tags" do
result.should have(2).headers
result.should have(4).tags
end
it "can still use old Nokogiri traversal methods" do
result.search('.tag-cloud a').should have(4).nodes
result.search('h1').should have(2).nodes
end
describe "chaining selectors" do
it "can chain selector aliases" do
result.user_links.headers.should have(1).node
result.user_links.tags.should have(2).nodes
end
it "works with with_text filter" do
result.user_links.tags.with_text('Fizz').should have(1).node
end
it "works with with_attrs filter" do
result.user_links.tags.with_attrs(:class => /even/).should have(1).node
end
describe "as an rspec matcher" do
it "works with no matches" do
result.tags.should have(0).user_links
end
it "works with 1 match" do
result.user_links.should have(1).headers
end
it "works with many matches" do
result.user_links.should have(2).tags
end
it "allows chaining" do
result.user_links.should have(1).tags.with_attrs(:class => /even/).with_text('Fizz')
end
end
end
describe ":from option" do
it "determines which method should be called to get the markup" do
meta_eval { alias_method :other_body, :body }
@result = elements(:from => :other_body) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.user_links "#user-links"
end
result.should have(2).headers
result.should have(4).tags
end
it "works when the HTML isn't present until after the #elements call" do
mock(self).deferred_source.once.returns(HTML.dup)
@result = elements(:from => :deferred_source) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.bodies "body"
end
result.should have(2).headers
result.should have(4).tags
end
end
describe "filters" do
describe "#with_text" do
it "limits results by text" do
result.tags.with_text("Foo").should have(1).node
end
it "limits results by regex" do
result.tags.with_text(/foo|bar/i).should have(2).nodes
end
it "allows chaining with other filters" do
result.tags.with_text('zz').with_attrs(:class => /even/).should have(1).node
end
it "allows chaining with selector aliases" do
result.tag_clouds.with_text('Fizz').tags.should have(2).nodes
end
describe "as an rspec matcher" do
it "works with no matches" do
result.should have(0).tags.with_text("Wee")
end
it "works with 1 match" do
result.should have(1).tags.with_text("Foo")
end
it "works with many matches" do
result.should have(2).tags.with_text("zz")
end
it "allows chaining" do
result.should have(1).tags.with_text("zz").with_attrs(:class => /even/)
end
end
end
describe "#with_attrs" do
context "using string values" do
it "limits results by one attribute" do
result.tags.with_attrs(:href => '#foo').should have(1).node
result.tags.with_attrs(:class => "tag even").should have(2).nodes
end
it "limits results by multiple attributes" do
result.tags.with_attrs(:class => /even/, :href => '#foo').should have(1).node
end
end
context "using regex values" do
it "limits results by one attribute" do
result.tags.with_attrs(:href => /#(foo|bar)/).should have(2).node
end
it "limits results by multiple attributes" do
result.tags.with_attrs(:class => /even/, :href => /#(foo|bar)/).should have(1).node
end
end
it "allows chaining with other filters" do
result.tags.with_attrs(:class => /even/).with_text('Fizz').should have(1).node
end
it "allows chaining with selector aliases" do
result.tag_clouds.with_attrs(:rel => 'other').tags.should have(2).nodes
end
describe "as an rspec matcher" do
it "works with no matches" do
result.should have(0).tags.with_attrs(:href => '#Wee')
end
it "works with 1 match" do
result.should have(1).tags.with_attrs(:href => '#foo')
end
it "works with many matches" do
result.should have(2).tags.with_attrs(:class => /even/)
end
it "allows chaining" do
result.should have(1).tags.with_attrs(:class => /even/).with_text('Fizz')
end
end
end
end
describe "error handling" do
context "before methods have been called on result object" do
it "swallows errors" do
view = Class.new { def render; send(:foo!) end }.new
meta_def(:whoops!) { view.render }
proc {
@result = elements(:from => :whoops!) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.user_links "#user-links"
end
@result.instance_variable_get("@this").doc
}.should_not raise_error
end
end
context "after methods have been called on result object" do
it "re-raises errors that occur when :from blows up" do
view = Class.new { def render; send(:foo!) end }.new
meta_def(:whoops!) { view.render }
proc {
@result = elements(:from => :whoops!) do |tag|
tag.headers "h1"
tag.tags ".tag-cloud a"
tag.user_links "#user-links"
end
result.should have(0).headers
}.should raise_error(NoMethodError)
end
end
end
describe "#parse!" do
it "parses new markup" do
result.should have(4).tags
result.parse!(%(<h1>Fred</h1>))
result.should have(0).tags
result.should have(1).headers.with_text("Fred")
end
end
end
end
end