3
3
inject ,
4
4
async ,
5
5
fakeAsync ,
6
- flushMicrotasks ,
7
- tick
6
+ flushMicrotasks
8
7
} from '@angular/core/testing' ;
9
8
import {
10
9
FORM_DIRECTIVES ,
@@ -214,13 +213,47 @@ describe('MdCheckbox', () => {
214
213
expect ( testComponent . onCheckboxClick ) . toHaveBeenCalledTimes ( 1 ) ;
215
214
} ) ;
216
215
217
- it ( 'should emit a change event when the `checked` value changes' , async ( ( ) => {
216
+ it ( 'should trigger a change event when the native input does' , async ( ( ) => {
217
+ spyOn ( testComponent , 'onCheckboxChange' ) ;
218
+
219
+ expect ( inputElement . checked ) . toBe ( false ) ;
220
+ expect ( checkboxNativeElement . classList ) . not . toContain ( 'md-checkbox-checked' ) ;
221
+
222
+ labelElement . click ( ) ;
223
+ fixture . detectChanges ( ) ;
224
+
225
+ expect ( inputElement . checked ) . toBe ( true ) ;
226
+ expect ( checkboxNativeElement . classList ) . toContain ( 'md-checkbox-checked' ) ;
227
+
228
+ // Wait for the fixture to become stable, because the EventEmitter for the change event,
229
+ // will only fire after the zone async change detection has finished.
230
+ fixture . whenStable ( ) . then ( ( ) => {
231
+ // The change event shouldn't fire, because the value change was not caused
232
+ // by any interaction.
233
+ expect ( testComponent . onCheckboxChange ) . toHaveBeenCalledTimes ( 1 ) ;
234
+ } ) ;
235
+ } ) ) ;
236
+
237
+ it ( 'should not trigger the change event by changing the native value' , async ( ( ) => {
238
+ spyOn ( testComponent , 'onCheckboxChange' ) ;
239
+
240
+ expect ( inputElement . checked ) . toBe ( false ) ;
241
+ expect ( checkboxNativeElement . classList ) . not . toContain ( 'md-checkbox-checked' ) ;
242
+
218
243
testComponent . isChecked = true ;
219
244
fixture . detectChanges ( ) ;
220
245
246
+ expect ( inputElement . checked ) . toBe ( true ) ;
247
+ expect ( checkboxNativeElement . classList ) . toContain ( 'md-checkbox-checked' ) ;
248
+
249
+ // Wait for the fixture to become stable, because the EventEmitter for the change event,
250
+ // will only fire after the zone async change detection has finished.
221
251
fixture . whenStable ( ) . then ( ( ) => {
222
- expect ( fixture . componentInstance . changeCount ) . toBe ( 1 ) ;
252
+ // The change event shouldn't fire, because the value change was not caused
253
+ // by any interaction.
254
+ expect ( testComponent . onCheckboxChange ) . not . toHaveBeenCalled ( ) ;
223
255
} ) ;
256
+
224
257
} ) ) ;
225
258
226
259
describe ( 'state transition css classes' , ( ) => {
@@ -300,17 +333,21 @@ describe('MdCheckbox', () => {
300
333
} ) ;
301
334
} ) ) ;
302
335
303
- it ( 'should call the change event on first change after initialization' , fakeAsync ( ( ) => {
304
- fixture . detectChanges ( ) ;
305
- expect ( testComponent . lastEvent ) . toBeUndefined ( ) ;
336
+ it ( 'should emit the event to the change observable' , ( ) => {
337
+ let changeSpy = jasmine . createSpy ( 'onChangeObservable' ) ;
338
+
339
+ checkboxInstance . change . subscribe ( changeSpy ) ;
306
340
307
- checkboxInstance . checked = true ;
308
341
fixture . detectChanges ( ) ;
342
+ expect ( changeSpy ) . not . toHaveBeenCalled ( ) ;
309
343
310
- tick ( ) ;
344
+ // When changing the native `checked` property the checkbox will not fire a change event,
345
+ // because the element is not focused and it's not the native behavior of the input element.
346
+ labelElement . click ( ) ;
347
+ fixture . detectChanges ( ) ;
311
348
312
- expect ( testComponent . lastEvent . checked ) . toBe ( true ) ;
313
- } ) ) ;
349
+ expect ( changeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
350
+ } ) ;
314
351
315
352
it ( 'should not emit a DOM event to the change output' , async ( ( ) => {
316
353
fixture . detectChanges ( ) ;
@@ -488,7 +525,8 @@ describe('MdCheckbox', () => {
488
525
[indeterminate]="isIndeterminate"
489
526
[disabled]="isDisabled"
490
527
(change)="changeCount = changeCount + 1"
491
- (click)="onCheckboxClick($event)">
528
+ (click)="onCheckboxClick($event)"
529
+ (change)="onCheckboxChange($event)">
492
530
Simple checkbox
493
531
</md-checkbox>
494
532
</div>`
@@ -504,6 +542,7 @@ class SingleCheckbox {
504
542
changeCount : number = 0 ;
505
543
506
544
onCheckboxClick ( event : Event ) { }
545
+ onCheckboxChange ( event : MdCheckboxChange ) { }
507
546
}
508
547
509
548
/** Simple component for testing an MdCheckbox with ngModel. */
0 commit comments