forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.component.spec.ts
856 lines (698 loc) · 29.6 KB
/
app.component.spec.ts
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
import { NO_ERRORS_SCHEMA, DebugElement } from '@angular/core';
import { async, inject, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { Title } from '@angular/platform-browser';
import { APP_BASE_HREF } from '@angular/common';
import { Http } from '@angular/http';
import { MdProgressBar } from '@angular/material';
import { By } from '@angular/platform-browser';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { of } from 'rxjs/observable/of';
import { AppComponent } from './app.component';
import { AppModule } from './app.module';
import { DocViewerComponent } from 'app/layout/doc-viewer/doc-viewer.component';
import { GaService } from 'app/shared/ga.service';
import { LocationService } from 'app/shared/location.service';
import { Logger } from 'app/shared/logger.service';
import { MockLocationService } from 'testing/location.service';
import { MockLogger } from 'testing/logger.service';
import { MockSearchService } from 'testing/search.service';
import { MockSwUpdateNotificationsService } from 'testing/sw-update-notifications.service';
import { NavigationNode } from 'app/navigation/navigation.service';
import { ScrollService } from 'app/shared/scroll.service';
import { SearchBoxComponent } from 'app/search/search-box/search-box.component';
import { SearchResultsComponent } from 'app/search/search-results/search-results.component';
import { SearchService } from 'app/search/search.service';
import { SwUpdateNotificationsService } from 'app/sw-updates/sw-update-notifications.service';
import { TocComponent } from 'app/embedded/toc/toc.component';
import { MdSidenav } from '@angular/material';
const sideBySideBreakPoint = 992;
const hideToCBreakPoint = 800;
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let docViewer: HTMLElement;
let hamburger: HTMLButtonElement;
let locationService: MockLocationService;
let sidenav: HTMLElement;
const initializeTest = () => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.onResize(sideBySideBreakPoint + 1); // wide by default
docViewer = fixture.debugElement.query(By.css('aio-doc-viewer')).nativeElement;
hamburger = fixture.debugElement.query(By.css('.hamburger')).nativeElement;
locationService = fixture.debugElement.injector.get(LocationService) as any;
sidenav = fixture.debugElement.query(By.css('md-sidenav')).nativeElement;
};
describe('with proper DocViewer', () => {
beforeEach(() => {
createTestingModule('a/b');
initializeTest();
});
it('should create', () => {
expect(component).toBeDefined();
});
describe('ServiceWorker update notifications', () => {
it('should be enabled', () => {
const swUpdateNotifications = TestBed.get(SwUpdateNotificationsService) as SwUpdateNotificationsService;
expect(swUpdateNotifications.enable).toHaveBeenCalled();
});
});
describe('onResize', () => {
it('should update `isSideBySide` accordingly', () => {
component.onResize(sideBySideBreakPoint + 1);
expect(component.isSideBySide).toBe(true);
component.onResize(sideBySideBreakPoint - 1);
expect(component.isSideBySide).toBe(false);
});
it('should update `showFloatingToc` accordingly', () => {
component.onResize(hideToCBreakPoint + 1);
expect(component.showFloatingToc).toBe(true);
component.onResize(hideToCBreakPoint - 1);
expect(component.showFloatingToc).toBe(false);
});
});
describe('onScroll', () => {
it('should update `tocMaxHeight` accordingly', () => {
expect(component.tocMaxHeight).toBeUndefined();
component.onScroll();
expect(component.tocMaxHeight).toBeGreaterThan(0);
});
});
describe('SideNav when side-by-side (wide)', () => {
beforeEach(() => {
component.onResize(sideBySideBreakPoint + 1); // side-by-side
});
it('should open when nav to a guide page (guide/pipes)', () => {
locationService.go('guide/pipes');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-open/);
});
it('should open when nav to an api page', () => {
locationService.go('api/a/b/c/d');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-open/);
});
it('should be closed when nav to a marketing page (features)', () => {
locationService.go('features');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
describe('when manually closed', () => {
beforeEach(() => {
locationService.go('guide/pipes');
fixture.detectChanges();
hamburger.click();
fixture.detectChanges();
});
it('should be closed', () => {
expect(sidenav.className).toMatch(/sidenav-clos/);
});
it('should stay closed when nav from one guide page to another', () => {
locationService.go('guide/bags');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
it('should stay closed when nav from a guide page to api page', () => {
locationService.go('api');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
it('should reopen when nav to market page and back to guide page', () => {
locationService.go('features');
fixture.detectChanges();
locationService.go('guide/bags');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-open/);
});
});
});
describe('SideNav when NOT side-by-side (narrow)', () => {
beforeEach(() => {
component.onResize(sideBySideBreakPoint - 1); // NOT side-by-side
});
it('should be closed when nav to a guide page (guide/pipes)', () => {
locationService.go('guide/pipes');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
it('should be closed when nav to an api page', () => {
locationService.go('api/a/b/c/d');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
it('should be closed when nav to a marketing page (features)', () => {
locationService.go('features');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
describe('when manually opened', () => {
beforeEach(() => {
locationService.go('guide/pipes');
fixture.detectChanges();
hamburger.click();
fixture.detectChanges();
});
it('should be open', () => {
expect(sidenav.className).toMatch(/sidenav-open/);
});
it('should close when click in gray content area overlay', () => {
const sidenavBackdrop = fixture.debugElement.query(By.css('.mat-sidenav-backdrop')).nativeElement;
sidenavBackdrop.click();
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
it('should close when nav to another guide page', () => {
locationService.go('guide/bags');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
it('should close when nav to api page', () => {
locationService.go('api');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
it('should close again when nav to market page', () => {
locationService.go('features');
fixture.detectChanges();
expect(sidenav.className).toMatch(/sidenav-clos/);
});
});
});
describe('SideNav version selector', () => {
beforeEach(() => {
component.onResize(sideBySideBreakPoint + 1); // side-by-side
});
it('should pick first (current) version by default', () => {
const versionSelector = sidenav.querySelector('select');
expect(versionSelector.value).toEqual(component.versionInfo.raw);
expect(versionSelector.selectedIndex).toEqual(0);
});
// Older docs versions have an href
it('should navigate when change to a version with an href', () => {
component.onDocVersionChange(1);
expect(locationService.go).toHaveBeenCalledWith(TestHttp.docVersions[0].url);
});
// The current docs version should not have an href
// This may change when we perfect our docs versioning approach
it('should not navigate when change to a version without an href', () => {
component.onDocVersionChange(0);
expect(locationService.go).not.toHaveBeenCalled();
});
});
describe('pageId', () => {
it('should set the id of the doc viewer container based on the current doc', () => {
const container = fixture.debugElement.query(By.css('section.sidenav-content'));
locationService.go('guide/pipes');
fixture.detectChanges();
expect(component.pageId).toEqual('guide-pipes');
expect(container.properties['id']).toEqual('guide-pipes');
locationService.go('news');
fixture.detectChanges();
expect(component.pageId).toEqual('news');
expect(container.properties['id']).toEqual('news');
locationService.go('');
fixture.detectChanges();
expect(component.pageId).toEqual('home');
expect(container.properties['id']).toEqual('home');
});
it('should not be affected by changes to the query', () => {
const container = fixture.debugElement.query(By.css('section.sidenav-content'));
locationService.go('guide/pipes');
fixture.detectChanges();
locationService.go('guide/other?search=http');
fixture.detectChanges();
expect(component.pageId).toEqual('guide-other');
expect(container.properties['id']).toEqual('guide-other');
});
});
describe('hostClasses', () => {
let host: DebugElement;
beforeEach(() => {
host = fixture.debugElement;
});
it('should set the css classes of the host container based on the current doc and navigation view', () => {
locationService.go('guide/pipes');
fixture.detectChanges();
checkHostClass('page', 'guide-pipes');
checkHostClass('folder', 'guide');
checkHostClass('view', 'SideNav');
locationService.go('features');
fixture.detectChanges();
checkHostClass('page', 'features');
checkHostClass('folder', 'features');
checkHostClass('view', 'TopBar');
locationService.go('');
fixture.detectChanges();
checkHostClass('page', 'home');
checkHostClass('folder', 'home');
checkHostClass('view', '');
});
it('should set the css class of the host container based on the open/closed state of the side nav', () => {
const sideNav = host.query(By.directive(MdSidenav));
locationService.go('guide/pipes');
fixture.detectChanges();
checkHostClass('sidenav', 'open');
sideNav.componentInstance.opened = false;
sideNav.triggerEventHandler('close', {});
fixture.detectChanges();
checkHostClass('sidenav', 'closed');
sideNav.componentInstance.opened = true;
sideNav.triggerEventHandler('open', {});
fixture.detectChanges();
checkHostClass('sidenav', 'open');
});
function checkHostClass(type, value) {
const classes = host.properties['className'];
const classArray = classes.split(' ').filter(c => c.indexOf(`${type}-`) === 0);
expect(classArray.length).toBeLessThanOrEqual(1, `"${classes}" should have only one class matching ${type}-*`);
expect(classArray).toEqual([`${type}-${value}`], `"${classes}" should contain ${type}-${value}`);
}
});
describe('currentDocument', () => {
it('should display a guide page (guide/pipes)', () => {
locationService.go('guide/pipes');
fixture.detectChanges();
expect(docViewer.innerText).toMatch(/Pipes/i);
});
it('should display the api page', () => {
locationService.go('api');
fixture.detectChanges();
expect(docViewer.innerText).toMatch(/API/i);
});
it('should display a marketing page', () => {
locationService.go('features');
fixture.detectChanges();
expect(docViewer.innerText).toMatch(/Features/i);
});
it('should update the document title', () => {
const titleService = TestBed.get(Title);
spyOn(titleService, 'setTitle');
locationService.go('guide/pipes');
fixture.detectChanges();
expect(titleService.setTitle).toHaveBeenCalledWith('Angular - Pipes');
});
it('should update the document title, with a default value if the document has no title', () => {
const titleService = TestBed.get(Title);
spyOn(titleService, 'setTitle');
locationService.go('no-title');
fixture.detectChanges();
expect(titleService.setTitle).toHaveBeenCalledWith('Angular');
});
});
describe('auto-scrolling', () => {
const scrollDelay = 500;
let scrollService: ScrollService;
let scrollSpy: jasmine.Spy;
beforeEach(() => {
scrollService = fixture.debugElement.injector.get(ScrollService);
scrollSpy = spyOn(scrollService, 'scroll');
});
it('should not scroll immediately when the docId (path) changes', () => {
locationService.go('guide/pipes');
// deliberately not calling `fixture.detectChanges` because don't want `onDocRendered`
expect(scrollSpy).not.toHaveBeenCalled();
});
it('should scroll when just the hash changes (# alone)', () => {
locationService.go('guide/pipes');
locationService.go('guide/pipes#somewhere');
expect(scrollSpy).toHaveBeenCalled();
});
it('should scroll when just the hash changes (/#)', () => {
locationService.go('guide/pipes');
locationService.go('guide/pipes/#somewhere');
expect(scrollSpy).toHaveBeenCalled();
});
it('should scroll again when nav to the same hash twice in succession', () => {
locationService.go('guide/pipes');
locationService.go('guide/pipes#somewhere');
locationService.go('guide/pipes#somewhere');
expect(scrollSpy.calls.count()).toBe(2);
});
it('should scroll when nav to the same path', () => {
locationService.go('guide/pipes');
scrollSpy.calls.reset();
locationService.go('guide/pipes');
expect(scrollSpy).toHaveBeenCalledTimes(1);
});
it('should scroll when e-nav to the empty path', () => {
locationService.go('');
scrollSpy.calls.reset();
locationService.go('');
expect(scrollSpy).toHaveBeenCalledTimes(1);
});
it('should scroll after a delay when call onDocRendered directly', fakeAsync(() => {
component.onDocRendered();
expect(scrollSpy).not.toHaveBeenCalled();
tick(scrollDelay);
expect(scrollSpy).toHaveBeenCalled();
}));
it('should scroll (via onDocRendered) when finish navigating to a new doc', fakeAsync(() => {
locationService.go('guide/pipes');
fixture.detectChanges(); // triggers the event that calls onDocRendered
expect(scrollSpy).not.toHaveBeenCalled();
tick(scrollDelay);
expect(scrollSpy).toHaveBeenCalled();
}));
});
describe('click intercepting', () => {
it('should intercept clicks on anchors and call `location.handleAnchorClick()`',
inject([LocationService], (location: LocationService) => {
const el = fixture.nativeElement as Element;
el.innerHTML = '<a href="some/local/url">click me</a>';
const anchorElement = el.getElementsByTagName('a')[0];
anchorElement.click();
expect(location.handleAnchorClick).toHaveBeenCalledWith(anchorElement, 0, false, false);
}));
it('should intercept clicks on elements deep within an anchor tag',
inject([LocationService], (location: LocationService) => {
const el = fixture.nativeElement as Element;
el.innerHTML = '<a href="some/local/url"><div><img></div></a>';
const imageElement = el.getElementsByTagName('img')[0];
const anchorElement = el.getElementsByTagName('a')[0];
imageElement.click();
expect(location.handleAnchorClick).toHaveBeenCalledWith(anchorElement, 0, false, false);
}));
it('should ignore clicks on elements without an anchor ancestor',
inject([LocationService], (location: LocationService) => {
const el = fixture.nativeElement as Element;
el.innerHTML = '<div><p><div><img></div></p></div>';
const imageElement = el.getElementsByTagName('img')[0];
imageElement.click();
expect(location.handleAnchorClick).not.toHaveBeenCalled();
}));
});
describe('aio-toc', () => {
let tocDebugElement: DebugElement;
let tocContainer: DebugElement;
beforeEach(() => {
tocDebugElement = fixture.debugElement.query(By.directive(TocComponent));
tocContainer = tocDebugElement.parent;
});
it('should have a non-embedded `<aio-toc>` element', () => {
expect(tocDebugElement).toBeDefined();
expect(tocDebugElement.classes['embedded']).toBeFalsy();
});
it('should update the TOC container\'s `maxHeight` based on `tocMaxHeight`', () => {
expect(tocContainer.styles['max-height']).toBeNull();
component.tocMaxHeight = '100';
fixture.detectChanges();
expect(tocContainer.styles['max-height']).toBe('100px');
});
});
describe('footer', () => {
it('should have version number', () => {
const versionEl: HTMLElement = fixture.debugElement.query(By.css('aio-footer')).nativeElement;
expect(versionEl.innerText).toContain(TestHttp.versionFull);
});
});
describe('search', () => {
describe('initialization', () => {
it('should initialize the search worker', inject([SearchService], (searchService: SearchService) => {
fixture.detectChanges(); // triggers ngOnInit
expect(searchService.initWorker).toHaveBeenCalled();
expect(searchService.loadIndex).toHaveBeenCalled();
}));
});
describe('click handling', () => {
it('should intercept clicks not on the search elements and hide the search results', () => {
component.showSearchResults = true;
fixture.detectChanges();
// docViewer is a commonly-clicked, non-search element
docViewer.click();
expect(component.showSearchResults).toBe(false);
});
it('should not intercept clicks on the searchResults', () => {
component.showSearchResults = true;
fixture.detectChanges();
const searchResults = fixture.debugElement.query(By.directive(SearchResultsComponent));
searchResults.nativeElement.click();
fixture.detectChanges();
expect(component.showSearchResults).toBe(true);
});
it('should not intercept clicks om the searchBox', () => {
component.showSearchResults = true;
fixture.detectChanges();
const searchBox = fixture.debugElement.query(By.directive(SearchBoxComponent));
searchBox.nativeElement.click();
fixture.detectChanges();
expect(component.showSearchResults).toBe(true);
});
});
describe('keyup handling', () => {
it('should grab focus when the / key is pressed', () => {
const searchBox: SearchBoxComponent = fixture.debugElement.query(By.directive(SearchBoxComponent)).componentInstance;
spyOn(searchBox, 'focus');
window.document.dispatchEvent(new KeyboardEvent('keyup', { 'key': '/' }));
fixture.detectChanges();
expect(searchBox.focus).toHaveBeenCalled();
});
it('should set focus back to the search box when the search results are displayed and the escape key is pressed', () => {
const searchBox: SearchBoxComponent = fixture.debugElement.query(By.directive(SearchBoxComponent)).componentInstance;
spyOn(searchBox, 'focus');
component.showSearchResults = true;
window.document.dispatchEvent(new KeyboardEvent('keyup', { 'key': 'Escape' }));
fixture.detectChanges();
expect(searchBox.focus).toHaveBeenCalled();
});
});
describe('showing search results', () => {
it('should not display search results when query is empty', () => {
const searchService: MockSearchService = TestBed.get(SearchService);
searchService.searchResults.next({ query: '', results: [] });
fixture.detectChanges();
expect(component.showSearchResults).toBe(false);
});
it('should hide the results when a search result is selected', () => {
const searchService: MockSearchService = TestBed.get(SearchService);
const results = [
{ path: 'news', title: 'News', type: 'marketing', keywords: '', titleWords: '' }
];
searchService.searchResults.next({ query: 'something', results: results });
component.showSearchResults = true;
fixture.detectChanges();
const searchResultsComponent = fixture.debugElement.query(By.directive(SearchResultsComponent));
searchResultsComponent.triggerEventHandler('resultSelected', {});
fixture.detectChanges();
expect(component.showSearchResults).toBe(false);
});
});
});
});
describe('with mocked DocViewer', () => {
const getDocViewer = () => fixture.debugElement.query(By.css('aio-doc-viewer'));
const triggerDocRendered = () => getDocViewer().triggerEventHandler('docRendered', {});
beforeEach(() => {
createTestingModule('a/b');
// Remove the DocViewer for this test and hide the missing component message
TestBed.overrideModule(AppModule, {
remove: { declarations: [DocViewerComponent] },
add: { schemas: [NO_ERRORS_SCHEMA] }
});
});
describe('initial rendering', () => {
it('should initially add the starting class until the first document is rendered', fakeAsync(() => {
const getSidenavContainer = () => fixture.debugElement.query(By.css('md-sidenav-container'));
initializeTest();
expect(component.isStarting).toBe(true);
expect(getSidenavContainer().classes['starting']).toBe(true);
triggerDocRendered();
fixture.detectChanges();
expect(component.isStarting).toBe(true);
expect(getSidenavContainer().classes['starting']).toBe(true);
tick(499);
fixture.detectChanges();
expect(component.isStarting).toBe(true);
expect(getSidenavContainer().classes['starting']).toBe(true);
tick(2);
fixture.detectChanges();
expect(component.isStarting).toBe(false);
expect(getSidenavContainer().classes['starting']).toBe(false);
}));
});
describe('progress bar', () => {
const SHOW_DELAY = 200;
const HIDE_DELAY = 500;
const getProgressBar = () => fixture.debugElement.query(By.directive(MdProgressBar));
const initializeAndCompleteNavigation = () => {
initializeTest();
triggerDocRendered();
tick(HIDE_DELAY);
};
it('should initially be hidden', () => {
initializeTest();
expect(getProgressBar()).toBeFalsy();
});
it('should be shown (after a delay) when the path changes', fakeAsync(() => {
initializeAndCompleteNavigation();
locationService.urlSubject.next('c/d');
fixture.detectChanges();
expect(getProgressBar()).toBeFalsy();
tick(SHOW_DELAY - 1);
fixture.detectChanges();
expect(getProgressBar()).toBeFalsy();
tick(1);
fixture.detectChanges();
expect(getProgressBar()).toBeTruthy();
}));
it('should not be shown when the URL changes but the path remains the same', fakeAsync(() => {
initializeAndCompleteNavigation();
locationService.urlSubject.next('a/b');
tick(SHOW_DELAY);
fixture.detectChanges();
expect(getProgressBar()).toBeFalsy();
}));
it('should not be shown when re-navigating to the empty path', fakeAsync(() => {
initializeAndCompleteNavigation();
locationService.urlSubject.next('');
triggerDocRendered();
locationService.urlSubject.next('');
tick(SHOW_DELAY);
fixture.detectChanges();
expect(getProgressBar()).toBeFalsy();
tick(HIDE_DELAY); // Fire the remaining timer or `fakeAsync()` complains.
}));
it('should not be shown if the doc is rendered quickly', fakeAsync(() => {
initializeAndCompleteNavigation();
locationService.urlSubject.next('c/d');
tick(SHOW_DELAY - 1);
triggerDocRendered();
tick(1);
fixture.detectChanges();
expect(getProgressBar()).toBeFalsy();
tick(HIDE_DELAY); // Fire the remaining timer or `fakeAsync()` complains.
}));
it('should be shown if rendering the doc takes too long', fakeAsync(() => {
initializeAndCompleteNavigation();
locationService.urlSubject.next('c/d');
tick(SHOW_DELAY);
triggerDocRendered();
fixture.detectChanges();
expect(getProgressBar()).toBeTruthy();
tick(HIDE_DELAY); // Fire the remaining timer or `fakeAsync()` complains.
}));
it('should be hidden (after a delay) once the doc is rendered', fakeAsync(() => {
initializeAndCompleteNavigation();
locationService.urlSubject.next('c/d');
tick(SHOW_DELAY);
triggerDocRendered();
fixture.detectChanges();
expect(getProgressBar()).toBeTruthy();
tick(HIDE_DELAY - 1);
fixture.detectChanges();
expect(getProgressBar()).toBeTruthy();
tick(1);
fixture.detectChanges();
expect(getProgressBar()).toBeFalsy();
}));
it('should only take the latest request into account', fakeAsync(() => {
initializeAndCompleteNavigation();
locationService.urlSubject.next('c/d'); // The URL changes.
locationService.urlSubject.next('e/f'); // The URL changes again before `onDocRendered()`.
tick(SHOW_DELAY - 1); // `onDocRendered()` is triggered (for the last doc),
triggerDocRendered(); // before the progress bar is shown.
tick(1);
fixture.detectChanges();
expect(getProgressBar()).toBeFalsy();
tick(HIDE_DELAY); // Fire the remaining timer or `fakeAsync()` complains.
}));
});
});
});
//// test helpers ////
function createTestingModule(initialUrl: string) {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [ AppModule ],
providers: [
{ provide: APP_BASE_HREF, useValue: '/' },
{ provide: GaService, useClass: TestGaService },
{ provide: Http, useClass: TestHttp },
{ provide: LocationService, useFactory: () => new MockLocationService(initialUrl) },
{ provide: Logger, useClass: MockLogger },
{ provide: SearchService, useClass: MockSearchService },
{ provide: SwUpdateNotificationsService, useClass: MockSwUpdateNotificationsService },
]
});
}
class TestGaService {
locationChanged = jasmine.createSpy('locationChanged');
}
class TestSearchService {
initWorker = jasmine.createSpy('initWorker');
loadIndex = jasmine.createSpy('loadIndex');
}
class TestHttp {
static versionFull = '4.0.0-local+sha.73808dd';
static docVersions: NavigationNode[] = [
{ title: 'v2', url: 'https://v2.angular.io' }
];
// tslint:disable:quotemark
navJson = {
"TopBar": [
{
"url": "features",
"title": "Features"
},
{
"url": "no-title",
"title": "No Title"
},
],
"SideNav": [
{
"title": "Core",
"tooltip": "Learn the core capabilities of Angular",
"children": [
{
"url": "guide/pipes",
"title": "Pipes",
"tooltip": "Pipes transform displayed values within a template."
},
{
"url": "guide/bags",
"title": "Bags",
"tooltip": "Pack your bags for a code adventure."
}
]
},
{
"url": "api",
"title": "API",
"tooltip": "Details of the Angular classes and values."
}
],
"docVersions": TestHttp.docVersions,
"__versionInfo": {
"raw": "4.0.0-rc.6",
"major": 4,
"minor": 0,
"patch": 0,
"prerelease": [
"local"
],
"build": "sha.73808dd",
"version": "4.0.0-local",
"codeName": "snapshot",
"isSnapshot": true,
"full": TestHttp.versionFull,
"branch": "master",
"commitSHA": "73808dd38b5ccd729404936834d1568bd066de81"
}
};
get(url: string) {
let data;
if (/navigation\.json/.test(url)) {
data = this.navJson;
} else {
const match = /generated\/docs\/(.+)\.json/.exec(url);
const id = match[1];
// Make up a title for test purposes
const title = id.split('/').pop().replace(/^([a-z])/, (_, letter) => letter.toUpperCase());
const h1 = (id === 'no-title') ? '' : `<h1>${title}</h1>`;
const contents = `${h1}<h2 id="#somewhere">Some heading</h2>`;
data = { id, contents };
}
return of({ json: () => data });
}
}