Skip to content

Commit ffa622a

Browse files
jelbournandrewseguin
authored andcommitted
chore(checkbox): add test for click + ngModel (#2738)
1 parent 5b76989 commit ffa622a

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/lib/checkbox/checkbox.spec.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
import {
2-
async,
3-
fakeAsync,
4-
flushMicrotasks,
5-
ComponentFixture,
6-
TestBed,
7-
} from '@angular/core/testing';
8-
import {
9-
NgControl,
10-
FormsModule,
11-
ReactiveFormsModule,
12-
FormControl,
13-
} from '@angular/forms';
1+
import {async, fakeAsync, flushMicrotasks, ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {NgControl, FormsModule, ReactiveFormsModule, FormControl} from '@angular/forms';
143
import {Component, DebugElement} from '@angular/core';
154
import {By} from '@angular/platform-browser';
165
import {MdCheckbox, MdCheckboxChange, MdCheckboxModule} from './checkbox';
176
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
187
import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
198

209

21-
2210
describe('MdCheckbox', () => {
2311
let fixture: ComponentFixture<any>;
2412

@@ -586,9 +574,19 @@ describe('MdCheckbox', () => {
586574
});
587575

588576
describe('with ngModel', () => {
577+
let checkboxDebugElement: DebugElement;
578+
let checkboxNativeElement: HTMLElement;
579+
let checkboxInstance: MdCheckbox;
580+
let inputElement: HTMLInputElement;
581+
589582
beforeEach(() => {
590583
fixture = TestBed.createComponent(CheckboxWithFormDirectives);
591584
fixture.detectChanges();
585+
586+
checkboxDebugElement = fixture.debugElement.query(By.directive(MdCheckbox));
587+
checkboxNativeElement = checkboxDebugElement.nativeElement;
588+
checkboxInstance = checkboxDebugElement.componentInstance;
589+
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
592590
});
593591

594592
it('should be in pristine, untouched, and valid states initially', fakeAsync(() => {
@@ -604,6 +602,20 @@ describe('MdCheckbox', () => {
604602
// TODO(jelbourn): test that `touched` and `pristine` state are modified appropriately.
605603
// This is currently blocked on issues with async() and fakeAsync().
606604
}));
605+
606+
it('should toggle checked state on click', () => {
607+
expect(checkboxInstance.checked).toBe(false);
608+
609+
inputElement.click();
610+
fixture.detectChanges();
611+
612+
expect(checkboxInstance.checked).toBe(true);
613+
614+
inputElement.click();
615+
fixture.detectChanges();
616+
617+
expect(checkboxInstance.checked).toBe(false);
618+
});
607619
});
608620

609621
describe('with name attribute', () => {

0 commit comments

Comments
 (0)