Skip to content

Commit aaef71b

Browse files
committed
Improve IE11 test stability
1 parent 40105e8 commit aaef71b

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

src/cdk/a11y/focus-monitor.spec.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import {TAB} from '@angular/cdk/keycodes';
2-
import {dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent} from '@angular/cdk/testing';
2+
import {
3+
dispatchFakeEvent,
4+
dispatchKeyboardEvent,
5+
dispatchMouseEvent,
6+
patchElementFocus
7+
} from '@angular/cdk/testing';
38
import {Component} from '@angular/core';
49
import {ComponentFixture, fakeAsync, inject, TestBed, tick} from '@angular/core/testing';
510
import {By} from '@angular/platform-browser';
@@ -399,14 +404,3 @@ class ComplexComponentWithMonitorElementFocus {}
399404
template: `<div tabindex="0" cdkMonitorSubtreeFocus><button></button></div>`
400405
})
401406
class ComplexComponentWithMonitorSubtreeFocus {}
402-
403-
404-
/**
405-
* Patches an elements focus and blur methods to emit events consistently and predictably.
406-
* This is necessary, because some browsers, like IE11, will call the focus handlers asynchronously,
407-
* while others won't fire them at all if the browser window is not focused.
408-
*/
409-
function patchElementFocus(element: HTMLElement) {
410-
element.focus = () => dispatchFakeEvent(element, 'focus');
411-
element.blur = () => dispatchFakeEvent(element, 'blur');
412-
}

src/cdk/testing/element-focus.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {dispatchFakeEvent} from './dispatch-events';
10+
11+
/**
12+
* Patches an elements focus and blur methods to emit events consistently and predictably.
13+
* This is necessary, because some browsers, like IE11, will call the focus handlers asynchronously,
14+
* while others won't fire them at all if the browser window is not focused.
15+
*/
16+
export function patchElementFocus(element: HTMLElement) {
17+
element.focus = () => dispatchFakeEvent(element, 'focus');
18+
element.blur = () => dispatchFakeEvent(element, 'blur');
19+
}

src/cdk/testing/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
10-
119
export * from './public-api';

src/cdk/testing/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export * from './type-in-element';
1212
export * from './wrapped-error-message';
1313
export * from './fake-viewport-ruler';
1414
export * from './mock-ng-zone';
15+
export * from './element-focus';

src/lib/dialog/dialog.spec.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {MatDialogContainer} from './dialog-container';
2727
import {OverlayContainer, ScrollStrategy} from '@angular/cdk/overlay';
2828
import {FocusOrigin, FocusMonitor} from '@angular/cdk/a11y';
2929
import {A, ESCAPE} from '@angular/cdk/keycodes';
30-
import {dispatchKeyboardEvent} from '@angular/cdk/testing';
30+
import {dispatchKeyboardEvent, patchElementFocus} from '@angular/cdk/testing';
3131
import {
3232
MAT_DIALOG_DATA,
3333
MatDialog,
@@ -907,15 +907,20 @@ describe('MatDialog', () => {
907907
const button = document.createElement('button');
908908
let lastFocusOrigin: FocusOrigin = null;
909909

910-
focusMonitor.monitor(button, false).subscribe(focusOrigin => lastFocusOrigin = focusOrigin);
910+
focusMonitor.monitor(button, false)
911+
.subscribe(focusOrigin => lastFocusOrigin = focusOrigin);
912+
911913
document.body.appendChild(button);
912914
button.focus();
913915

916+
// Patch the element focus after the initial and real focus, because otherwise the
917+
// `activeElement` won't be set, and the dialog won't be able to restore focus to an element.
918+
patchElementFocus(button);
919+
914920
dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
915921

916-
flushMicrotasks();
922+
tick(500);
917923
viewContainerFixture.detectChanges();
918-
flushMicrotasks();
919924

920925
expect(lastFocusOrigin!).toBeNull('Expected the trigger button to be blurred');
921926

@@ -936,17 +941,20 @@ describe('MatDialog', () => {
936941
const button = document.createElement('button');
937942
let lastFocusOrigin: FocusOrigin = null;
938943

939-
focusMonitor.monitor(button, false).subscribe(focusOrigin => lastFocusOrigin = focusOrigin);
944+
focusMonitor.monitor(button, false)
945+
.subscribe(focusOrigin => lastFocusOrigin = focusOrigin);
946+
940947
document.body.appendChild(button);
941948
button.focus();
942949

950+
// Patch the element focus after the initial and real focus, because otherwise the
951+
// `activeElement` won't be set, and the dialog won't be able to restore focus to an element.
952+
patchElementFocus(button);
953+
943954
dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
944955

945-
flushMicrotasks();
956+
tick(500);
946957
viewContainerFixture.detectChanges();
947-
flushMicrotasks();
948-
949-
expect(lastFocusOrigin!).toBeNull('Expected the trigger button to be blurred');
950958

951959
const backdrop = overlayContainerElement
952960
.querySelector('.cdk-overlay-backdrop') as HTMLElement;

0 commit comments

Comments
 (0)