|
| 1 | +import {async, TestBed, ComponentFixture} from '@angular/core/testing'; |
| 2 | +import {MaterialModule, MdSidenav} from '@angular/material'; |
| 3 | +import {RouterTestingModule} from '@angular/router/testing'; |
| 4 | +import {Observable} from 'rxjs/Observable'; |
| 5 | +import {Router} from '@angular/router'; |
| 6 | +import {CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA} from '@angular/core'; |
| 7 | + |
| 8 | +import {DocumentationItems} from '../../shared/documentation-items/documentation-items'; |
| 9 | +import {ComponentSidenav} from './component-sidenav'; |
| 10 | + |
| 11 | +const mockRouter = { |
| 12 | + events: Observable.create(observer => { |
| 13 | + observer.next(null); |
| 14 | + observer.complete(); |
| 15 | + }) |
| 16 | +}; |
| 17 | + |
| 18 | + |
| 19 | +describe('ComponentSidenav', () => { |
| 20 | + let fixture: ComponentFixture<ComponentSidenav>; |
| 21 | + |
| 22 | + beforeEach(async(() => { |
| 23 | + TestBed.configureTestingModule({ |
| 24 | + imports: [MaterialModule], |
| 25 | + schemas: [NO_ERRORS_SCHEMA], |
| 26 | + declarations: [ComponentSidenav], |
| 27 | + providers: [ |
| 28 | + {provide: Router, useValue: mockRouter}, |
| 29 | + DocumentationItems |
| 30 | + ], |
| 31 | + }); |
| 32 | + |
| 33 | + fixture = TestBed.createComponent(ComponentSidenav); |
| 34 | + })); |
| 35 | + |
| 36 | + it('should close the sidenav on init', () => { |
| 37 | + const component = fixture.componentInstance; |
| 38 | + |
| 39 | + // Spy on window.mediaMatch and return stub |
| 40 | + spyOn(window, 'matchMedia').and.returnValue({ |
| 41 | + matches: true |
| 42 | + }); |
| 43 | + |
| 44 | + // Spy on viewChild component's `close` method |
| 45 | + spyOn(component.sidenav, 'close'); |
| 46 | + |
| 47 | + fixture.detectChanges(); |
| 48 | + |
| 49 | + expect(component.sidenav instanceof MdSidenav).toBeTruthy(); |
| 50 | + expect(component.isScreenSmall()).toBeTruthy(); |
| 51 | + expect(component.sidenav.close).toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should show a link for each item in doc items categories', () => { |
| 55 | + const component = fixture.componentInstance; |
| 56 | + |
| 57 | + fixture.detectChanges(); |
| 58 | + |
| 59 | + const totalItems = component.docItems.getAllItems().length; |
| 60 | + const totalLinks = fixture |
| 61 | + .nativeElement |
| 62 | + .querySelectorAll('.docs-component-viewer-sidenav li a') |
| 63 | + .length; |
| 64 | + expect(totalLinks).toEqual(totalItems); |
| 65 | + }); |
| 66 | +}); |
0 commit comments