|
1 |
| -import {it, beforeEach, inject, async, fakeAsync, flushMicrotasks} from '@angular/core/testing'; |
| 1 | +import { |
| 2 | + it, |
| 3 | + beforeEach, |
| 4 | + inject, |
| 5 | + async, |
| 6 | + fakeAsync, |
| 7 | + flushMicrotasks, |
| 8 | + tick |
| 9 | +} from '@angular/core/testing'; |
2 | 10 | import {FORM_DIRECTIVES, NgModel, NgControl} from '@angular/common';
|
3 | 11 | import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
|
4 | 12 | import {Component, DebugElement} from '@angular/core';
|
5 | 13 | import {By} from '@angular/platform-browser';
|
6 | 14 | import {MdCheckbox} from './checkbox';
|
7 | 15 | import {PromiseCompleter} from '@angular2-material/core/async/promise-completer';
|
8 | 16 |
|
| 17 | + |
| 18 | + |
9 | 19 | // TODO: Implement E2E tests for spacebar/click behavior for checking/unchecking
|
10 | 20 |
|
11 | 21 | describe('MdCheckbox', () => {
|
@@ -244,6 +254,42 @@ describe('MdCheckbox', () => {
|
244 | 254 | });
|
245 | 255 | });
|
246 | 256 |
|
| 257 | + describe('with change event and no initial value', () => { |
| 258 | + let checkboxDebugElement: DebugElement; |
| 259 | + let checkboxNativeElement: HTMLElement; |
| 260 | + let checkboxInstance: MdCheckbox; |
| 261 | + let testComponent: CheckboxWithChangeEvent; |
| 262 | + let inputElement: HTMLInputElement; |
| 263 | + let labelElement: HTMLLabelElement; |
| 264 | + |
| 265 | + beforeEach(async(() => { |
| 266 | + builder.createAsync(CheckboxWithChangeEvent).then(f => { |
| 267 | + fixture = f; |
| 268 | + fixture.detectChanges(); |
| 269 | + |
| 270 | + checkboxDebugElement = fixture.debugElement.query(By.directive(MdCheckbox)); |
| 271 | + checkboxNativeElement = checkboxDebugElement.nativeElement; |
| 272 | + checkboxInstance = checkboxDebugElement.componentInstance; |
| 273 | + testComponent = fixture.debugElement.componentInstance; |
| 274 | + inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input'); |
| 275 | + labelElement = <HTMLLabelElement>checkboxNativeElement.querySelector('label'); |
| 276 | + |
| 277 | + spyOn(testComponent, 'handleChange'); |
| 278 | + }); |
| 279 | + })); |
| 280 | + |
| 281 | + it('should call the change event on first change after initialization', fakeAsync(() => { |
| 282 | + fixture.detectChanges(); |
| 283 | + expect(testComponent.handleChange).not.toHaveBeenCalled(); |
| 284 | + |
| 285 | + checkboxInstance.checked = true; |
| 286 | + fixture.detectChanges(); |
| 287 | + |
| 288 | + tick(); |
| 289 | + expect(testComponent.handleChange).toHaveBeenCalled(); |
| 290 | + })); |
| 291 | + }); |
| 292 | + |
247 | 293 | describe('with provided aria-label ', () => {
|
248 | 294 | let checkboxDebugElement: DebugElement;
|
249 | 295 | let checkboxNativeElement: HTMLElement;
|
@@ -471,3 +517,12 @@ class CheckboxWithAriaLabelledby {}
|
471 | 517 | template: `<md-checkbox name="test-name"></md-checkbox>`
|
472 | 518 | })
|
473 | 519 | class CheckboxWithNameAttribute {}
|
| 520 | + |
| 521 | +/** Simple test component with change event */ |
| 522 | +@Component({ |
| 523 | + directives: [MdCheckbox], |
| 524 | + template: `<md-checkbox (change)="handleChange()"></md-checkbox>` |
| 525 | +}) |
| 526 | +class CheckboxWithChangeEvent { |
| 527 | + handleChange(): void {} |
| 528 | +} |
0 commit comments