@@ -28,14 +28,13 @@ import {
28
28
} from '@angular/material/autocomplete' ;
29
29
import { MatError , MatFormFieldModule } from '@angular/material/form-field' ;
30
30
import { MatInputModule } from '@angular/material/input' ;
31
- import { OverlayContainer } from '@angular/cdk/overlay' ;
32
- import { DebugElement , NgZone } from '@angular/core' ;
31
+ import { DebugElement } from '@angular/core' ;
33
32
import {
34
33
ComponentFixture ,
35
34
fakeAsync ,
36
- inject ,
37
35
TestBed ,
38
36
tick ,
37
+ waitForAsync ,
39
38
} from '@angular/core/testing' ;
40
39
import { ReactiveFormsModule } from '@angular/forms' ;
41
40
import { By } from '@angular/platform-browser' ;
@@ -44,11 +43,15 @@ import {
44
43
ErrorTestExpectation ,
45
44
setupMockStore ,
46
45
getJsonFormsService ,
47
- } from '@jsonforms/angular-test ' ;
46
+ } from './common ' ;
48
47
import { ControlElement , JsonSchema , Actions } from '@jsonforms/core' ;
49
48
import { AutocompleteControlRenderer } from '../src' ;
50
49
import { JsonFormsAngularService } from '@jsonforms/angular' ;
51
50
import { ErrorObject } from 'ajv' ;
51
+ import { initTestEnvironment } from './test' ;
52
+ import { HarnessLoader } from '@angular/cdk/testing' ;
53
+ import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed' ;
54
+ import { MatAutocompleteHarness } from '@angular/material/autocomplete/testing' ;
52
55
53
56
const data = { foo : 'A' } ;
54
57
const schema : JsonSchema = {
@@ -80,17 +83,19 @@ const errorTest: ErrorTestExpectation = {
80
83
indexOfElement : 0 ,
81
84
} ;
82
85
86
+ initTestEnvironment ( ) ;
87
+
83
88
describe ( 'Autocomplete control Base Tests' , ( ) => {
84
89
let fixture : ComponentFixture < AutocompleteControlRenderer > ;
85
90
let component : AutocompleteControlRenderer ;
86
91
let inputElement : HTMLInputElement ;
87
- beforeEach ( ( ) => {
92
+ beforeEach ( waitForAsync ( ( ) => {
88
93
TestBed . configureTestingModule ( {
89
94
declarations : [ componentUT ] ,
90
95
imports : imports ,
91
96
providers : providers ,
92
97
} ) . compileComponents ( ) ;
93
- } ) ;
98
+ } ) ) ;
94
99
beforeEach ( ( ) => {
95
100
fixture = TestBed . createComponent ( componentUT ) ;
96
101
component = fixture . componentInstance ;
@@ -211,40 +216,21 @@ describe('Autocomplete control Base Tests', () => {
211
216
describe ( 'AutoComplete control Input Event Tests' , ( ) => {
212
217
let fixture : ComponentFixture < AutocompleteControlRenderer > ;
213
218
let component : AutocompleteControlRenderer ;
214
- let inputElement : HTMLInputElement ;
215
- let overlayContainer : OverlayContainer ;
216
- let overlayContainerElement : HTMLElement ;
217
- let zone : NgZone ;
218
- beforeEach ( ( ) => {
219
+ let loader : HarnessLoader ;
220
+ beforeEach ( waitForAsync ( ( ) => {
219
221
TestBed . configureTestingModule ( {
220
222
declarations : [ componentUT ] ,
221
223
imports : imports ,
222
224
providers : [ ...providers ] ,
223
225
} ) . compileComponents ( ) ;
224
-
225
- inject ( [ OverlayContainer ] , ( oc : OverlayContainer ) => {
226
- overlayContainer = oc ;
227
- overlayContainerElement = oc . getContainerElement ( ) ;
228
- } ) ( ) ;
229
- } ) ;
230
- beforeEach ( ( ) => {
226
+ } ) ) ;
227
+ beforeEach ( waitForAsync ( ( ) => {
231
228
fixture = TestBed . createComponent ( componentUT ) ;
232
229
component = fixture . componentInstance ;
233
- zone = TestBed . inject ( NgZone ) ;
234
- spyOn ( zone , 'runOutsideAngular' ) . and . callFake ( ( fn : ( ) => any ) => fn ( ) ) ;
235
- inputElement = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
236
- } ) ;
230
+ loader = TestbedHarnessEnvironment . loader ( fixture ) ;
231
+ } ) ) ;
237
232
238
- afterEach ( inject (
239
- [ OverlayContainer ] ,
240
- ( currentOverlayContainer : OverlayContainer ) => {
241
- // Since we're resetting the testing module in some of the tests,
242
- // we can potentially have multiple overlay containers.
243
- currentOverlayContainer . ngOnDestroy ( ) ;
244
- overlayContainer . ngOnDestroy ( ) ;
245
- }
246
- ) ) ;
247
- it ( 'should update via input event' , fakeAsync ( ( ) => {
233
+ it ( 'should update via input event' , fakeAsync ( async ( ) => {
248
234
setupMockStore ( fixture , { uischema, schema, data } ) ;
249
235
getJsonFormsService ( component ) . updateCore (
250
236
Actions . init ( data , schema , uischema )
@@ -255,14 +241,12 @@ describe('AutoComplete control Input Event Tests', () => {
255
241
256
242
const spy = spyOn ( component , 'onSelect' ) ;
257
243
258
- inputElement . focus ( ) ;
259
- zone . runOutsideAngular ( ( ) => zone . onStable . emit ( null ) ) ;
244
+ await ( await loader . getHarness ( MatAutocompleteHarness ) ) . focus ( ) ;
260
245
fixture . detectChanges ( ) ;
261
246
262
- const options = overlayContainerElement . querySelectorAll (
263
- 'mat-option'
264
- ) as NodeListOf < HTMLElement > ;
265
- options . item ( 1 ) . click ( ) ;
247
+ await (
248
+ await loader . getHarness ( MatAutocompleteHarness )
249
+ ) . selectOption ( { text : 'B' } ) ;
266
250
tick ( ) ;
267
251
fixture . detectChanges ( ) ;
268
252
@@ -272,7 +256,7 @@ describe('AutoComplete control Input Event Tests', () => {
272
256
273
257
expect ( event . option . value ) . toBe ( 'B' ) ;
274
258
} ) ) ;
275
- it ( 'options should prefer own props' , fakeAsync ( ( ) => {
259
+ it ( 'options should prefer own props' , fakeAsync ( async ( ) => {
276
260
setupMockStore ( fixture , { uischema, schema, data } ) ;
277
261
getJsonFormsService ( component ) . updateCore (
278
262
Actions . init ( data , schema , uischema )
@@ -283,33 +267,30 @@ describe('AutoComplete control Input Event Tests', () => {
283
267
fixture . detectChanges ( ) ;
284
268
const spy = spyOn ( component , 'onSelect' ) ;
285
269
286
- inputElement . focus ( ) ;
287
- zone . runOutsideAngular ( ( ) => zone . onStable . emit ( null ) ) ;
270
+ await ( await loader . getHarness ( MatAutocompleteHarness ) ) . focus ( ) ;
288
271
fixture . detectChanges ( ) ;
289
272
290
- fixture . whenStable ( ) . then ( ( ) => {
291
- const options = overlayContainerElement ?. querySelectorAll (
292
- 'mat-option'
293
- ) as NodeListOf < HTMLElement > ;
294
- ( options [ 1 ] as HTMLElement ) . click ( ) ;
295
- fixture . detectChanges ( ) ;
296
- tick ( ) ;
297
- const event = spy . calls . mostRecent ( )
298
- . args [ 0 ] as MatAutocompleteSelectedEvent ;
299
- expect ( event . option . value ) . toBe ( 'Y' ) ;
300
- } ) ;
273
+ await (
274
+ await loader . getHarness ( MatAutocompleteHarness )
275
+ ) . selectOption ( { text : 'Y' } ) ;
276
+ fixture . detectChanges ( ) ;
277
+ tick ( ) ;
278
+
279
+ const event = spy . calls . mostRecent ( )
280
+ . args [ 0 ] as MatAutocompleteSelectedEvent ;
281
+ expect ( event . option . value ) . toBe ( 'Y' ) ;
301
282
} ) ) ;
302
283
} ) ;
303
284
describe ( 'AutoComplete control Error Tests' , ( ) => {
304
285
let fixture : ComponentFixture < AutocompleteControlRenderer > ;
305
286
let component : AutocompleteControlRenderer ;
306
- beforeEach ( ( ) => {
287
+ beforeEach ( waitForAsync ( ( ) => {
307
288
TestBed . configureTestingModule ( {
308
289
declarations : [ componentUT ] ,
309
290
imports : imports ,
310
291
providers : providers ,
311
292
} ) . compileComponents ( ) ;
312
- } ) ;
293
+ } ) ) ;
313
294
beforeEach ( ( ) => {
314
295
fixture = TestBed . createComponent ( componentUT ) ;
315
296
component = fixture . componentInstance ;
@@ -349,13 +330,13 @@ describe('AutoComplete control updateFilter function', () => {
349
330
let fixture : ComponentFixture < AutocompleteControlRenderer > ;
350
331
let component : AutocompleteControlRenderer ;
351
332
352
- beforeEach ( ( ) => {
333
+ beforeEach ( waitForAsync ( ( ) => {
353
334
TestBed . configureTestingModule ( {
354
335
declarations : [ componentUT ] ,
355
336
imports : imports ,
356
337
providers : providers ,
357
338
} ) . compileComponents ( ) ;
358
- } ) ;
339
+ } ) ) ;
359
340
360
341
beforeEach ( ( ) => {
361
342
fixture = TestBed . createComponent ( componentUT ) ;
0 commit comments