66 patchElementFocus ,
77} from '@angular/cdk/testing' ;
88import { Component } from '@angular/core' ;
9- import { ComponentFixture , fakeAsync , inject , TestBed , tick } from '@angular/core/testing' ;
9+ import { ComponentFixture , fakeAsync , flush , inject , TestBed , tick } from '@angular/core/testing' ;
1010import { By } from '@angular/platform-browser' ;
1111import { FocusMonitor , FocusOrigin , TOUCH_BUFFER_MS } from './focus-monitor' ;
1212import { A11yModule } from '../index' ;
@@ -54,7 +54,7 @@ describe('FocusMonitor', () => {
5454 dispatchKeyboardEvent ( document , 'keydown' , TAB ) ;
5555 buttonElement . focus ( ) ;
5656 fixture . detectChanges ( ) ;
57- tick ( ) ;
57+ flush ( ) ;
5858
5959 expect ( buttonElement . classList . length )
6060 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -70,7 +70,7 @@ describe('FocusMonitor', () => {
7070 dispatchMouseEvent ( buttonElement , 'mousedown' ) ;
7171 buttonElement . focus ( ) ;
7272 fixture . detectChanges ( ) ;
73- tick ( ) ;
73+ flush ( ) ;
7474
7575 expect ( buttonElement . classList . length )
7676 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -114,7 +114,7 @@ describe('FocusMonitor', () => {
114114
115115 it ( 'focusVia keyboard should simulate keyboard focus' , fakeAsync ( ( ) => {
116116 focusMonitor . focusVia ( buttonElement , 'keyboard' ) ;
117- tick ( ) ;
117+ flush ( ) ;
118118
119119 expect ( buttonElement . classList . length )
120120 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -128,7 +128,7 @@ describe('FocusMonitor', () => {
128128 it ( 'focusVia mouse should simulate mouse focus' , fakeAsync ( ( ) => {
129129 focusMonitor . focusVia ( buttonElement , 'mouse' ) ;
130130 fixture . detectChanges ( ) ;
131- tick ( ) ;
131+ flush ( ) ;
132132
133133 expect ( buttonElement . classList . length )
134134 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -142,7 +142,7 @@ describe('FocusMonitor', () => {
142142 it ( 'focusVia mouse should simulate mouse focus' , fakeAsync ( ( ) => {
143143 focusMonitor . focusVia ( buttonElement , 'touch' ) ;
144144 fixture . detectChanges ( ) ;
145- tick ( ) ;
145+ flush ( ) ;
146146
147147 expect ( buttonElement . classList . length )
148148 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -156,7 +156,7 @@ describe('FocusMonitor', () => {
156156 it ( 'focusVia program should simulate programmatic focus' , fakeAsync ( ( ) => {
157157 focusMonitor . focusVia ( buttonElement , 'program' ) ;
158158 fixture . detectChanges ( ) ;
159- tick ( ) ;
159+ flush ( ) ;
160160
161161 expect ( buttonElement . classList . length )
162162 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -213,6 +213,20 @@ describe('FocusMonitor', () => {
213213 expect ( buttonElement . classList . length ) . toBe ( 0 , 'button should not have any focus classes' ) ;
214214 } ) ) ;
215215
216+ it ( 'should not clear the focus origin too early in the current event loop' , fakeAsync ( ( ) => {
217+ dispatchKeyboardEvent ( document , 'keydown' , TAB ) ;
218+
219+ // Simulate the behavior of Firefox 57 where the focus event sometimes happens a tick later.
220+ tick ( ) ;
221+
222+ buttonElement . focus ( ) ;
223+
224+ // Since the timeout doesn't clear the focus origin too early as with the `0ms` timeout, the
225+ // focus origin should be reported properly.
226+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'keyboard' ) ;
227+
228+ flush ( ) ;
229+ } ) ) ;
216230} ) ;
217231
218232
@@ -250,7 +264,7 @@ describe('cdkMonitorFocus', () => {
250264 dispatchKeyboardEvent ( document , 'keydown' , TAB ) ;
251265 buttonElement . focus ( ) ;
252266 fixture . detectChanges ( ) ;
253- tick ( ) ;
267+ flush ( ) ;
254268
255269 expect ( buttonElement . classList . length )
256270 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -266,7 +280,7 @@ describe('cdkMonitorFocus', () => {
266280 dispatchMouseEvent ( buttonElement , 'mousedown' ) ;
267281 buttonElement . focus ( ) ;
268282 fixture . detectChanges ( ) ;
269- tick ( ) ;
283+ flush ( ) ;
270284
271285 expect ( buttonElement . classList . length )
272286 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
0 commit comments