|
| 1 | +import {HarnessLoader} from '@angular/cdk-experimental/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed'; |
| 3 | +import {Component} from '@angular/core'; |
| 4 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {MatSidenavModule} from '@angular/material/sidenav'; |
| 6 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 7 | +import {MatSidenavModule as MatMdcSidenavModule} from '../index'; |
| 8 | +import {MatSidenavHarness} from './sidenav-harness'; |
| 9 | +import {MatSidenavHarness as MatMdcSidenavHarness} from './mdc-sidenav-harness'; |
| 10 | + |
| 11 | +let fixture: ComponentFixture<SidenavHarnessTest>; |
| 12 | +let loader: HarnessLoader; |
| 13 | +let harness: typeof MatSidenavHarness; |
| 14 | + |
| 15 | +describe('MatSidenavHarness', () => { |
| 16 | + describe('non-MDC-based', () => { |
| 17 | + beforeEach(async () => { |
| 18 | + await TestBed.configureTestingModule({ |
| 19 | + imports: [MatSidenavModule, NoopAnimationsModule], |
| 20 | + declarations: [SidenavHarnessTest], |
| 21 | + }).compileComponents(); |
| 22 | + |
| 23 | + fixture = TestBed.createComponent(SidenavHarnessTest); |
| 24 | + fixture.detectChanges(); |
| 25 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 26 | + harness = MatSidenavHarness; |
| 27 | + }); |
| 28 | + |
| 29 | + runTests(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('MDC-based', () => { |
| 33 | + beforeEach(async () => { |
| 34 | + await TestBed.configureTestingModule({ |
| 35 | + imports: [MatMdcSidenavModule, NoopAnimationsModule], |
| 36 | + declarations: [SidenavHarnessTest], |
| 37 | + }).compileComponents(); |
| 38 | + |
| 39 | + fixture = TestBed.createComponent(SidenavHarnessTest); |
| 40 | + fixture.detectChanges(); |
| 41 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 42 | + // Public APIs are the same as MatSidenavHarness, but cast |
| 43 | + // is necessary because of different private fields. |
| 44 | + harness = MatMdcSidenavHarness as any; |
| 45 | + }); |
| 46 | + |
| 47 | + // TODO: enable after MDC sidenav is implemented |
| 48 | + // runTests(); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +/** Shared tests to run on both the original and MDC-based sidenav. */ |
| 53 | +function runTests() { |
| 54 | + it('should load all sidenav harnesses', async () => { |
| 55 | + const sidenavs = await loader.getAllHarnesses(harness); |
| 56 | + expect(sidenavs.length).toBe(3); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should be able to get whether the sidenav is open', async () => { |
| 60 | + const sidenavs = await loader.getAllHarnesses(harness); |
| 61 | + |
| 62 | + expect(await sidenavs[0].isOpen()).toBe(false); |
| 63 | + expect(await sidenavs[1].isOpen()).toBe(false); |
| 64 | + expect(await sidenavs[2].isOpen()).toBe(true); |
| 65 | + |
| 66 | + fixture.componentInstance.threeOpened = false; |
| 67 | + fixture.detectChanges(); |
| 68 | + |
| 69 | + expect(await sidenavs[0].isOpen()).toBe(false); |
| 70 | + expect(await sidenavs[1].isOpen()).toBe(false); |
| 71 | + expect(await sidenavs[2].isOpen()).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should be able to get the position of a sidenav', async () => { |
| 75 | + const sidenavs = await loader.getAllHarnesses(harness); |
| 76 | + |
| 77 | + expect(await sidenavs[0].getPosition()).toBe('start'); |
| 78 | + expect(await sidenavs[1].getPosition()).toBe('end'); |
| 79 | + expect(await sidenavs[2].getPosition()).toBe('start'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should be able to get the mode of a sidenav', async () => { |
| 83 | + const sidenavs = await loader.getAllHarnesses(harness); |
| 84 | + |
| 85 | + expect(await sidenavs[0].getMode()).toBe('over'); |
| 86 | + expect(await sidenavs[1].getMode()).toBe('side'); |
| 87 | + expect(await sidenavs[2].getMode()).toBe('push'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should be able to get whether a sidenav is fixed in the viewport', async () => { |
| 91 | + const sidenavs = await loader.getAllHarnesses(harness); |
| 92 | + |
| 93 | + expect(await sidenavs[0].isFixedInViewport()).toBe(false); |
| 94 | + expect(await sidenavs[1].isFixedInViewport()).toBe(false); |
| 95 | + expect(await sidenavs[2].isFixedInViewport()).toBe(true); |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +@Component({ |
| 100 | + template: ` |
| 101 | + <mat-sidenav-container> |
| 102 | + <mat-sidenav id="one" [opened]="oneOpened" position="start">One</mat-sidenav> |
| 103 | + <mat-sidenav id="two" mode="side" position="end">Two</mat-sidenav> |
| 104 | + <mat-sidenav-content>Content</mat-sidenav-content> |
| 105 | + </mat-sidenav-container> |
| 106 | +
|
| 107 | + <mat-sidenav-container> |
| 108 | + <mat-sidenav id="three" mode="push" [opened]="threeOpened" fixedInViewport>Three</mat-sidenav> |
| 109 | + <mat-sidenav-content>Content</mat-sidenav-content> |
| 110 | + </mat-sidenav-container> |
| 111 | + ` |
| 112 | +}) |
| 113 | +class SidenavHarnessTest { |
| 114 | + threeOpened = true; |
| 115 | +} |
| 116 | + |
0 commit comments