|
| 1 | +import {Component} from '@angular/core'; |
| 2 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 3 | +import {HarnessLoader} from '@angular/cdk/testing'; |
| 4 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 5 | +import {FormsModule} from '@angular/forms'; |
1 | 6 | import {MatInputModule} from '@angular/material/input';
|
2 |
| -import {runInputHarnessTests} from './shared-input.spec'; |
| 7 | +import {getSupportedInputTypes} from '@angular/cdk/platform'; |
| 8 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
3 | 9 | import {MatInputHarness} from './input-harness';
|
4 | 10 |
|
5 |
| -describe('MDC-based MatInputHarness', () => { |
6 |
| - runInputHarnessTests(MatInputModule, MatInputHarness); |
| 11 | +describe('MatInputHarness', () => { |
| 12 | + let fixture: ComponentFixture<InputHarnessTest>; |
| 13 | + let loader: HarnessLoader; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + await TestBed.configureTestingModule({ |
| 17 | + imports: [NoopAnimationsModule, MatInputModule, FormsModule], |
| 18 | + declarations: [InputHarnessTest], |
| 19 | + }).compileComponents(); |
| 20 | + |
| 21 | + fixture = TestBed.createComponent(InputHarnessTest); |
| 22 | + fixture.detectChanges(); |
| 23 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should load all input harnesses', async () => { |
| 27 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 28 | + expect(inputs.length).toBe(7); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should load input with specific id', async () => { |
| 32 | + const inputs = await loader.getAllHarnesses(MatInputHarness.with({selector: '#myTextarea'})); |
| 33 | + expect(inputs.length).toBe(1); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should load input with specific name', async () => { |
| 37 | + const inputs = await loader.getAllHarnesses( |
| 38 | + MatInputHarness.with({selector: '[name="favorite-food"]'}), |
| 39 | + ); |
| 40 | + expect(inputs.length).toBe(1); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should load input with a specific value', async () => { |
| 44 | + const inputs = await loader.getAllHarnesses(MatInputHarness.with({value: 'Sushi'})); |
| 45 | + expect(inputs.length).toBe(1); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should load input with a value that matches a regex', async () => { |
| 49 | + const inputs = await loader.getAllHarnesses(MatInputHarness.with({value: /shi$/})); |
| 50 | + expect(inputs.length).toBe(1); |
| 51 | + expect(await inputs[0].getValue()).toBe('Sushi'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should load input with a specific placeholder', async () => { |
| 55 | + const inputs = await loader.getAllHarnesses( |
| 56 | + MatInputHarness.with({placeholder: 'Favorite food'}), |
| 57 | + ); |
| 58 | + expect(inputs.length).toBe(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should load input with a placeholder that matches a regex', async () => { |
| 62 | + const inputs = await loader.getAllHarnesses(MatInputHarness.with({placeholder: / food$/})); |
| 63 | + expect(inputs.length).toBe(1); |
| 64 | + expect(await inputs[0].getPlaceholder()).toBe('Favorite food'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should be able to get id of input', async () => { |
| 68 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 69 | + expect(inputs.length).toBe(7); |
| 70 | + expect(await inputs[0].getId()).toMatch(/mat-input-\d+/); |
| 71 | + expect(await inputs[1].getId()).toMatch(/mat-input-\d+/); |
| 72 | + expect(await inputs[2].getId()).toBe('myTextarea'); |
| 73 | + expect(await inputs[3].getId()).toBe('nativeControl'); |
| 74 | + expect(await inputs[4].getId()).toMatch(/mat-input-\d+/); |
| 75 | + expect(await inputs[5].getId()).toBe('has-ng-model'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should be able to get name of input', async () => { |
| 79 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 80 | + expect(inputs.length).toBe(7); |
| 81 | + expect(await inputs[0].getName()).toBe('favorite-food'); |
| 82 | + expect(await inputs[1].getName()).toBe(''); |
| 83 | + expect(await inputs[2].getName()).toBe(''); |
| 84 | + expect(await inputs[3].getName()).toBe(''); |
| 85 | + expect(await inputs[4].getName()).toBe(''); |
| 86 | + expect(await inputs[5].getName()).toBe('has-ng-model'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should be able to get value of input', async () => { |
| 90 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 91 | + expect(inputs.length).toBe(7); |
| 92 | + expect(await inputs[0].getValue()).toBe('Sushi'); |
| 93 | + expect(await inputs[1].getValue()).toBe(''); |
| 94 | + expect(await inputs[2].getValue()).toBe(''); |
| 95 | + expect(await inputs[3].getValue()).toBe(''); |
| 96 | + expect(await inputs[4].getValue()).toBe(''); |
| 97 | + expect(await inputs[5].getValue()).toBe(''); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should be able to set value of input', async () => { |
| 101 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 102 | + expect(inputs.length).toBe(7); |
| 103 | + expect(await inputs[0].getValue()).toBe('Sushi'); |
| 104 | + expect(await inputs[1].getValue()).toBe(''); |
| 105 | + expect(await inputs[3].getValue()).toBe(''); |
| 106 | + expect(await inputs[4].getValue()).toBe(''); |
| 107 | + |
| 108 | + await inputs[0].setValue(''); |
| 109 | + await inputs[2].setValue('new-value'); |
| 110 | + await inputs[3].setValue('new-value'); |
| 111 | + await inputs[4].setValue('new-value'); |
| 112 | + |
| 113 | + expect(await inputs[0].getValue()).toBe(''); |
| 114 | + expect(await inputs[2].getValue()).toBe('new-value'); |
| 115 | + expect(await inputs[3].getValue()).toBe('new-value'); |
| 116 | + expect(await inputs[4].getValue()).toBe('new-value'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should be able to get disabled state', async () => { |
| 120 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 121 | + expect(inputs.length).toBe(7); |
| 122 | + |
| 123 | + expect(await inputs[0].isDisabled()).toBe(false); |
| 124 | + expect(await inputs[1].isDisabled()).toBe(false); |
| 125 | + expect(await inputs[2].isDisabled()).toBe(false); |
| 126 | + expect(await inputs[3].isDisabled()).toBe(false); |
| 127 | + expect(await inputs[4].isDisabled()).toBe(false); |
| 128 | + expect(await inputs[5].isDisabled()).toBe(false); |
| 129 | + |
| 130 | + fixture.componentInstance.disabled = true; |
| 131 | + |
| 132 | + expect(await inputs[1].isDisabled()).toBe(true); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should be able to get readonly state', async () => { |
| 136 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 137 | + expect(inputs.length).toBe(7); |
| 138 | + |
| 139 | + expect(await inputs[0].isReadonly()).toBe(false); |
| 140 | + expect(await inputs[1].isReadonly()).toBe(false); |
| 141 | + expect(await inputs[2].isReadonly()).toBe(false); |
| 142 | + expect(await inputs[3].isReadonly()).toBe(false); |
| 143 | + expect(await inputs[4].isReadonly()).toBe(false); |
| 144 | + expect(await inputs[5].isReadonly()).toBe(false); |
| 145 | + |
| 146 | + fixture.componentInstance.readonly = true; |
| 147 | + |
| 148 | + expect(await inputs[1].isReadonly()).toBe(true); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should be able to get required state', async () => { |
| 152 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 153 | + expect(inputs.length).toBe(7); |
| 154 | + |
| 155 | + expect(await inputs[0].isRequired()).toBe(false); |
| 156 | + expect(await inputs[1].isRequired()).toBe(false); |
| 157 | + expect(await inputs[2].isRequired()).toBe(false); |
| 158 | + expect(await inputs[3].isRequired()).toBe(false); |
| 159 | + expect(await inputs[4].isRequired()).toBe(false); |
| 160 | + expect(await inputs[5].isRequired()).toBe(false); |
| 161 | + |
| 162 | + fixture.componentInstance.required = true; |
| 163 | + |
| 164 | + expect(await inputs[1].isRequired()).toBe(true); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should be able to get placeholder of input', async () => { |
| 168 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 169 | + expect(inputs.length).toBe(7); |
| 170 | + expect(await inputs[0].getPlaceholder()).toBe('Favorite food'); |
| 171 | + expect(await inputs[1].getPlaceholder()).toBe(''); |
| 172 | + expect(await inputs[2].getPlaceholder()).toBe('Leave a comment'); |
| 173 | + expect(await inputs[3].getPlaceholder()).toBe('Native control'); |
| 174 | + expect(await inputs[4].getPlaceholder()).toBe(''); |
| 175 | + expect(await inputs[5].getPlaceholder()).toBe(''); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should be able to get type of input', async () => { |
| 179 | + const inputs = await loader.getAllHarnesses(MatInputHarness); |
| 180 | + expect(inputs.length).toBe(7); |
| 181 | + expect(await inputs[0].getType()).toBe('text'); |
| 182 | + expect(await inputs[1].getType()).toBe('number'); |
| 183 | + expect(await inputs[2].getType()).toBe('textarea'); |
| 184 | + expect(await inputs[3].getType()).toBe('text'); |
| 185 | + expect(await inputs[4].getType()).toBe('textarea'); |
| 186 | + expect(await inputs[5].getType()).toBe('text'); |
| 187 | + |
| 188 | + fixture.componentInstance.inputType = 'text'; |
| 189 | + |
| 190 | + expect(await inputs[1].getType()).toBe('text'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should be able to focus input', async () => { |
| 194 | + const input = await loader.getHarness( |
| 195 | + MatInputHarness.with({selector: '[name="favorite-food"]'}), |
| 196 | + ); |
| 197 | + expect(await input.isFocused()).toBe(false); |
| 198 | + await input.focus(); |
| 199 | + expect(await input.isFocused()).toBe(true); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should be able to blur input', async () => { |
| 203 | + const input = await loader.getHarness( |
| 204 | + MatInputHarness.with({selector: '[name="favorite-food"]'}), |
| 205 | + ); |
| 206 | + expect(await input.isFocused()).toBe(false); |
| 207 | + await input.focus(); |
| 208 | + expect(await input.isFocused()).toBe(true); |
| 209 | + await input.blur(); |
| 210 | + expect(await input.isFocused()).toBe(false); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should be able to set the value of a control that cannot be typed in', async () => { |
| 214 | + // We can't test this against browsers that don't support color inputs. |
| 215 | + if (!getSupportedInputTypes().has('color')) { |
| 216 | + return; |
| 217 | + } |
| 218 | + |
| 219 | + const input = await loader.getHarness(MatInputHarness.with({selector: '#colorControl'})); |
| 220 | + expect(await input.getValue()).toBe('#000000'); // Color inputs default to black. |
| 221 | + await input.setValue('#00ff00'); |
| 222 | + expect((await input.getValue()).toLowerCase()).toBe('#00ff00'); |
| 223 | + }); |
7 | 224 | });
|
| 225 | + |
| 226 | +@Component({ |
| 227 | + template: ` |
| 228 | + <mat-form-field> |
| 229 | + <input matInput placeholder="Favorite food" value="Sushi" name="favorite-food"> |
| 230 | + </mat-form-field> |
| 231 | +
|
| 232 | + <mat-form-field> |
| 233 | + <input matInput [type]="inputType" |
| 234 | + [readonly]="readonly" |
| 235 | + [disabled]="disabled" |
| 236 | + [required]="required"> |
| 237 | + </mat-form-field> |
| 238 | +
|
| 239 | + <mat-form-field> |
| 240 | + <textarea id="myTextarea" matInput placeholder="Leave a comment"></textarea> |
| 241 | + </mat-form-field> |
| 242 | +
|
| 243 | + <mat-form-field> |
| 244 | + <input matNativeControl placeholder="Native control" id="nativeControl"> |
| 245 | + </mat-form-field> |
| 246 | +
|
| 247 | + <mat-form-field> |
| 248 | + <textarea matNativeControl></textarea> |
| 249 | + </mat-form-field> |
| 250 | +
|
| 251 | + <mat-form-field> |
| 252 | + <!-- |
| 253 | + Select native controls should not be handled as part of the input harness. We add this |
| 254 | + to assert that the harness does not accidentally match it. |
| 255 | + --> |
| 256 | + <select matNativeControl> |
| 257 | + <option value="first">First</option> |
| 258 | + </select> |
| 259 | + </mat-form-field> |
| 260 | +
|
| 261 | + <mat-form-field> |
| 262 | + <input [(ngModel)]="ngModelValue" [name]="ngModelName" id="has-ng-model" matNativeControl> |
| 263 | + </mat-form-field> |
| 264 | +
|
| 265 | + <mat-form-field> |
| 266 | + <input matNativeControl placeholder="Color control" id="colorControl" type="color"> |
| 267 | + </mat-form-field> |
| 268 | + `, |
| 269 | +}) |
| 270 | +class InputHarnessTest { |
| 271 | + inputType = 'number'; |
| 272 | + readonly = false; |
| 273 | + disabled = false; |
| 274 | + required = false; |
| 275 | + ngModelValue = ''; |
| 276 | + ngModelName = 'has-ng-model'; |
| 277 | +} |
0 commit comments