|
6 | 6 | ViewContainerRef, |
7 | 7 | ViewEncapsulation, |
8 | 8 | } from '@angular/core'; |
9 | | -import {waitForAsync, ComponentFixture, TestBed} from '@angular/core/testing'; |
| 9 | +import {waitForAsync, ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing'; |
10 | 10 | import {PortalModule, CdkPortalOutlet, TemplatePortal} from '@angular/cdk/portal'; |
11 | 11 | import {A11yModule, FocusTrap, CdkTrapFocus} from '../index'; |
12 | 12 | import {By} from '@angular/platform-browser'; |
@@ -91,10 +91,11 @@ describe('FocusTrap', () => { |
91 | 91 | describe('with bindings', () => { |
92 | 92 | let fixture: ComponentFixture<FocusTrapWithBindings>; |
93 | 93 |
|
94 | | - beforeEach(() => { |
| 94 | + beforeEach(fakeAsync(() => { |
95 | 95 | fixture = TestBed.createComponent(FocusTrapWithBindings); |
96 | 96 | fixture.detectChanges(); |
97 | | - }); |
| 97 | + tick(); |
| 98 | + })); |
98 | 99 |
|
99 | 100 | it('should clean up its anchor sibling elements on destroy', () => { |
100 | 101 | const rootElement = fixture.debugElement.nativeElement as HTMLElement; |
@@ -298,26 +299,51 @@ describe('FocusTrap', () => { |
298 | 299 |
|
299 | 300 | }); |
300 | 301 |
|
301 | | - it('should put anchors inside the outlet when set at the root of a template portal', () => { |
302 | | - const fixture = TestBed.createComponent(FocusTrapInsidePortal); |
303 | | - const instance = fixture.componentInstance; |
304 | | - fixture.detectChanges(); |
305 | | - const outlet: HTMLElement = fixture.nativeElement.querySelector('.portal-outlet'); |
306 | | - |
307 | | - expect(outlet.querySelectorAll('button').length) |
308 | | - .withContext('Expected no buttons inside the outlet on init.').toBe(0); |
309 | | - expect(outlet.querySelectorAll('.cdk-focus-trap-anchor').length) |
310 | | - .withContext('Expected no focus trap anchors inside the outlet on init.').toBe(0); |
311 | | - |
312 | | - const portal = new TemplatePortal(instance.template, instance.viewContainerRef); |
313 | | - instance.portalOutlet.attachTemplatePortal(portal); |
314 | | - fixture.detectChanges(); |
315 | | - |
316 | | - expect(outlet.querySelectorAll('button').length) |
317 | | - .withContext('Expected one button inside the outlet after attaching.').toBe(1); |
318 | | - expect(outlet.querySelectorAll('.cdk-focus-trap-anchor').length) |
319 | | - .withContext('Expected two focus trap anchors in the outlet after attaching.').toBe(2); |
320 | | - }); |
| 302 | + it('should put anchors inside the outlet when set at the root of a template portal', |
| 303 | + fakeAsync(() => { |
| 304 | + const fixture = TestBed.createComponent(FocusTrapInsidePortal); |
| 305 | + const instance = fixture.componentInstance; |
| 306 | + fixture.detectChanges(); |
| 307 | + const outlet: HTMLElement = fixture.nativeElement.querySelector('.portal-outlet'); |
| 308 | + |
| 309 | + expect(outlet.querySelectorAll('button').length) |
| 310 | + .withContext('Expected no buttons inside the outlet on init.').toBe(0); |
| 311 | + expect(outlet.querySelectorAll('.cdk-focus-trap-anchor').length) |
| 312 | + .withContext('Expected no focus trap anchors inside the outlet on init.').toBe(0); |
| 313 | + |
| 314 | + const portal = new TemplatePortal(instance.template, instance.viewContainerRef); |
| 315 | + instance.portalOutlet.attachTemplatePortal(portal); |
| 316 | + fixture.detectChanges(); |
| 317 | + tick(); |
| 318 | + |
| 319 | + expect(outlet.querySelectorAll('button').length) |
| 320 | + .withContext('Expected one button inside the outlet after attaching.').toBe(1); |
| 321 | + expect(outlet.querySelectorAll('.cdk-focus-trap-anchor').length) |
| 322 | + .withContext('Expected two focus trap anchors in the outlet after attaching.').toBe(2); |
| 323 | + })); |
| 324 | + |
| 325 | + it('should put anchors inside the outlet when set at the root of a template portal', |
| 326 | + fakeAsync(() => { |
| 327 | + const fixture = TestBed.createComponent(FocusTrapInsidePortal); |
| 328 | + const instance = fixture.componentInstance; |
| 329 | + fixture.detectChanges(); |
| 330 | + const outlet: HTMLElement = fixture.nativeElement.querySelector('.portal-outlet'); |
| 331 | + |
| 332 | + expect(outlet.querySelectorAll('button').length) |
| 333 | + .withContext('Expected no buttons inside the outlet on init.').toBe(0); |
| 334 | + expect(outlet.querySelectorAll('.cdk-focus-trap-anchor').length) |
| 335 | + .withContext('Expected no focus trap anchors inside the outlet on init.').toBe(0); |
| 336 | + |
| 337 | + const portal = new TemplatePortal(instance.template, instance.viewContainerRef); |
| 338 | + instance.portalOutlet.attachTemplatePortal(portal); |
| 339 | + fixture.detectChanges(); |
| 340 | + tick(); |
| 341 | + |
| 342 | + expect(outlet.querySelectorAll('button').length) |
| 343 | + .withContext('Expected one button inside the outlet after attaching.').toBe(1); |
| 344 | + expect(outlet.querySelectorAll('.cdk-focus-trap-anchor').length) |
| 345 | + .withContext('Expected two focus trap anchors in the outlet after attaching.').toBe(2); |
| 346 | + })); |
321 | 347 | }); |
322 | 348 |
|
323 | 349 | /** Gets the currently-focused element while accounting for the shadow DOM. */ |
|
0 commit comments