forked from emberjs/guides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc_spec.rb
697 lines (585 loc) · 22.7 KB
/
toc_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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
require "spec_helper"
require "hashie"
describe TOC::Helpers do
let(:helper) { HelperTester.new }
let(:basic_chapter_title) { "What even is middleman?" }
let(:basic_guide_title) { "Middleman Basics" }
let(:basics_page) { double(path: "middleman-basics/index.html") }
before(:each) do
class HelperTester
include TOC::Helpers
end
data_yml = %Q{
guides:
- title: "Middleman Basics"
url: "middleman-basics"
chapters:
- title: "What even is middleman?"
url: "index"
}
data = Hashie::Mash.new(YAML.load(data_yml))
testing_page = double(path: "custom-extensions/testing-custom-extensions.html")
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(testing_page)
allow(helper).to receive(:link_to).and_wrap_original do |_, title, url|
%Q{<a href="#{url}">#{title}</a>}
end
end
after do
# Remove our testing class
Object.send :remove_const, :HelperTester
end
describe "#toc_for" do
before(:each) do
building_page = double(path: "custom-extensions/building-custom-extensions.html")
allow(helper).to receive(:request).and_return(building_page)
allow(File).to receive(:exist?).and_return(true)
end
it "raises an exception if a file doesn't exist" do
allow(File).to receive(:exist?).and_return(false)
expect {
helper.toc_for(helper.data.guides)
}.to raise_error(RuntimeError, "source/middleman-basics/index.md does not exist. Please fix guides.yml.")
end
it "includes guide titles except guides that are marked to skip sidbar" do
data_yml = %Q{
guides:
- title: "Middleman Basics"
url: "middleman-basics"
chapters:
- title: "What even is middleman?"
url: ""
skip_sidebar: true
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Advanced Concepts"
url: "index"
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
}
data = Hashie::Mash.new(YAML.load(data_yml))
toc = helper.toc_for(data.guides)
expect(toc).to include("Advanced Middleman")
expect(toc).to include("Extending Middleman")
expect(toc).not_to include("Middleman Basics")
end
it "includes chapter titles except for chapter that are marked to skip sidbar" do
data_yml = %Q{
guides:
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
skip_sidebar_item: true
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
- title: "Testing Middleman Extensions"
url: "testing-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
toc = helper.toc_for(data.guides)
expect(toc).to include("Building Middleman Extensions")
expect(toc).to include("Testing Middleman Extensions")
expect(toc).not_to include("What are extensions?")
end
it "includes guide urls except guides that are marked to skip sidbar" do
data_yml = %Q{
guides:
- title: "Middleman Basics"
url: "middleman-basics"
chapters:
- title: "What even is middleman?"
url: ""
skip_sidebar: true
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Advanced Concepts"
url: "index"
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
}
data = Hashie::Mash.new(YAML.load(data_yml))
toc = helper.toc_for(data.guides)
expect(toc).to include("advanced-middleman")
expect(toc).to include("extending-middleman")
expect(toc).not_to include("middleman-basics")
end
it "includes chapter urls except for chapter that are marked to skip sidbar" do
data_yml = %Q{
guides:
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Advanced Concepts"
url: "index"
- title: "Middleman Architecture"
url: "middleman-architecture"
skip_sidebar_item: true
- title: "Contributing to Middleman"
url: "contributing-to-middleman"
}
data = Hashie::Mash.new(YAML.load(data_yml))
toc = helper.toc_for(data.guides)
advanced_index_page = double(path: "advanced-middleman/index.html")
allow(helper).to receive(:request).and_return(advanced_index_page)
expect(toc).to include(advanced_index_page.path)
expect(toc).to include("contributing-to-middleman")
expect(toc).not_to include("middleman-architecture")
end
it "contains a link to first chapter as a guide link even if it is marked with :skip_sidebar_item" do
data_yml = %Q{
guides:
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
skip_sidebar_item: true
}
data = Hashie::Mash.new(YAML.load(data_yml))
toc = helper.toc_for(data.guides)
expectation = %Q{<li class='level-1 '><a href=\"/extending-middleman/index.html\">Extending Middleman</a>}
expect(toc).to include(expectation)
end
end
describe "#page_title" do
before(:each) do
allow(helper).to receive(:current_page).and_return(basics_page)
end
it "is generic when current guide is not specified" do
allow(helper).to receive(:current_guide).and_return(nil)
expect(helper.page_title).to eq("Guides")
end
it "is current guide's title when current chapter is not specified" do
allow(helper).to receive(:current_chapter).and_return(nil)
expect(helper.page_title).to eq("Middleman Basics")
end
it "is a combination of current guide & chapter titles when both are specified" do
expect(helper.page_title).to eq("#{basic_guide_title}: #{basic_chapter_title}")
end
end
describe "#guide_name" do
before(:each) do
allow(helper).to receive(:current_page).and_return(basics_page)
end
it "is nil if current guide is not specified" do
allow(helper).to receive(:current_guide).and_return(nil)
expect(helper.guide_name).to be_nil
end
it "is the current guide's title when current guide is specified" do
expect(helper.guide_name).to eq(basic_guide_title)
end
end
describe "#chapter_name" do
before(:each) do
allow(helper).to receive(:current_page).and_return(basics_page)
end
it "is an empty string when current chapter is not specified" do
allow(helper).to receive(:current_chapter).and_return(nil)
expect(helper.chapter_name).to eq("")
end
it "is current chapter's title when current chapter is specified" do
expect(helper.chapter_name).to eq(basic_chapter_title)
end
end
describe "#chapter_heading" do
before(:each) do
allow(helper).to receive(:current_page).and_return(basics_page)
end
it "is nil if chapter name is blank" do
allow(helper).to receive(:chapter_name).and_return("")
expect(helper.chapter_heading).to be_nil
end
it "is header markup with name & source URL when chapter name is specified" do
expect(helper.chapter_heading).to include(basic_chapter_title)
expect(helper.chapter_heading).to include("https://github.com/emberjs/guides/edit")
end
end
describe "#chapter_github_source_url" do
it "is the github URL to the source file for current page" do
expect(helper.chapter_github_source_url).to eq("https://github.com/emberjs/guides/edit/master/source/custom-extensions/testing-custom-extensions.md")
end
end
describe "#current_guide" do
before(:each) do
allow(helper).to receive(:current_page).and_return(basics_page)
end
it "is the current value if present" do
helper.instance_variable_set(:@current_guide, "some random value")
expect(helper.current_guide).to eq("some random value")
end
it "sets the guide associated with the current page as current guide & returns it" do
guide = helper.data.guides.first
expect(helper.instance_variable_get(:@current_guide)).to be_nil
expect(helper.current_guide).to eq(guide)
expect(helper.instance_variable_get(:@current_guide)).to eq(guide)
end
end
describe "#current_guide_index" do
before(:each) do
data_yml = %Q{
guides:
- title: "Middleman Basics"
url: "middleman-basics"
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Advanced Concepts"
url: "index"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(basics_page)
end
it "is nil if current guide is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.current_guide_index).to be_nil
end
it "is the index of the current guide when specified" do
allow(helper).to receive(:current_page).and_return(double(path: "advanced-middleman/index.html"))
expect(helper.current_guide_index).to eq(1)
end
end
describe "#current_chapter" do
before(:each) do
data_yml = %Q{
guides:
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Advanced Concepts"
url: "index"
- title: "Middleman Architecture"
url: "middleman-architecture"
}
@data = Hashie::Mash.new(YAML.load(data_yml))
end
it "is nil if current guide is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.current_chapter).to be_nil
end
it "is the current value if present" do
allow(helper).to receive(:data).and_return(@data)
allow(helper).to receive(:current_page).and_return(double(path: "advanced-middleman/middleman-architecture.html"))
architecture_chapter = helper.data.guides.first.chapters.last
helper.instance_variable_set(:@current_chapter, architecture_chapter)
expect(helper.current_chapter).to eq(architecture_chapter)
end
it "sets the chapter associated with the current page as current chapter & returns it" do
allow(helper).to receive(:data).and_return(@data)
allow(helper).to receive(:current_page).and_return(double(path: "advanced-middleman/middleman-architecture.html"))
architecture_chapter = helper.data.guides.first.chapters.last
expect(helper.instance_variable_get(:@current_chapter)).to be_nil
expect(helper.current_chapter).to eq(architecture_chapter)
expect(helper.instance_variable_get(:@current_chapter)).to eq(architecture_chapter)
end
end
describe "#current_chapter_index" do
it "is nil if current guide is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.current_chapter_index).to be_nil
end
it "is nil if the current chapter is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.current_chapter_index).to be_nil
end
it "is the index of the current chapter in the current guide if both are specified" do
data_yml = %Q{
guides:
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Advanced Concepts"
url: "index"
- title: "Middleman Architecture"
url: "middleman-architecture"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "advanced-middleman/middleman-architecture.html"))
expect(helper.current_chapter_index).to eq(1)
end
end
describe "#chapter_links" do
it "is markup that consists of previous & next chapter links" do
data_yml = %Q{
guides:
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
skip_sidebar_item: true
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
- title: "Testing Middleman Extensions"
url: "testing-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "extending-middleman/building-middleman-extensions"))
expect(helper.chapter_links).to include("What are extensions?")
expect(helper.chapter_links).to include("Testing Middleman Extensions")
end
end
describe "#previous_chapter_link" do
it "is link to previous chapter in current guide if previous chapter is specified" do
data_yml = %Q{
guides:
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "extending-middleman/building-middleman-extensions"))
expect(helper.previous_chapter_link).to include("What are extensions?")
expect(helper.previous_chapter_link).to include("index")
end
it "is link to last chapter in previous guide if current chapter is first chapter in previous guide" do
data_yml = %Q{
guides:
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Middleman Architecture"
url: "middleman-architecture"
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "extending-middleman/building-middleman-extensions"))
expect(helper.previous_chapter_link).to include("Middleman Architecture")
end
end
describe "#next_chapter_link" do
it "is link to next chapter in current guide if next chapter is specified" do
data_yml = %Q{
guides:
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "extending-middleman/index"))
expect(helper.next_chapter_link).to include("Building Middleman Extensions")
end
it "is link to the first chapter in next guide if next chapter is not specified" do
data_yml = %Q{
guides:
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Middleman Architecture"
url: "middleman-architecture"
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "advanced-middleman/middleman-architecture"))
expectation = "We're done with Advanced Middleman. Next up: Extending Middleman - Building Middleman Extensions"
expect(helper.next_chapter_link).to include(expectation)
end
end
describe "#previous_chapter" do
it "is nil if current guide is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.previous_chapter).to be_nil
end
it "is nil if current chapter index is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.previous_chapter).to be_nil
end
it "is nil if current chapter is the first chapter in the guide" do
data_yml = %Q{
guides:
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Middleman Architecture"
url: "middleman-architecture"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "advanced-middleman/middleman-architecture"))
expect(helper.previous_chapter).to be_nil
end
it "is the previous chapter when current guide & current chapter are specified & there is a chapter before that" do
data_yml = %Q{
guides:
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "extending-middleman/building-middleman-extensions"))
extensions_chapter = helper.data.guides.first.chapters.first
expect(helper.previous_chapter).to eq(extensions_chapter)
end
end
describe "#next_chapter" do
it "is nil if current guide is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.next_chapter).to be_nil
end
it "is nil if current chapter index is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.next_chapter).to be_nil
end
it "is nil if current chapter is the last chapter in the guide" do
data_yml = %Q{
guides:
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Middleman Architecture"
url: "middleman-architecture"
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "advanced-middleman/middleman-architecture"))
expect(helper.next_chapter).to be_nil
end
it "is the next chapter when current guide has a next chapter" do
data_yml = %Q{
guides:
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "extending-middleman/index"))
building_chapter = helper.data.guides.first.chapters.last
expect(helper.next_chapter).to eq(building_chapter)
end
end
describe "#previous_guide" do
it "is nil if current guide is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.previous_guide).to be_nil
end
it "is nil if current guide index is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.previous_guide).to be_nil
end
it "is nil if current guide is the first guide" do
data_yml = %Q{
guides:
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "extending-middleman/index"))
expect(helper.previous_guide).to be_nil
end
it "is the previous guide when current guide is specified & there is a guide before that" do
data_yml = %Q{
guides:
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Middleman Architecture"
url: "middleman-architecture"
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "extending-middleman/building-middleman-extensions"))
first_guide = helper.data.guides.first
expect(helper.previous_guide).to eq(first_guide)
end
end
describe "#next_guide" do
it "is nil if current guide is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.next_guide).to be_nil
end
it "is nil if current guide index is not specified" do
allow(helper).to receive(:current_page).and_return(double(path: "some/nonexistent/path.html"))
expect(helper.next_guide).to be_nil
end
it "is nil if current guide is the last guide" do
data_yml = %Q{
guides:
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "What are extensions?"
url: "index"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "extending-middleman/index"))
expect(helper.next_guide).to be_nil
end
it "is the next guide when current guide is specified & next guide exists" do
data_yml = %Q{
guides:
- title: "Advanced Middleman"
url: "advanced-middleman"
chapters:
- title: "Middleman Architecture"
url: "middleman-architecture"
- title: "Extending Middleman"
url: "extending-middleman"
chapters:
- title: "Building Middleman Extensions"
url: "building-middleman-extensions"
}
data = Hashie::Mash.new(YAML.load(data_yml))
allow(helper).to receive(:data).and_return(data)
allow(helper).to receive(:current_page).and_return(double(path: "advanced-middleman/middleman-architecture"))
next_guide = helper.data.guides.last
expect(helper.next_guide).to eq(next_guide)
end
end
end