|
| 1 | +import {HarnessLoader} from '@angular/cdk-experimental/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed'; |
| 3 | +import {Component} from '@angular/core'; |
| 4 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {ReactiveFormsModule} from '@angular/forms'; |
| 6 | +import {MatInputModule} from '@angular/material/input'; |
| 7 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 8 | + |
| 9 | +import {MatInputHarness} from './input-harness'; |
| 10 | + |
| 11 | +let fixture: ComponentFixture<InputHarnessTest>; |
| 12 | +let loader: HarnessLoader; |
| 13 | +let inputHarness: typeof MatInputHarness; |
| 14 | + |
| 15 | +describe('MatInputHarness', () => { |
| 16 | + describe('non-MDC-based', () => { |
| 17 | + beforeEach(async () => { |
| 18 | + await TestBed |
| 19 | + .configureTestingModule({ |
| 20 | + imports: [NoopAnimationsModule, MatInputModule, ReactiveFormsModule], |
| 21 | + declarations: [InputHarnessTest], |
| 22 | + }) |
| 23 | + .compileComponents(); |
| 24 | + |
| 25 | + fixture = TestBed.createComponent(InputHarnessTest); |
| 26 | + fixture.detectChanges(); |
| 27 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 28 | + inputHarness = MatInputHarness; |
| 29 | + }); |
| 30 | + |
| 31 | + runTests(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe( |
| 35 | + 'MDC-based', |
| 36 | + () => { |
| 37 | + // TODO: run tests for MDC based input once implemented. |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +/** Shared tests to run on both the original and MDC-based input's. */ |
| 42 | +function runTests() { |
| 43 | + it('should load all input harnesses', async () => { |
| 44 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 45 | + expect(inputs.length).toBe(3); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should load input with specific id', async () => { |
| 49 | + const inputs = await loader.getAllHarnesses(inputHarness.with({id: 'myTextarea'})); |
| 50 | + expect(inputs.length).toBe(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should load input with specific name', async () => { |
| 54 | + const inputs = await loader.getAllHarnesses(inputHarness.with({name: 'favorite-food'})); |
| 55 | + expect(inputs.length).toBe(1); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should load input with specific value', async () => { |
| 59 | + const inputs = await loader.getAllHarnesses(inputHarness.with({value: 'Sushi'})); |
| 60 | + expect(inputs.length).toBe(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should be able to get id of input', async () => { |
| 64 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 65 | + expect(inputs.length).toBe(3); |
| 66 | + expect(await inputs[0].getId()).toMatch(/mat-input-\d+/); |
| 67 | + expect(await inputs[1].getId()).toMatch(/mat-input-\d+/); |
| 68 | + expect(await inputs[2].getId()).toBe('myTextarea'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should be able to get name of input', async () => { |
| 72 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 73 | + expect(inputs.length).toBe(3); |
| 74 | + expect(await inputs[0].getName()).toBe('favorite-food'); |
| 75 | + expect(await inputs[1].getName()).toBe(''); |
| 76 | + expect(await inputs[2].getName()).toBe(''); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should be able to get value of input', async () => { |
| 80 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 81 | + expect(inputs.length).toBe(3); |
| 82 | + expect(await inputs[0].getValue()).toBe('Sushi'); |
| 83 | + expect(await inputs[1].getValue()).toBe(''); |
| 84 | + expect(await inputs[2].getValue()).toBe(''); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should be able to set value of input', async () => { |
| 88 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 89 | + expect(inputs.length).toBe(3); |
| 90 | + expect(await inputs[0].getValue()).toBe('Sushi'); |
| 91 | + expect(await inputs[1].getValue()).toBe(''); |
| 92 | + |
| 93 | + await inputs[0].setValue(''); |
| 94 | + await inputs[2].setValue('new-value'); |
| 95 | + |
| 96 | + expect(await inputs[0].getValue()).toBe(''); |
| 97 | + expect(await inputs[2].getValue()).toBe('new-value'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should be able to get disabled state', async () => { |
| 101 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 102 | + expect(inputs.length).toBe(3); |
| 103 | + |
| 104 | + expect(await inputs[0].isDisabled()).toBe(false); |
| 105 | + expect(await inputs[1].isDisabled()).toBe(false); |
| 106 | + expect(await inputs[2].isDisabled()).toBe(false); |
| 107 | + |
| 108 | + fixture.componentInstance.disabled = true; |
| 109 | + |
| 110 | + expect(await inputs[1].isDisabled()).toBe(true); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should be able to get readonly state', async () => { |
| 114 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 115 | + expect(inputs.length).toBe(3); |
| 116 | + |
| 117 | + expect(await inputs[0].isReadonly()).toBe(false); |
| 118 | + expect(await inputs[1].isReadonly()).toBe(false); |
| 119 | + expect(await inputs[2].isReadonly()).toBe(false); |
| 120 | + |
| 121 | + fixture.componentInstance.readonly = true; |
| 122 | + |
| 123 | + expect(await inputs[1].isReadonly()).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should be able to get required state', async () => { |
| 127 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 128 | + expect(inputs.length).toBe(3); |
| 129 | + |
| 130 | + expect(await inputs[0].isRequired()).toBe(false); |
| 131 | + expect(await inputs[1].isRequired()).toBe(false); |
| 132 | + expect(await inputs[2].isRequired()).toBe(false); |
| 133 | + |
| 134 | + fixture.componentInstance.required = true; |
| 135 | + |
| 136 | + expect(await inputs[1].isRequired()).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should be able to get placeholder of input', async () => { |
| 140 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 141 | + expect(inputs.length).toBe(3); |
| 142 | + expect(await inputs[0].getPlaceholder()).toBe('Favorite food'); |
| 143 | + expect(await inputs[1].getPlaceholder()).toBe(''); |
| 144 | + expect(await inputs[2].getPlaceholder()).toBe('Leave a comment'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should be able to get type of input', async () => { |
| 148 | + const inputs = await loader.getAllHarnesses(inputHarness); |
| 149 | + expect(inputs.length).toBe(3); |
| 150 | + expect(await inputs[0].getType()).toBe('text'); |
| 151 | + expect(await inputs[1].getType()).toBe('number'); |
| 152 | + expect(await inputs[2].getType()).toBe('textarea'); |
| 153 | + |
| 154 | + fixture.componentInstance.inputType = 'text'; |
| 155 | + |
| 156 | + expect(await inputs[1].getType()).toBe('text'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should be able to focus input', async () => { |
| 160 | + const input = await loader.getHarness(inputHarness.with({name: 'favorite-food'})); |
| 161 | + expect(getActiveElementTagName()).not.toBe('input'); |
| 162 | + await input.focus(); |
| 163 | + expect(getActiveElementTagName()).toBe('input'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should be able to blur input', async () => { |
| 167 | + const input = await loader.getHarness(inputHarness.with({name: 'favorite-food'})); |
| 168 | + expect(getActiveElementTagName()).not.toBe('input'); |
| 169 | + await input.focus(); |
| 170 | + expect(getActiveElementTagName()).toBe('input'); |
| 171 | + await input.blur(); |
| 172 | + expect(getActiveElementTagName()).not.toBe('input'); |
| 173 | + }); |
| 174 | +} |
| 175 | + |
| 176 | +function getActiveElementTagName() { |
| 177 | + return document.activeElement ? document.activeElement.tagName.toLowerCase() : ''; |
| 178 | +} |
| 179 | + |
| 180 | +@Component({ |
| 181 | + template: ` |
| 182 | + <mat-form-field> |
| 183 | + <input matInput placeholder="Favorite food" value="Sushi" name="favorite-food"> |
| 184 | + </mat-form-field> |
| 185 | +
|
| 186 | + <mat-form-field> |
| 187 | + <input matInput [type]="inputType" |
| 188 | + [readonly]="readonly" |
| 189 | + [disabled]="disabled" |
| 190 | + [required]="required"> |
| 191 | + </mat-form-field> |
| 192 | +
|
| 193 | + <mat-form-field> |
| 194 | + <textarea id="myTextarea" matInput placeholder="Leave a comment"></textarea> |
| 195 | + </mat-form-field> |
| 196 | + ` |
| 197 | +}) |
| 198 | +class InputHarnessTest { |
| 199 | + inputType = 'number'; |
| 200 | + readonly = false; |
| 201 | + disabled = false; |
| 202 | + required = false; |
| 203 | +} |
0 commit comments