From 7a9df1eddc8e04c58d301b9e2c701a1a4689daef Mon Sep 17 00:00:00 2001 From: Robert Messerle Date: Wed, 25 May 2016 12:17:29 -0700 Subject: [PATCH] fix(checkbox): change event should now fire on first change closes #481 --- src/components/checkbox/checkbox.spec.ts | 57 +++++++++++++++++++++++- src/components/checkbox/checkbox.ts | 2 + 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/components/checkbox/checkbox.spec.ts b/src/components/checkbox/checkbox.spec.ts index 7cb9ae712044..90ce7e8ee014 100644 --- a/src/components/checkbox/checkbox.spec.ts +++ b/src/components/checkbox/checkbox.spec.ts @@ -1,4 +1,12 @@ -import {it, beforeEach, inject, async, fakeAsync, flushMicrotasks} from '@angular/core/testing'; +import { + it, + beforeEach, + inject, + async, + fakeAsync, + flushMicrotasks, + tick +} from '@angular/core/testing'; import {FORM_DIRECTIVES, NgModel, NgControl} from '@angular/common'; import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing'; import {Component, DebugElement} from '@angular/core'; @@ -6,6 +14,8 @@ import {By} from '@angular/platform-browser'; import {MdCheckbox} from './checkbox'; import {PromiseCompleter} from '@angular2-material/core/async/promise-completer'; + + // TODO: Implement E2E tests for spacebar/click behavior for checking/unchecking describe('MdCheckbox', () => { @@ -244,6 +254,42 @@ describe('MdCheckbox', () => { }); }); + describe('with change event and no initial value', () => { + let checkboxDebugElement: DebugElement; + let checkboxNativeElement: HTMLElement; + let checkboxInstance: MdCheckbox; + let testComponent: CheckboxWithChangeEvent; + let inputElement: HTMLInputElement; + let labelElement: HTMLLabelElement; + + beforeEach(async(() => { + builder.createAsync(CheckboxWithChangeEvent).then(f => { + fixture = f; + fixture.detectChanges(); + + checkboxDebugElement = fixture.debugElement.query(By.directive(MdCheckbox)); + checkboxNativeElement = checkboxDebugElement.nativeElement; + checkboxInstance = checkboxDebugElement.componentInstance; + testComponent = fixture.debugElement.componentInstance; + inputElement = checkboxNativeElement.querySelector('input'); + labelElement = checkboxNativeElement.querySelector('label'); + + spyOn(testComponent, 'handleChange'); + }); + })); + + it('should call the change event on first change after initialization', fakeAsync(() => { + fixture.detectChanges(); + expect(testComponent.handleChange).not.toHaveBeenCalled(); + + checkboxInstance.checked = true; + fixture.detectChanges(); + + tick(); + expect(testComponent.handleChange).toHaveBeenCalled(); + })); + }); + describe('with provided aria-label ', () => { let checkboxDebugElement: DebugElement; let checkboxNativeElement: HTMLElement; @@ -471,3 +517,12 @@ class CheckboxWithAriaLabelledby {} template: `` }) class CheckboxWithNameAttribute {} + +/** Simple test component with change event */ +@Component({ + directives: [MdCheckbox], + template: `` +}) +class CheckboxWithChangeEvent { + handleChange(): void {} +} diff --git a/src/components/checkbox/checkbox.ts b/src/components/checkbox/checkbox.ts index 3dd9b3f86f60..45da46e5adb8 100644 --- a/src/components/checkbox/checkbox.ts +++ b/src/components/checkbox/checkbox.ts @@ -149,7 +149,9 @@ export class MdCheckbox implements ControlValueAccessor { this.change.emit(this._checked); } } + } + ngAfterContentInit() { this._isInitialized = true; }