-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(material-experimental/drawer): add harness for mat-drawer (#17010)
* feat(material-experimental): add harness for mat-drawer Follow-up from #16695. Adds a separate harness for `mat-drawer` and has `mat-sidenav` inherit from it. * fix bad rebase
- Loading branch information
Showing
8 changed files
with
199 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/material-experimental/mdc-sidenav/harness/drawer-harness.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import {HarnessLoader} from '@angular/cdk/testing'; | ||
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; | ||
import {Component} from '@angular/core'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {MatSidenavModule} from '@angular/material/sidenav'; | ||
import {NoopAnimationsModule} from '@angular/platform-browser/animations'; | ||
import {MatSidenavModule as MatMdcSidenavModule} from '../index'; | ||
import {MatDrawerHarness} from './drawer-harness'; | ||
import {MatDrawerHarness as MatMdcDrawerHarness} from './mdc-drawer-harness'; | ||
|
||
let fixture: ComponentFixture<DrawerHarnessTest>; | ||
let loader: HarnessLoader; | ||
let harness: typeof MatDrawerHarness; | ||
|
||
describe('MatDrawerHarness', () => { | ||
describe('non-MDC-based', () => { | ||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [MatSidenavModule, NoopAnimationsModule], | ||
declarations: [DrawerHarnessTest], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(DrawerHarnessTest); | ||
fixture.detectChanges(); | ||
loader = TestbedHarnessEnvironment.loader(fixture); | ||
harness = MatDrawerHarness; | ||
}); | ||
|
||
runTests(); | ||
}); | ||
|
||
describe('MDC-based', () => { | ||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [MatMdcSidenavModule, NoopAnimationsModule], | ||
declarations: [DrawerHarnessTest], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(DrawerHarnessTest); | ||
fixture.detectChanges(); | ||
loader = TestbedHarnessEnvironment.loader(fixture); | ||
// Public APIs are the same as MatDrawerHarness, but cast | ||
// is necessary because of different private fields. | ||
harness = MatMdcDrawerHarness as any; | ||
}); | ||
|
||
// TODO: enable after MDC drawer is implemented | ||
// runTests(); | ||
}); | ||
}); | ||
|
||
/** Shared tests to run on both the original and MDC-based drawer. */ | ||
function runTests() { | ||
it('should load all drawer harnesses', async () => { | ||
const drawers = await loader.getAllHarnesses(harness); | ||
expect(drawers.length).toBe(3); | ||
}); | ||
|
||
it('should be able to get whether the drawer is open', async () => { | ||
const drawers = await loader.getAllHarnesses(harness); | ||
|
||
expect(await drawers[0].isOpen()).toBe(false); | ||
expect(await drawers[1].isOpen()).toBe(false); | ||
expect(await drawers[2].isOpen()).toBe(true); | ||
|
||
fixture.componentInstance.threeOpened = false; | ||
fixture.detectChanges(); | ||
|
||
expect(await drawers[0].isOpen()).toBe(false); | ||
expect(await drawers[1].isOpen()).toBe(false); | ||
expect(await drawers[2].isOpen()).toBe(false); | ||
}); | ||
|
||
it('should be able to get the position of a drawer', async () => { | ||
const drawers = await loader.getAllHarnesses(harness); | ||
|
||
expect(await drawers[0].getPosition()).toBe('start'); | ||
expect(await drawers[1].getPosition()).toBe('end'); | ||
expect(await drawers[2].getPosition()).toBe('start'); | ||
}); | ||
|
||
it('should be able to get the mode of a drawer', async () => { | ||
const drawers = await loader.getAllHarnesses(harness); | ||
|
||
expect(await drawers[0].getMode()).toBe('over'); | ||
expect(await drawers[1].getMode()).toBe('side'); | ||
expect(await drawers[2].getMode()).toBe('push'); | ||
}); | ||
} | ||
|
||
@Component({ | ||
template: ` | ||
<mat-drawer-container> | ||
<mat-drawer id="one" [opened]="oneOpened" position="start">One</mat-drawer> | ||
<mat-drawer id="two" mode="side" position="end">Two</mat-drawer> | ||
<mat-drawer-content>Content</mat-drawer-content> | ||
</mat-drawer-container> | ||
<mat-drawer-container> | ||
<mat-drawer id="three" mode="push" [opened]="threeOpened">Three</mat-drawer> | ||
<mat-drawer-content>Content</mat-drawer-content> | ||
</mat-drawer-container> | ||
` | ||
}) | ||
class DrawerHarnessTest { | ||
threeOpened = true; | ||
} | ||
|
54 changes: 54 additions & 0 deletions
54
src/material-experimental/mdc-sidenav/harness/drawer-harness.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing'; | ||
import {DrawerHarnessFilters} from './drawer-harness-filters'; | ||
|
||
/** | ||
* Harness for interacting with a standard mat-drawer in tests. | ||
* @dynamic | ||
*/ | ||
export class MatDrawerHarness extends ComponentHarness { | ||
static hostSelector = '.mat-drawer'; | ||
|
||
/** | ||
* Gets a `HarnessPredicate` that can be used to search for a drawer with | ||
* specific attributes. | ||
* @param options Options for narrowing the search. | ||
* @return `HarnessPredicate` configured with the given options. | ||
*/ | ||
static with(options: DrawerHarnessFilters = {}): HarnessPredicate<MatDrawerHarness> { | ||
return new HarnessPredicate(MatDrawerHarness, options); | ||
} | ||
|
||
/** Gets whether the drawer is open. */ | ||
async isOpen(): Promise<boolean> { | ||
return (await this.host()).hasClass('mat-drawer-opened'); | ||
} | ||
|
||
/** Gets the position of the drawer inside its container. */ | ||
async getPosition(): Promise<'start'|'end'> { | ||
const host = await this.host(); | ||
return (await host.hasClass('mat-drawer-end')) ? 'end' : 'start'; | ||
} | ||
|
||
/** Gets the mode that the drawer is in. */ | ||
async getMode(): Promise<'over'|'push'|'side'> { | ||
const host = await this.host(); | ||
|
||
if (await host.hasClass('mat-drawer-push')) { | ||
return 'push'; | ||
} | ||
|
||
if (await host.hasClass('mat-drawer-side')) { | ||
return 'side'; | ||
} | ||
|
||
return 'over'; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/material-experimental/mdc-sidenav/harness/mdc-drawer-harness.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ComponentHarness} from '@angular/cdk/testing'; | ||
|
||
|
||
/** | ||
* Harness for interacting with a MDC-based drawer in tests. | ||
* @dynamic | ||
*/ | ||
export class MatDrawerHarness extends ComponentHarness { | ||
// TODO: implement once MDC drawer is done. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters