1
1
import { async , ComponentFixture , TestBed , inject } from '@angular/core/testing' ;
2
- import { Component , Renderer } from '@angular/core' ;
2
+ import { Component , Renderer , ViewChild } from '@angular/core' ;
3
3
import { StyleModule } from './index' ;
4
4
import { By } from '@angular/platform-browser' ;
5
5
import { TAB } from '../keyboard/keycodes' ;
6
- import { FocusOriginMonitor } from './focus-classes' ;
6
+ import { FocusOriginMonitor , FocusOrigin , CdkFocusClasses } from './focus-classes' ;
7
7
import { PlatformModule } from '../platform/index' ;
8
8
import { Platform } from '../platform/platform' ;
9
9
@@ -20,6 +20,7 @@ describe('FocusOriginMonitor', () => {
20
20
let buttonRenderer : Renderer ;
21
21
let focusOriginMonitor : FocusOriginMonitor ;
22
22
let platform : Platform ;
23
+ let changeHandler : ( origin : FocusOrigin ) => void ;
23
24
24
25
beforeEach ( async ( ( ) => {
25
26
TestBed . configureTestingModule ( {
@@ -41,7 +42,9 @@ describe('FocusOriginMonitor', () => {
41
42
focusOriginMonitor = fom ;
42
43
platform = pfm ;
43
44
44
- focusOriginMonitor . registerElementForFocusClasses ( buttonElement , buttonRenderer ) ;
45
+ changeHandler = jasmine . createSpy ( 'focus origin change handler' ) ;
46
+ focusOriginMonitor . registerElementForFocusClasses ( buttonElement , buttonRenderer )
47
+ . subscribe ( changeHandler ) ;
45
48
} ) ) ;
46
49
47
50
it ( 'manually registered element should receive focus classes' , async ( ( ) => {
@@ -55,6 +58,7 @@ describe('FocusOriginMonitor', () => {
55
58
56
59
expect ( buttonElement . classList . contains ( 'cdk-focused' ) )
57
60
. toBe ( true , 'button should have cdk-focused class' ) ;
61
+ expect ( changeHandler ) . toHaveBeenCalledTimes ( 1 ) ;
58
62
} , 0 ) ;
59
63
} ) ) ;
60
64
@@ -75,6 +79,7 @@ describe('FocusOriginMonitor', () => {
75
79
. toBe ( true , 'button should have cdk-focused class' ) ;
76
80
expect ( buttonElement . classList . contains ( 'cdk-keyboard-focused' ) )
77
81
. toBe ( true , 'button should have cdk-keyboard-focused class' ) ;
82
+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'keyboard' ) ;
78
83
} , 0 ) ;
79
84
} ) ) ;
80
85
@@ -95,6 +100,7 @@ describe('FocusOriginMonitor', () => {
95
100
. toBe ( true , 'button should have cdk-focused class' ) ;
96
101
expect ( buttonElement . classList . contains ( 'cdk-mouse-focused' ) )
97
102
. toBe ( true , 'button should have cdk-mouse-focused class' ) ;
103
+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'mouse' ) ;
98
104
} , 0 ) ;
99
105
} ) ) ;
100
106
@@ -114,6 +120,7 @@ describe('FocusOriginMonitor', () => {
114
120
. toBe ( true , 'button should have cdk-focused class' ) ;
115
121
expect ( buttonElement . classList . contains ( 'cdk-program-focused' ) )
116
122
. toBe ( true , 'button should have cdk-program-focused class' ) ;
123
+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'program' ) ;
117
124
} , 0 ) ;
118
125
} ) ) ;
119
126
@@ -132,6 +139,7 @@ describe('FocusOriginMonitor', () => {
132
139
. toBe ( true , 'button should have cdk-focused class' ) ;
133
140
expect ( buttonElement . classList . contains ( 'cdk-keyboard-focused' ) )
134
141
. toBe ( true , 'button should have cdk-keyboard-focused class' ) ;
142
+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'keyboard' ) ;
135
143
} , 0 ) ;
136
144
} ) ) ;
137
145
@@ -150,6 +158,7 @@ describe('FocusOriginMonitor', () => {
150
158
. toBe ( true , 'button should have cdk-focused class' ) ;
151
159
expect ( buttonElement . classList . contains ( 'cdk-mouse-focused' ) )
152
160
. toBe ( true , 'button should have cdk-mouse-focused class' ) ;
161
+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'mouse' ) ;
153
162
} , 0 ) ;
154
163
} ) ) ;
155
164
@@ -168,6 +177,7 @@ describe('FocusOriginMonitor', () => {
168
177
. toBe ( true , 'button should have cdk-focused class' ) ;
169
178
expect ( buttonElement . classList . contains ( 'cdk-program-focused' ) )
170
179
. toBe ( true , 'button should have cdk-program-focused class' ) ;
180
+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'program' ) ;
171
181
} , 0 ) ;
172
182
} ) ) ;
173
183
} ) ;
@@ -177,6 +187,7 @@ describe('cdkFocusClasses', () => {
177
187
let fixture : ComponentFixture < ButtonWithFocusClasses > ;
178
188
let buttonElement : HTMLElement ;
179
189
let platform : Platform ;
190
+ let changeHandler : ( origin : FocusOrigin ) => void ;
180
191
181
192
beforeEach ( async ( ( ) => {
182
193
TestBed . configureTestingModule ( {
@@ -193,6 +204,8 @@ describe('cdkFocusClasses', () => {
193
204
fixture = TestBed . createComponent ( ButtonWithFocusClasses ) ;
194
205
fixture . detectChanges ( ) ;
195
206
207
+ changeHandler = jasmine . createSpy ( 'focus origin change handler' ) ;
208
+ fixture . componentInstance . cdkFocusClasses . changes . subscribe ( changeHandler ) ;
196
209
buttonElement = fixture . debugElement . query ( By . css ( 'button' ) ) . nativeElement ;
197
210
platform = pfm ;
198
211
} ) ) ;
@@ -218,6 +231,7 @@ describe('cdkFocusClasses', () => {
218
231
. toBe ( true , 'button should have cdk-focused class' ) ;
219
232
expect ( buttonElement . classList . contains ( 'cdk-keyboard-focused' ) )
220
233
. toBe ( true , 'button should have cdk-keyboard-focused class' ) ;
234
+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'keyboard' ) ;
221
235
} , 0 ) ;
222
236
} ) ) ;
223
237
@@ -238,6 +252,7 @@ describe('cdkFocusClasses', () => {
238
252
. toBe ( true , 'button should have cdk-focused class' ) ;
239
253
expect ( buttonElement . classList . contains ( 'cdk-mouse-focused' ) )
240
254
. toBe ( true , 'button should have cdk-mouse-focused class' ) ;
255
+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'mouse' ) ;
241
256
} , 0 ) ;
242
257
} ) ) ;
243
258
@@ -257,6 +272,7 @@ describe('cdkFocusClasses', () => {
257
272
. toBe ( true , 'button should have cdk-focused class' ) ;
258
273
expect ( buttonElement . classList . contains ( 'cdk-program-focused' ) )
259
274
. toBe ( true , 'button should have cdk-program-focused class' ) ;
275
+ expect ( changeHandler ) . toHaveBeenCalledWith ( 'program' ) ;
260
276
} , 0 ) ;
261
277
} ) ) ;
262
278
} ) ;
@@ -269,7 +285,9 @@ class PlainButton {
269
285
270
286
271
287
@Component ( { template : `<button cdkFocusClasses>focus me!</button>` } )
272
- class ButtonWithFocusClasses { }
288
+ class ButtonWithFocusClasses {
289
+ @ViewChild ( CdkFocusClasses ) cdkFocusClasses : CdkFocusClasses ;
290
+ }
273
291
274
292
275
293
/** Dispatches a mousedown event on the specified element. */
0 commit comments