|
| 1 | +import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {MatButtonHarness} from '@angular/material/button/testing'; |
| 4 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 5 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 6 | + from '@angular/platform-browser-dynamic/testing'; |
| 7 | +import {MatButtonModule} from '@angular/material/button'; |
| 8 | +import {ButtonHarnessExample} from './button-harness-example'; |
| 9 | + |
| 10 | +describe('ButtonHarnessExample', () => { |
| 11 | + let fixture: ComponentFixture<ButtonHarnessExample>; |
| 12 | + let loader: HarnessLoader; |
| 13 | + let buttonHarness = MatButtonHarness; |
| 14 | + |
| 15 | + beforeAll(() => { |
| 16 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 17 | + }); |
| 18 | + |
| 19 | + beforeEach( |
| 20 | + waitForAsync(() => { |
| 21 | + TestBed.configureTestingModule({ |
| 22 | + imports: [MatButtonModule], |
| 23 | + declarations: [ButtonHarnessExample] |
| 24 | + }).compileComponents(); |
| 25 | + fixture = TestBed.createComponent(ButtonHarnessExample); |
| 26 | + fixture.detectChanges(); |
| 27 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 28 | + }) |
| 29 | + ); |
| 30 | + |
| 31 | + it('should load all button harnesses', async () => { |
| 32 | + const buttons = await loader.getAllHarnesses(MatButtonHarness); |
| 33 | + expect(buttons.length).toBe(1); |
| 34 | + } |
| 35 | + ); |
| 36 | + |
| 37 | + it('should load button with exact text', async () => { |
| 38 | + const buttons = await loader.getAllHarnesses(buttonHarness.with({text: 'Basic button'})); |
| 39 | + expect(buttons.length).toBe(1); |
| 40 | + expect(await buttons[0].getText()).toBe('Basic button'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should click a button', async () => { |
| 44 | + const button = await loader.getHarness(buttonHarness.with({text: 'Basic button'})); |
| 45 | + await button.click(); |
| 46 | + expect(fixture.componentInstance.clicked).toBe(true); |
| 47 | + }); |
| 48 | +}); |
0 commit comments