Skip to content

Commit 461ecc9

Browse files
committed
tests: add tests for button toggle change events
1 parent 6f21d10 commit 461ecc9

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

src/components/button-toggle/button-toggle.spec.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ describe('MdButtonToggle', () => {
151151
tick();
152152

153153
// The default browser behavior is to not emit a change event, when the value was set
154-
// to false. That's why the change event was only triggered once.
154+
// to false. That's because the current input type is set to `radio`
155155
expect(changeSpy).toHaveBeenCalledTimes(1);
156156
}));
157157

@@ -341,6 +341,7 @@ describe('MdButtonToggle', () => {
341341
let groupNativeElement: HTMLElement;
342342
let buttonToggleDebugElements: DebugElement[];
343343
let buttonToggleNativeElements: HTMLElement[];
344+
let buttonToggleLabelElements: HTMLLabelElement[];
344345
let groupInstance: MdButtonToggleGroupMultiple;
345346
let buttonToggleInstances: MdButtonToggle[];
346347
let testComponent: ButtonTogglesInsideButtonToggleGroupMultiple;
@@ -357,8 +358,10 @@ describe('MdButtonToggle', () => {
357358
groupInstance = groupDebugElement.injector.get(MdButtonToggleGroupMultiple);
358359

359360
buttonToggleDebugElements = fixture.debugElement.queryAll(By.directive(MdButtonToggle));
360-
buttonToggleNativeElements =
361-
buttonToggleDebugElements.map(debugEl => debugEl.nativeElement);
361+
buttonToggleNativeElements = buttonToggleDebugElements
362+
.map(debugEl => debugEl.nativeElement);
363+
buttonToggleLabelElements = fixture.debugElement.queryAll(By.css('label'))
364+
.map(debugEl => debugEl.nativeElement);
362365
buttonToggleInstances = buttonToggleDebugElements.map(debugEl => debugEl.componentInstance);
363366
});
364367
}));
@@ -409,12 +412,36 @@ describe('MdButtonToggle', () => {
409412

410413
expect(buttonToggleInstances[0].checked).toBe(false);
411414
});
415+
416+
it('should emit a change event for state changes', fakeAsync(() => {
417+
418+
expect(buttonToggleInstances[0].checked).toBe(false);
419+
420+
let changeSpy = jasmine.createSpy('button-toggle change listener');
421+
buttonToggleInstances[0].change.subscribe(changeSpy);
422+
423+
buttonToggleLabelElements[0].click();
424+
fixture.detectChanges();
425+
tick();
426+
expect(changeSpy).toHaveBeenCalled();
427+
428+
buttonToggleLabelElements[0].click();
429+
fixture.detectChanges();
430+
tick();
431+
432+
// The default browser behavior is to emit an event, when the value was set
433+
// to false. That's because the current input type is set to `checkbox` when
434+
// using the multiple mode.
435+
expect(changeSpy).toHaveBeenCalledTimes(2);
436+
}));
437+
412438
});
413439

414440
describe('as standalone', () => {
415441
let fixture: ComponentFixture<StandaloneButtonToggle>;
416442
let buttonToggleDebugElement: DebugElement;
417443
let buttonToggleNativeElement: HTMLElement;
444+
let buttonToggleLabelElement: HTMLLabelElement;
418445
let buttonToggleInstance: MdButtonToggle;
419446
let testComponent: StandaloneButtonToggle;
420447

@@ -427,24 +454,45 @@ describe('MdButtonToggle', () => {
427454

428455
buttonToggleDebugElement = fixture.debugElement.query(By.directive(MdButtonToggle));
429456
buttonToggleNativeElement = buttonToggleDebugElement.nativeElement;
457+
buttonToggleLabelElement = fixture.debugElement.query(By.css('label')).nativeElement;
430458
buttonToggleInstance = buttonToggleDebugElement.componentInstance;
431459
});
432460
}));
433461

434462
it('should toggle when clicked', () => {
435-
let nativeCheckboxLabel = buttonToggleDebugElement.query(By.css('label')).nativeElement;
436-
437-
nativeCheckboxLabel.click();
463+
buttonToggleLabelElement.click();
438464

439465
fixture.detectChanges();
440466

441467
expect(buttonToggleInstance.checked).toBe(true);
442468

443-
nativeCheckboxLabel.click();
469+
buttonToggleLabelElement.click();
444470
fixture.detectChanges();
445471

446472
expect(buttonToggleInstance.checked).toBe(false);
447473
});
474+
475+
it('should emit a change event for state changes', fakeAsync(() => {
476+
477+
expect(buttonToggleInstance.checked).toBe(false);
478+
479+
let changeSpy = jasmine.createSpy('button-toggle change listener');
480+
buttonToggleInstance.change.subscribe(changeSpy);
481+
482+
buttonToggleLabelElement.click();
483+
fixture.detectChanges();
484+
tick();
485+
expect(changeSpy).toHaveBeenCalled();
486+
487+
buttonToggleLabelElement.click();
488+
fixture.detectChanges();
489+
tick();
490+
491+
// The default browser behavior is to emit an event, when the value was set
492+
// to false. That's because the current input type is set to `checkbox`.
493+
expect(changeSpy).toHaveBeenCalledTimes(2);
494+
}));
495+
448496
});
449497
});
450498

src/components/button-toggle/button-toggle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ export class MdButtonToggle implements OnInit {
369369
this._emitChangeEvent();
370370
}
371371

372+
/** TODO: internal */
372373
_onInputClick(event: Event) {
373374

374375
// We have to stop propagation for click events on the visual hidden input element.

src/components/radio/radio.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
inject,
33
async,
44
fakeAsync,
5-
tick,
65
TestComponentBuilder,
76
ComponentFixture,
87
TestBed,

0 commit comments

Comments
 (0)