|
| 1 | +import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 3 | +import {MatFormFieldHarness} from '@angular/material/form-field/testing'; |
| 4 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 5 | +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} |
| 6 | + from '@angular/platform-browser-dynamic/testing'; |
| 7 | +import {MatFormFieldModule} from '@angular/material/form-field'; |
| 8 | +import {FormFieldHarnessExample} from './form-field-harness-example'; |
| 9 | +import {MatInputModule} from '@angular/material/input'; |
| 10 | +import {ReactiveFormsModule} from '@angular/forms'; |
| 11 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 12 | +import {MatInputHarness} from '@angular/material/input/testing'; |
| 13 | + |
| 14 | +describe('FormFieldHarnessExample', () => { |
| 15 | + let fixture: ComponentFixture<FormFieldHarnessExample>; |
| 16 | + let loader: HarnessLoader; |
| 17 | + |
| 18 | + beforeAll(() => { |
| 19 | + TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); |
| 20 | + }); |
| 21 | + |
| 22 | + beforeEach( |
| 23 | + waitForAsync(() => { |
| 24 | + TestBed.configureTestingModule({ |
| 25 | + imports: [MatFormFieldModule, MatInputModule, ReactiveFormsModule, NoopAnimationsModule], |
| 26 | + declarations: [FormFieldHarnessExample] |
| 27 | + }).compileComponents(); |
| 28 | + fixture = TestBed.createComponent(FormFieldHarnessExample); |
| 29 | + fixture.detectChanges(); |
| 30 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 31 | + }) |
| 32 | + ); |
| 33 | + |
| 34 | + it('should be able to load harnesses', async () => { |
| 35 | + const formFields = await loader.getAllHarnesses(MatFormFieldHarness); |
| 36 | + expect(formFields.length).toBe(1); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should be able to get control of form-field', async () => { |
| 40 | + const formField = await loader.getHarness(MatFormFieldHarness); |
| 41 | + expect(await formField.getControl() instanceof MatInputHarness).toBe(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should be able to get error messages and hints of form-field', async () => { |
| 45 | + const formField = await loader.getHarness(MatFormFieldHarness); |
| 46 | + expect(await formField.getTextErrors()).toEqual([]); |
| 47 | + expect(await formField.getTextHints()).toEqual(['Hint']); |
| 48 | + |
| 49 | + fixture.componentInstance.requiredControl.setValue(''); |
| 50 | + (await formField.getControl())?.blur(); |
| 51 | + expect(await formField.getTextErrors()).toEqual(['Error 1']); |
| 52 | + expect(await formField.getTextHints()).toEqual([]); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should be able to check if form field is invalid', async () => { |
| 56 | + const formField = await loader.getHarness(MatFormFieldHarness); |
| 57 | + expect(await formField.isControlValid()).toBe(true); |
| 58 | + |
| 59 | + fixture.componentInstance.requiredControl.setValue(''); |
| 60 | + expect(await formField.isControlValid()).toBe(false); |
| 61 | + }); |
| 62 | +}); |
0 commit comments