|
| 1 | +import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {MatCheckboxHarness} from '@angular/material/checkbox/testing'; |
| 4 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 5 | +import {ReactiveFormsModule} from '@angular/forms'; |
| 6 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 7 | + from '@angular/platform-browser-dynamic/testing'; |
| 8 | +import {MatCheckboxModule} from '@angular/material/checkbox'; |
| 9 | +import {CheckboxHarnessExample} from './checkbox-harness-example'; |
| 10 | + |
| 11 | +describe('CheckboxHarnessExample', () => { |
| 12 | + let fixture: ComponentFixture<CheckboxHarnessExample>; |
| 13 | + let loader: HarnessLoader; |
| 14 | + |
| 15 | + beforeAll(() => { |
| 16 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 17 | + }); |
| 18 | + |
| 19 | + beforeEach( |
| 20 | + waitForAsync(() => { |
| 21 | + TestBed.configureTestingModule({ |
| 22 | + imports: [MatCheckboxModule, ReactiveFormsModule], |
| 23 | + declarations: [CheckboxHarnessExample] |
| 24 | + }).compileComponents(); |
| 25 | + })); |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + fixture = TestBed.createComponent(CheckboxHarnessExample); |
| 29 | + fixture.detectChanges(); |
| 30 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should load checkbox with name', async () => { |
| 34 | + const checkboxes = await loader.getAllHarnesses(MatCheckboxHarness.with({name: 'first-name'})); |
| 35 | + expect(checkboxes.length).toBe(1); |
| 36 | + expect(await checkboxes[0].getLabelText()).toBe('First'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should get checked state', async () => { |
| 40 | + const [checkedCheckbox, uncheckedCheckbox] = await loader.getAllHarnesses(MatCheckboxHarness); |
| 41 | + expect(await checkedCheckbox.isChecked()).toBe(true); |
| 42 | + expect(await uncheckedCheckbox.isChecked()).toBe(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should get name', async () => { |
| 46 | + const checkbox = await loader.getHarness(MatCheckboxHarness.with({label: 'First'})); |
| 47 | + expect(await checkbox.getName()).toBe('first-name'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should get label text', async () => { |
| 51 | + const [firstCheckbox, secondCheckbox] = await loader.getAllHarnesses(MatCheckboxHarness); |
| 52 | + expect(await firstCheckbox.getLabelText()).toBe('First'); |
| 53 | + expect(await secondCheckbox.getLabelText()).toBe('Second'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should toggle checkbox', async () => { |
| 57 | + fixture.componentInstance.disabled = false; |
| 58 | + const [checkedCheckbox, uncheckedCheckbox] = await loader.getAllHarnesses(MatCheckboxHarness); |
| 59 | + await checkedCheckbox.toggle(); |
| 60 | + await uncheckedCheckbox.toggle(); |
| 61 | + expect(await checkedCheckbox.isChecked()).toBe(false); |
| 62 | + expect(await uncheckedCheckbox.isChecked()).toBe(true); |
| 63 | + }); |
| 64 | +}); |
0 commit comments