33 inject ,
44 async ,
55 fakeAsync ,
6- flushMicrotasks ,
7- tick
6+ flushMicrotasks
87} from '@angular/core/testing' ;
98import {
109 FORM_DIRECTIVES ,
@@ -214,13 +213,47 @@ describe('MdCheckbox', () => {
214213 expect ( testComponent . onCheckboxClick ) . toHaveBeenCalledTimes ( 1 ) ;
215214 } ) ;
216215
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+
218243 testComponent . isChecked = true ;
219244 fixture . detectChanges ( ) ;
220245
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.
221251 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 ( ) ;
223255 } ) ;
256+
224257 } ) ) ;
225258
226259 describe ( 'state transition css classes' , ( ) => {
@@ -300,17 +333,21 @@ describe('MdCheckbox', () => {
300333 } ) ;
301334 } ) ) ;
302335
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 ) ;
306340
307- checkboxInstance . checked = true ;
308341 fixture . detectChanges ( ) ;
342+ expect ( changeSpy ) . not . toHaveBeenCalled ( ) ;
309343
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 ( ) ;
311348
312- expect ( testComponent . lastEvent . checked ) . toBe ( true ) ;
313- } ) ) ;
349+ expect ( changeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
350+ } ) ;
314351
315352 it ( 'should not emit a DOM event to the change output' , async ( ( ) => {
316353 fixture . detectChanges ( ) ;
@@ -488,7 +525,8 @@ describe('MdCheckbox', () => {
488525 [indeterminate]="isIndeterminate"
489526 [disabled]="isDisabled"
490527 (change)="changeCount = changeCount + 1"
491- (click)="onCheckboxClick($event)">
528+ (click)="onCheckboxClick($event)"
529+ (change)="onCheckboxChange($event)">
492530 Simple checkbox
493531 </md-checkbox>
494532 </div>`
@@ -504,6 +542,7 @@ class SingleCheckbox {
504542 changeCount : number = 0 ;
505543
506544 onCheckboxClick ( event : Event ) { }
545+ onCheckboxChange ( event : MdCheckboxChange ) { }
507546}
508547
509548/** Simple component for testing an MdCheckbox with ngModel. */
0 commit comments