-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(material-experimental/checkbox): Add test harnesses for both versions of mat-checkbox #16334
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 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4a61a64
feat(material-experimental/checkbox): Add test harnesses for both
mmalerba 1131d8c
add tests
mmalerba a8a20a1
doc strings
mmalerba df04538
fix BUILD setup
mmalerba 41fcad2
re-enable checkbox before toggle test
mmalerba 974f046
add mdc theme to tests
mmalerba df5a44d
fix lint
mmalerba be72816
address feedback
mmalerba 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
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
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
191 changes: 191 additions & 0 deletions
191
src/material-experimental/mdc-checkbox/harness/checkbox-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,191 @@ | ||
import {HarnessLoader} from '@angular/cdk-experimental/testing'; | ||
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed'; | ||
import {Component} from '@angular/core'; | ||
import {ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {FormControl, ReactiveFormsModule} from '@angular/forms'; | ||
import {MatCheckboxModule} from '@angular/material/checkbox'; | ||
import {MatCheckboxModule as MatMdcCheckboxModule} from '../index'; | ||
import {MatCheckboxHarness} from './checkbox-harness'; | ||
import {MatCheckboxHarness as MatMdcCheckboxHarness} from './mdc-checkbox-harness'; | ||
|
||
let fixture: ComponentFixture<unknown>; | ||
let loader: HarnessLoader; | ||
let checkboxHarness: typeof MatCheckboxHarness; | ||
|
||
describe('MatCheckboxHarness', () => { | ||
describe('non-MDC-based', () => { | ||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [MatCheckboxModule, ReactiveFormsModule], | ||
declarations: [CheckboxHarnessTest], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(CheckboxHarnessTest); | ||
fixture.detectChanges(); | ||
loader = TestbedHarnessEnvironment.loader(fixture); | ||
checkboxHarness = MatCheckboxHarness; | ||
}); | ||
|
||
runTests(); | ||
}); | ||
|
||
describe('MDC-based', () => { | ||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [MatMdcCheckboxModule, ReactiveFormsModule], | ||
declarations: [CheckboxHarnessTest], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(CheckboxHarnessTest); | ||
fixture.detectChanges(); | ||
loader = TestbedHarnessEnvironment.loader(fixture); | ||
// Public APIs are the same as MatCheckboxHarness, but cast is necessary because of different | ||
// private fields. | ||
checkboxHarness = MatMdcCheckboxHarness as any; | ||
mmalerba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
runTests(); | ||
}); | ||
}); | ||
|
||
/** Shared tests to run on both the original and MDC-based checkboxes. */ | ||
function runTests() { | ||
it('should load all checkbox harnesses', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
expect(checkboxes.length).toBe(2); | ||
}); | ||
|
||
it('should load checkbox with exact label', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness.with({label: 'First'})); | ||
expect(checkboxes.length).toBe(1); | ||
expect(await checkboxes[0].getLabelText()).toBe('First'); | ||
mmalerba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it('should load checkbox with regex label match', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness.with({label: /^s/i})); | ||
expect(checkboxes.length).toBe(1); | ||
expect(await checkboxes[0].getLabelText()).toBe('Second'); | ||
}); | ||
|
||
it('should get checked state', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
expect(await checkboxes[0].isChecked()).toBe(true); | ||
expect(await checkboxes[1].isChecked()).toBe(false); | ||
}); | ||
|
||
it('should get indeterminate state', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
expect(await checkboxes[0].isIndeterminate()).toBe(false); | ||
expect(await checkboxes[1].isIndeterminate()).toBe(true); | ||
}); | ||
|
||
it('should get disabled state', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
expect(await checkboxes[0].isDisabled()).toBe(false); | ||
expect(await checkboxes[1].isDisabled()).toBe(true); | ||
}); | ||
|
||
it('should get required state', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
expect(await checkboxes[0].isRequired()).toBe(true); | ||
expect(await checkboxes[1].isRequired()).toBe(false); | ||
}); | ||
|
||
it('should get valid state', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
expect(await checkboxes[0].isValid()).toBe(true); | ||
expect(await checkboxes[1].isValid()).toBe(true); | ||
await checkboxes[0].uncheck(); | ||
expect(await checkboxes[0].isValid()).toBe(false); | ||
}); | ||
|
||
it('should get name', async () => { | ||
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); | ||
expect(await checkbox.getName()).toBe('first-name'); | ||
}); | ||
|
||
it('should get value', async () => { | ||
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); | ||
expect(await checkbox.getValue()).toBe('first-value'); | ||
}); | ||
|
||
it('should get aria-label', async () => { | ||
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); | ||
expect(await checkbox.getAriaLabel()).toBe('First checkbox'); | ||
}); | ||
|
||
it('should get aria-labelledby', async () => { | ||
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'Second'})); | ||
expect(await checkbox.getAriaLabelledby()).toBe('second-label'); | ||
}); | ||
|
||
it('should get label text', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
expect(await checkboxes[0].getLabelText()).toBe('First'); | ||
expect(await checkboxes[1].getLabelText()).toBe('Second'); | ||
}); | ||
|
||
it('should focus checkbox', async () => { | ||
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); | ||
expect(getActiveElementTagName()).not.toBe('input'); | ||
await checkbox.foucs(); | ||
expect(getActiveElementTagName()).toBe('input'); | ||
}); | ||
|
||
it('should blur checkbox', async () => { | ||
const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); | ||
await checkbox.foucs(); | ||
expect(getActiveElementTagName()).toBe('input'); | ||
await checkbox.blur(); | ||
expect(getActiveElementTagName()).not.toBe('input'); | ||
}); | ||
|
||
it('should toggle checkbox', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
await checkboxes[0].toggle(); | ||
await checkboxes[1].toggle(); | ||
expect(await checkboxes[0].isChecked()).toBe(false); | ||
expect(await checkboxes[1].isChecked()).toBe(true); | ||
}); | ||
|
||
it('should check checkbox', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
await checkboxes[0].check(); | ||
await checkboxes[1].check(); | ||
expect(await checkboxes[0].isChecked()).toBe(true); | ||
expect(await checkboxes[1].isChecked()).toBe(true); | ||
}); | ||
|
||
it('should uncheck checkbox', async () => { | ||
const checkboxes = await loader.getAllHarnesses(checkboxHarness); | ||
await checkboxes[0].uncheck(); | ||
await checkboxes[1].uncheck(); | ||
expect(await checkboxes[0].isChecked()).toBe(false); | ||
expect(await checkboxes[1].isChecked()).toBe(false); | ||
}); | ||
} | ||
|
||
function getActiveElementTagName() { | ||
return document.activeElement ? document.activeElement.tagName.toLowerCase() : ''; | ||
} | ||
|
||
@Component({ | ||
template: ` | ||
<mat-checkbox | ||
[formControl]="ctrl" | ||
required | ||
name="first-name" | ||
value="first-value" | ||
aria-label="First checkbox"> | ||
First | ||
</mat-checkbox> | ||
<mat-checkbox indeterminate="true" disabled="true" aria-labelledby="second-label"> | ||
Second | ||
</mat-checkbox> | ||
<span id="second-label">Second checkbox</span> | ||
` | ||
}) | ||
class CheckboxHarnessTest { | ||
ctrl = new FormControl(true); | ||
} | ||
|
Oops, something went wrong.
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.