@@ -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' ) ;
@@ -218,13 +218,27 @@ describe('FocusMonitor', () => {
218218
219219 focusMonitor . focusVia ( buttonElement , 'program' , { preventScroll : true } ) ;
220220 fixture . detectChanges ( ) ;
221- tick ( ) ;
221+ flush ( ) ;
222222
223223 expect ( buttonElement . focus ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
224224 preventScroll : true
225225 } ) ) ;
226226 } ) ) ;
227227
228+ it ( 'should not clear the focus origin too early in the current event loop' , fakeAsync ( ( ) => {
229+ dispatchKeyboardEvent ( document , 'keydown' , TAB ) ;
230+
231+ // Simulate the behavior of Firefox 57 where the focus event sometimes happens *one* tick later.
232+ tick ( ) ;
233+
234+ buttonElement . focus ( ) ;
235+
236+ // Since the timeout doesn't clear the focus origin too early as with the `0ms` timeout, the
237+ // focus origin should be reported properly.
238+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'keyboard' ) ;
239+
240+ flush ( ) ;
241+ } ) ) ;
228242} ) ;
229243
230244
@@ -263,7 +277,7 @@ describe('cdkMonitorFocus', () => {
263277 dispatchKeyboardEvent ( document , 'keydown' , TAB ) ;
264278 buttonElement . focus ( ) ;
265279 fixture . detectChanges ( ) ;
266- tick ( ) ;
280+ flush ( ) ;
267281
268282 expect ( buttonElement . classList . length )
269283 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
@@ -279,7 +293,7 @@ describe('cdkMonitorFocus', () => {
279293 dispatchMouseEvent ( buttonElement , 'mousedown' ) ;
280294 buttonElement . focus ( ) ;
281295 fixture . detectChanges ( ) ;
282- tick ( ) ;
296+ flush ( ) ;
283297
284298 expect ( buttonElement . classList . length )
285299 . toBe ( 2 , 'button should have exactly 2 focus classes' ) ;
0 commit comments