11import { TAB } from '@angular/cdk/keycodes' ;
22import { dispatchFakeEvent , dispatchKeyboardEvent , dispatchMouseEvent } from '@angular/cdk/testing' ;
33import { Component } from '@angular/core' ;
4- import { ComponentFixture , fakeAsync , inject , TestBed , tick } from '@angular/core/testing' ;
4+ import { ComponentFixture , fakeAsync , flush , inject , TestBed , tick } from '@angular/core/testing' ;
55import { By } from '@angular/platform-browser' ;
66import { FocusMonitor , FocusOrigin , TOUCH_BUFFER_MS } from './focus-monitor' ;
77import { A11yModule } from './index' ;
@@ -49,7 +49,7 @@ describe('FocusMonitor', () => {
4949 dispatchKeyboardEvent ( document , 'keydown' , TAB ) ;
5050 buttonElement . focus ( ) ;
5151 fixture . detectChanges ( ) ;
52- tick ( ) ;
52+ flush ( ) ;
5353
5454 expect ( buttonElement . classList . length )
5555 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -65,7 +65,7 @@ describe('FocusMonitor', () => {
6565 dispatchMouseEvent ( buttonElement , 'mousedown' ) ;
6666 buttonElement . focus ( ) ;
6767 fixture . detectChanges ( ) ;
68- tick ( ) ;
68+ flush ( ) ;
6969
7070 expect ( buttonElement . classList . length )
7171 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -109,7 +109,7 @@ describe('FocusMonitor', () => {
109109
110110 it ( 'focusVia keyboard should simulate keyboard focus' , fakeAsync ( ( ) => {
111111 focusMonitor . focusVia ( buttonElement , 'keyboard' ) ;
112- tick ( ) ;
112+ flush ( ) ;
113113
114114 expect ( buttonElement . classList . length )
115115 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -123,7 +123,7 @@ describe('FocusMonitor', () => {
123123 it ( 'focusVia mouse should simulate mouse focus' , fakeAsync ( ( ) => {
124124 focusMonitor . focusVia ( buttonElement , 'mouse' ) ;
125125 fixture . detectChanges ( ) ;
126- tick ( ) ;
126+ flush ( ) ;
127127
128128 expect ( buttonElement . classList . length )
129129 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -137,7 +137,7 @@ describe('FocusMonitor', () => {
137137 it ( 'focusVia mouse should simulate mouse focus' , fakeAsync ( ( ) => {
138138 focusMonitor . focusVia ( buttonElement , 'touch' ) ;
139139 fixture . detectChanges ( ) ;
140- tick ( ) ;
140+ flush ( ) ;
141141
142142 expect ( buttonElement . classList . length )
143143 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -151,7 +151,7 @@ describe('FocusMonitor', () => {
151151 it ( 'focusVia program should simulate programmatic focus' , fakeAsync ( ( ) => {
152152 focusMonitor . focusVia ( buttonElement , 'program' ) ;
153153 fixture . detectChanges ( ) ;
154- tick ( ) ;
154+ flush ( ) ;
155155
156156 expect ( buttonElement . classList . length )
157157 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -194,6 +194,21 @@ describe('FocusMonitor', () => {
194194
195195 expect ( buttonElement . classList . length ) . toBe ( 0 , 'button should not have any focus classes' ) ;
196196 } ) ) ;
197+
198+ it ( 'should not clear the focus origin too early in the current event loop' , fakeAsync ( ( ) => {
199+ dispatchKeyboardEvent ( document , 'keydown' , TAB ) ;
200+
201+ // Simulate the behavior of Firefox 57 where the focus event sometimes happens a tick later.
202+ tick ( ) ;
203+
204+ buttonElement . focus ( ) ;
205+
206+ // Since the timeout doesn't clear the focus origin too early as with the `0ms` timeout, the
207+ // focus origin should be reported properly.
208+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'keyboard' ) ;
209+
210+ flush ( ) ;
211+ } ) ) ;
197212} ) ;
198213
199214
@@ -231,7 +246,7 @@ describe('cdkMonitorFocus', () => {
231246 dispatchKeyboardEvent ( document , 'keydown' , TAB ) ;
232247 buttonElement . focus ( ) ;
233248 fixture . detectChanges ( ) ;
234- tick ( ) ;
249+ flush ( ) ;
235250
236251 expect ( buttonElement . classList . length )
237252 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -247,7 +262,7 @@ describe('cdkMonitorFocus', () => {
247262 dispatchMouseEvent ( buttonElement , 'mousedown' ) ;
248263 buttonElement . focus ( ) ;
249264 fixture . detectChanges ( ) ;
250- tick ( ) ;
265+ flush ( ) ;
251266
252267 expect ( buttonElement . classList . length )
253268 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
0 commit comments