-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
menu_spec.rb
472 lines (374 loc) · 15.4 KB
/
menu_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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# frozen_string_literal: true
require "rails_helper"
# rubocop:disable Metrics/LineLength
RSpec.describe Navigator::Menu do
let(:erb_handler) { ActionView::Template::Handlers::ERB.new }
let(:template) { ActionView::Template.new "<html></html>", "Example", erb_handler, {} }
let(:menu) { Navigator::Menu.new template }
describe "#add" do
it "adds single tag" do
menu.add "li", "one"
expect(menu.render).to eq("<ul><li>one</li></ul>")
end
it "adds single tag with multiple data attributes" do
menu.add "li", "one", attributes: {data: {one: 1, two: 2}}
expect(menu.render).to eq(%(<ul><li data-one="1" data-two="2">one</li></ul>))
end
it "adds multiple tags" do
menu.add "li", "one"
menu.add "li", "two"
menu.add "li", "three"
expect(menu.render).to eq("<ul><li>one</li><li>two</li><li>three</li></ul>")
end
it "adds nested tags" do
menu.add "li" do
add "ul" do
add "li", "sub"
end
end
expect(menu.render).to eq("<ul><li><ul><li>sub</li></ul></li></ul>")
end
it "adds tag with nested link" do
menu.add "li" do
link "One", "/one"
end
expect(menu.render).to eq(%(<ul><li><a href="/one">One</a></li></ul>))
end
it "adds tag with nested item" do
menu.add "section" do
item "One", "/one"
end
expect(menu.render).to eq(%(<ul><section><li><a href="/one">One</a></li></section></ul>))
end
it "adds tag using default menu tag activator" do
path = "/home"
activator = Navigator::TagActivator.new search_value: path
menu = Navigator::Menu.new template, tag: "nav", activator: activator
menu.add "a", "Home", attributes: {href: path}
menu.add "a", "About", attributes: {href: "/about"}
expect(menu.render).to eq(%(<nav><a href="#{path}" class="active">Home</a><a href="/about">About</a></nav>))
end
it "adds tag using custom item tag activator" do
activator = Navigator::TagActivator.new search_value: "/about"
menu = Navigator::Menu.new template, tag: "nav"
menu.add "a", "Home", attributes: {href: "/home"}
menu.add "a", "About", attributes: {href: "/about"}, activator: activator
expect(menu.render).to eq(%(<nav><a href="/home">Home</a><a href="/about" class="active">About</a></nav>))
end
end
describe "#link" do
context "with attributes" do
it "adds link" do
menu.link "Example", "/example"
expect(menu.render).to eq(%(<ul><a href="/example">Example</a></ul>))
end
it "adds link with no content" do
menu.link "/example"
expect(menu.render).to eq(%(<ul><a href="/example"></a></ul>))
end
it "adds link with attributes" do
menu.link "Example", "/example", attributes: {class: "active"}
expect(menu.render).to eq(%(<ul><a class="active" href="/example">Example</a></ul>))
end
end
context "with activators" do
it "adds link using menu tag activator" do
activator = Navigator::TagActivator.new search_value: "/examples/1"
menu = Navigator::Menu.new template, activator: activator
menu.link "Example 1", "/examples/1"
menu.link "Example 2", "/examples/2"
expect(menu.render).to eq(%(<ul><a href="/examples/1" class="active">Example 1</a><a href="/examples/2">Example 2</a></ul>))
end
it "adds link using link tag activator" do
activator = Navigator::TagActivator.new search_value: "/examples/2"
menu = Navigator::Menu.new template
menu.link "Example 1", "/examples/1"
menu.link "Example 2", "/examples/2", activator: activator
expect(menu.render).to eq(%(<ul><a href="/examples/1">Example 1</a><a href="/examples/2" class="active">Example 2</a></ul>))
end
it "adds link where link tag activator trumps menu tag activator" do
menu_activator = Navigator::TagActivator.new search_value: "/examples/1"
link_activator = Navigator::TagActivator.new search_value: "/examples/2"
menu = Navigator::Menu.new template, activator: menu_activator
menu.link "Example 1", "/examples/1"
menu.link "Example 2", "/examples/2", activator: link_activator
expect(menu.render).to eq(%(<ul><a href="/examples/1" class="active">Example 1</a><a href="/examples/2" class="active">Example 2</a></ul>))
end
context "with blocks" do
it "renders a block" do
menu.link("/example") { span "Test" }
expect(menu.render).to eq(%(<ul><a href="/example"><span>Test</span></a></ul>))
end
it "renders content with a block" do
menu.link("Example: ", "/example") { span "Test" }
expect(menu.render).to eq(%(<ul><a href="/example">Example: <span>Test</span></a></ul>))
end
end
end
end
describe "#image" do
context "with attributes" do
it "adds image" do
menu.image "/example", "Example"
expect(menu.render).to eq(%(<ul><img src="/example" alt="Example"></ul>))
end
it "adds image with no alt" do
menu.image "/example"
expect(menu.render).to eq(%(<ul><img src="/example"></ul>))
end
it "adds image with attributes" do
menu.image "/example", attributes: {class: "active"}
expect(menu.render).to eq(%(<ul><img class="active" src="/example"></ul>))
end
end
context "with activators" do
it "adds image using menu tag activator" do
activator = Navigator::TagActivator.new search_key: "src", search_value: "/examples/1"
menu = Navigator::Menu.new template, activator: activator
menu.image "/examples/1"
menu.image "/examples/2"
expect(menu.render).to eq(%(<ul><img src="/examples/1" class="active"><img src="/examples/2"></ul>))
end
it "adds image using image tag activator" do
activator = Navigator::TagActivator.new search_key: "src", search_value: "/examples/2"
menu = Navigator::Menu.new template
menu.image "/examples/1"
menu.image "/examples/2", activator: activator
expect(menu.render).to eq(%(<ul><img src="/examples/1"><img src="/examples/2" class="active"></ul>))
end
it "adds image where image tag activator trumps menu tag activator" do
menu_activator = Navigator::TagActivator.new search_key: "src", search_value: "/examples/1"
image_activator = Navigator::TagActivator.new search_key: "src", search_value: "/examples/2"
menu = Navigator::Menu.new template, activator: menu_activator
menu.image "/examples/1"
menu.image "/examples/2", activator: image_activator
expect(menu.render).to eq(%(<ul><img src="/examples/1" class="active"><img src="/examples/2" class="active"></ul>))
end
end
context "with blocks" do
it "ignores block" do
menu.image("/example") { "Nothing to see here." }
expect(menu.render).to eq(%(<ul><img src="/example"></ul>))
end
end
end
describe "#item" do
context "with attributes" do
it "adds item with no item or link attributes" do
menu.item "Dashboard", "/dashboard"
expect(menu.render).to eq(%(<ul><li><a href="/dashboard">Dashboard</a></li></ul>))
end
it "adds item with no link content" do
menu.item "/dashboard"
expect(menu.render).to eq(%(<ul><li><a href="/dashboard"></a></li></ul>))
end
it "adds item with item attributes" do
menu.item "Dashboard", "/dashboard", item_attributes: {class: "active"}
expect(menu.render).to eq(%(<ul><li class="active"><a href="/dashboard">Dashboard</a></li></ul>))
end
it "adds item with item and link attributes" do
menu.item "Dashboard", "/dashboard", item_attributes: {class: "active"}, link_attributes: {"data-enabled" => true}
expect(menu.render).to eq(%(<ul><li class="active"><a data-enabled="true" href="/dashboard">Dashboard</a></li></ul>))
end
it "adds item with multiple data attributes for item and link" do
menu.item "Test", "/test", item_attributes: {data: {one: 1, two: 2}}, link_attributes: {data: {a: "A", b: "B"}}
expect(menu.render).to eq(%(<ul><li data-one="1" data-two="2"><a href="/test" data-a="A" data-b="B">Test</a></li></ul>))
end
end
context "with activators" do
it "adds item using default menu tag activator" do
activator = Navigator::TagActivator.new search_value: "/users"
menu = Navigator::Menu.new template, activator: activator
menu.item "Dashboard", "/dashboard"
menu.item "Users", "/users"
expect(menu.render).to eq(%(<ul><li><a href="/dashboard">Dashboard</a></li><li class="active"><a href="/users">Users</a></li></ul>))
end
it "adds item with target value appended" do
activator = Navigator::TagActivator.new search_value: "/two"
menu = Navigator::Menu.new template, activator: activator
menu.item "One", "/one"
menu.item "Two", "/two", item_attributes: {class: "test"}
expect(menu.render).to eq(%(<ul><li><a href="/one">One</a></li><li class="test active"><a href="/two">Two</a></li></ul>))
end
it "adds item using custom item tag activator" do
activator = Navigator::TagActivator.new search_value: "/dashboard"
menu = Navigator::Menu.new template
menu.item "Dashboard", "/dashboard", activator: activator
menu.item "Users", "/users"
expect(menu.render).to eq(%(<ul><li class="active"><a href="/dashboard">Dashboard</a></li><li><a href="/users">Users</a></li></ul>))
end
it "adds item where item activator trumps menu tag activator" do
menu_activator = Navigator::TagActivator.new search_value: "/one"
item_activator = Navigator::TagActivator.new search_value: "/two"
menu = Navigator::Menu.new template, activator: menu_activator
menu.item "One", "/one"
menu.item "Two", "/two", activator: item_activator
menu.item "Three", "/three"
expect(menu.render).to eq(%(<ul><li class="active"><a href="/one">One</a></li><li class="active"><a href="/two">Two</a></li><li><a href="/three">Three</a></li></ul>))
end
end
context "with blocks" do
it "renders a block" do
menu.item("/test") { b "X" }
expect(menu.render).to eq(%(<ul><li><a href="/test"><b>X</b></a></li></ul>))
end
it "renders content with a block" do
menu.item("Test: ", "/test") { b "X" }
expect(menu.render).to eq(%(<ul><li><a href="/test">Test: <b>X</b></a></li></ul>))
end
end
end
describe "#respond_to?" do
it "answers true for a valid tag" do
expect(menu.respond_to?("section")).to eq(true)
end
it "answers false for an invalid tag" do
expect(menu.respond_to?("bogus")).to eq(false)
end
end
describe "#method_missing" do
it "adds a div tag" do
menu.div "div"
expect(menu.render).to eq("<ul><div>div</div></ul>")
end
it "adds a section tag" do
menu.section "section"
expect(menu.render).to eq("<ul><section>section</section></ul>")
end
it "adds a header tag" do
menu.header "header"
expect(menu.render).to eq("<ul><header>header</header></ul>")
end
context "header tags" do
it "adds an h1 tag" do
menu.h1 "header"
expect(menu.render).to eq("<ul><h1>header</h1></ul>")
end
it "adds an h2 tag" do
menu.h2 "header"
expect(menu.render).to eq("<ul><h2>header</h2></ul>")
end
it "adds an h3 tag" do
menu.h3 "header"
expect(menu.render).to eq("<ul><h3>header</h3></ul>")
end
it "adds an h4 tag" do
menu.h4 "header"
expect(menu.render).to eq("<ul><h4>header</h4></ul>")
end
it "adds an h5 tag" do
menu.h5 "header"
expect(menu.render).to eq("<ul><h5>header</h5></ul>")
end
it "adds an h6 tag" do
menu.h6 "header"
expect(menu.render).to eq("<ul><h6>header</h6></ul>")
end
it "raises no method error for h method" do
unknown_method = -> { menu.h }
expect(&unknown_method).to raise_error(NoMethodError)
end
it "raises no method error for h0 method" do
unknown_method = -> { menu.h0 }
expect(&unknown_method).to raise_error(NoMethodError)
end
it "raises no method error for h7 method" do
unknown_method = -> { menu.h7 }
expect(&unknown_method).to raise_error(NoMethodError)
end
end
it "adds nav tag" do
menu.nav
expect(menu.render).to eq("<ul><nav></nav></ul>")
end
it "adds ul tag" do
menu.ul
expect(menu.render).to eq("<ul><ul></ul></ul>")
end
it "adds li tag" do
menu.li "one"
expect(menu.render).to eq("<ul><li>one</li></ul>")
end
it "adds a tag" do
menu.a "one", attributes: {href: "http://www.example.com"}
expect(menu.render).to eq(%(<ul><a href="http://www.example.com">one</a></ul>))
end
it "adds img tag" do
url = "http://placehold.it/200x50"
menu.img attributes: {src: url}
expect(menu.render).to eq(%(<ul><img src="#{url}"></ul>))
end
it "adds b tag" do
menu.b "stylistic text"
expect(menu.render).to eq("<ul><b>stylistic text</b></ul>")
end
it "adds em tag" do
menu.em "emphasis"
expect(menu.render).to eq("<ul><em>emphasis</em></ul>")
end
it "adds s tag" do
menu.s "strike"
expect(menu.render).to eq("<ul><s>strike</s></ul>")
end
it "adds small tag" do
menu.small "small"
expect(menu.render).to eq("<ul><small>small</small></ul>")
end
it "adds span tag" do
menu.span "span"
expect(menu.render).to eq("<ul><span>span</span></ul>")
end
it "adds strong tag" do
menu.strong "bold"
expect(menu.render).to eq("<ul><strong>bold</strong></ul>")
end
it "adds sub tag" do
menu.sub "sub-text"
expect(menu.render).to eq("<ul><sub>sub-text</sub></ul>")
end
it "adds sup tag" do
menu.sup "super-text"
expect(menu.render).to eq("<ul><sup>super-text</sup></ul>")
end
it "adds form tag" do
menu.form "form"
expect(menu.render).to eq("<ul><form>form</form></ul>")
end
it "adds label tag" do
menu.label "label"
expect(menu.render).to eq("<ul><label>label</label></ul>")
end
it "adds select tag" do
menu.select
expect(menu.render).to eq("<ul><select></select></ul>")
end
it "adds option tag" do
menu.option
expect(menu.render).to eq("<ul><option></option></ul>")
end
it "adds input tag" do
menu.input "input"
expect(menu.render).to eq("<ul><input></ul>")
end
it "adds button tag" do
menu.button "button"
expect(menu.render).to eq("<ul><button>button</button></ul>")
end
it "raises no method error when calling non-existent method" do
unknown_method = -> { Navigator::Menu.new(template) { achoo content: "one" } }
expect(&unknown_method).to raise_error(NoMethodError)
end
end
describe "#render" do
it "renders an empty menu" do
expect(menu.render).to eq("<ul></ul>")
end
it "renders with a nav tag" do
expect(Navigator::Menu.new(template, tag: "nav").render).to eq("<nav></nav>")
end
it "renders one attribute with no content" do
menu = Navigator::Menu.new template, attributes: {class: "test"}
expect(menu.render).to eq(%(<ul class="test"></ul>))
end
end
end