1
1
import { Platform , _supportsShadowDom } from '@angular/cdk/platform' ;
2
+ import { CdkPortalOutlet , PortalModule , TemplatePortal } from '@angular/cdk/portal' ;
2
3
import {
3
4
Component ,
4
- ViewChild ,
5
5
TemplateRef ,
6
+ ViewChild ,
6
7
ViewContainerRef ,
7
8
ViewEncapsulation ,
8
- provideZoneChangeDetection ,
9
9
} from '@angular/core' ;
10
- import { waitForAsync , ComponentFixture , TestBed } from '@angular/core/testing' ;
11
- import { PortalModule , CdkPortalOutlet , TemplatePortal } from '@angular/cdk/portal' ;
12
- import { A11yModule , FocusTrap , CdkTrapFocus } from '../index' ;
10
+ import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ;
13
11
import { By } from '@angular/platform-browser' ;
12
+ import { A11yModule , CdkTrapFocus , FocusTrap } from '../index' ;
14
13
15
14
describe ( 'FocusTrap' , ( ) => {
16
15
beforeEach ( waitForAsync ( ( ) => {
17
16
TestBed . configureTestingModule ( {
18
- providers : [ provideZoneChangeDetection ( ) ] ,
19
17
imports : [
20
18
A11yModule ,
21
19
PortalModule ,
@@ -106,6 +104,7 @@ describe('FocusTrap', () => {
106
104
expect ( rootElement . querySelectorAll ( 'div.cdk-visually-hidden' ) . length ) . toBe ( 2 ) ;
107
105
108
106
fixture . componentInstance . renderFocusTrap = false ;
107
+ fixture . changeDetectorRef . markForCheck ( ) ;
109
108
fixture . detectChanges ( ) ;
110
109
111
110
expect ( rootElement . querySelectorAll ( 'div.cdk-visually-hidden' ) . length ) . toBe ( 0 ) ;
@@ -120,6 +119,7 @@ describe('FocusTrap', () => {
120
119
expect ( anchors . every ( current => current . getAttribute ( 'aria-hidden' ) === 'true' ) ) . toBe ( true ) ;
121
120
122
121
fixture . componentInstance . _isFocusTrapEnabled = false ;
122
+ fixture . changeDetectorRef . markForCheck ( ) ;
123
123
fixture . detectChanges ( ) ;
124
124
125
125
expect ( anchors . every ( current => ! current . hasAttribute ( 'tabindex' ) ) ) . toBe ( true ) ;
@@ -216,12 +216,16 @@ describe('FocusTrap', () => {
216
216
expect ( getActiveElement ( ) ) . toBe ( buttonOutsideTrappedRegion ) ;
217
217
218
218
fixture . componentInstance . showTrappedRegion = true ;
219
+ fixture . changeDetectorRef . markForCheck ( ) ;
219
220
fixture . detectChanges ( ) ;
220
221
221
222
fixture . whenStable ( ) . then ( ( ) => {
222
223
expect ( getActiveElement ( ) . id ) . toBe ( 'auto-capture-target' ) ;
223
224
224
- fixture . destroy ( ) ;
225
+ fixture . componentInstance . showTrappedRegion = false ;
226
+ fixture . changeDetectorRef . markForCheck ( ) ;
227
+ fixture . detectChanges ( ) ;
228
+
225
229
expect ( getActiveElement ( ) ) . toBe ( buttonOutsideTrappedRegion ) ;
226
230
} ) ;
227
231
} ) ) ;
@@ -230,19 +234,24 @@ describe('FocusTrap', () => {
230
234
const fixture = TestBed . createComponent ( FocusTrapWithAutoCapture ) ;
231
235
fixture . componentInstance . autoCaptureEnabled = false ;
232
236
fixture . componentInstance . showTrappedRegion = true ;
237
+ fixture . changeDetectorRef . markForCheck ( ) ;
233
238
fixture . detectChanges ( ) ;
234
239
235
240
const buttonOutsideTrappedRegion = fixture . nativeElement . querySelector ( 'button' ) ;
236
241
buttonOutsideTrappedRegion . focus ( ) ;
237
242
expect ( getActiveElement ( ) ) . toBe ( buttonOutsideTrappedRegion ) ;
238
243
239
244
fixture . componentInstance . autoCaptureEnabled = true ;
245
+ fixture . changeDetectorRef . markForCheck ( ) ;
240
246
fixture . detectChanges ( ) ;
241
247
242
248
fixture . whenStable ( ) . then ( ( ) => {
243
249
expect ( getActiveElement ( ) . id ) . toBe ( 'auto-capture-target' ) ;
244
250
245
- fixture . destroy ( ) ;
251
+ fixture . componentInstance . showTrappedRegion = false ;
252
+ fixture . changeDetectorRef . markForCheck ( ) ;
253
+ fixture . detectChanges ( ) ;
254
+
246
255
expect ( getActiveElement ( ) ) . toBe ( buttonOutsideTrappedRegion ) ;
247
256
} ) ;
248
257
} ) ) ;
@@ -260,12 +269,16 @@ describe('FocusTrap', () => {
260
269
expect ( getActiveElement ( ) ) . toBe ( buttonOutsideTrappedRegion ) ;
261
270
262
271
fixture . componentInstance . showTrappedRegion = true ;
272
+ fixture . changeDetectorRef . markForCheck ( ) ;
263
273
fixture . detectChanges ( ) ;
264
274
265
275
fixture . whenStable ( ) . then ( ( ) => {
266
276
expect ( getActiveElement ( ) . id ) . toBe ( 'auto-capture-target' ) ;
267
277
268
- fixture . destroy ( ) ;
278
+ fixture . componentInstance . showTrappedRegion = false ;
279
+ fixture . changeDetectorRef . markForCheck ( ) ;
280
+ fixture . detectChanges ( ) ;
281
+
269
282
expect ( getActiveElement ( ) ) . toBe ( buttonOutsideTrappedRegion ) ;
270
283
} ) ;
271
284
} ) ) ;
@@ -278,19 +291,24 @@ describe('FocusTrap', () => {
278
291
const fixture = TestBed . createComponent ( FocusTrapWithAutoCaptureInShadowDom ) ;
279
292
fixture . componentInstance . autoCaptureEnabled = false ;
280
293
fixture . componentInstance . showTrappedRegion = true ;
294
+ fixture . changeDetectorRef . markForCheck ( ) ;
281
295
fixture . detectChanges ( ) ;
282
296
283
297
const buttonOutsideTrappedRegion = fixture . debugElement . query ( By . css ( 'button' ) ) . nativeElement ;
284
298
buttonOutsideTrappedRegion . focus ( ) ;
285
299
expect ( getActiveElement ( ) ) . toBe ( buttonOutsideTrappedRegion ) ;
286
300
287
301
fixture . componentInstance . autoCaptureEnabled = true ;
302
+ fixture . changeDetectorRef . markForCheck ( ) ;
288
303
fixture . detectChanges ( ) ;
289
304
290
305
fixture . whenStable ( ) . then ( ( ) => {
291
306
expect ( getActiveElement ( ) . id ) . toBe ( 'auto-capture-target' ) ;
292
307
293
- fixture . destroy ( ) ;
308
+ fixture . componentInstance . showTrappedRegion = false ;
309
+ fixture . changeDetectorRef . markForCheck ( ) ;
310
+ fixture . detectChanges ( ) ;
311
+
294
312
expect ( getActiveElement ( ) ) . toBe ( buttonOutsideTrappedRegion ) ;
295
313
} ) ;
296
314
} ) ) ;
0 commit comments