-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(material-experimental/drawer): add harness for mat-drawer #17010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.