Skip to content

Commit

Permalink
fix(slide-toggle): not updating model or dispatching change event fro…
Browse files Browse the repository at this point in the history
…m toggle method

Fixes the slide toggle not propagating its value to the `ControlValueAccessor` or dispatching its change event when its value is changed through the `toggle` method. It seems like we had a test for one of these cases, but it wasn't failing when it was supposed to.

Fixes #11812.
  • Loading branch information
crisbeto committed Jun 19, 2018
1 parent f31b2f4 commit 4102f25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/lib/slide-toggle/slide-toggle.spec.ts
Expand Up @@ -49,7 +49,6 @@ describe('MatSlideToggle without forms', () => {
let labelElement: HTMLLabelElement;
let inputElement: HTMLInputElement;

// This initialization is async() because it needs to wait for ngModel to set the initial value.
beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(SlideToggleBasic);

Expand Down Expand Up @@ -252,12 +251,14 @@ describe('MatSlideToggle without forms', () => {
}));

it('should support subscription on the change observable', () => {
slideToggle.change.subscribe((event: MatSlideToggleChange) => {
expect(event.checked).toBe(true);
});
const spy = jasmine.createSpy('change spy');
const subscription = slideToggle.change.subscribe(spy);

slideToggle.toggle();
fixture.detectChanges();

expect(spy).toHaveBeenCalledWith(jasmine.objectContaining({checked: true}));
subscription.unsubscribe();
});

it('should show a ripple when focused by a keyboard action', fakeAsync(() => {
Expand Down Expand Up @@ -739,6 +740,18 @@ describe('MatSlideToggle with forms', () => {

expect(modelInstance.pristine).toBe(true);
}));

it('should set the model value when toggling via the `toggle` method', fakeAsync(() => {
expect(testComponent.modelValue).toBe(false);

fixture.debugElement.query(By.directive(MatSlideToggle)).componentInstance.toggle();
fixture.detectChanges();
flushMicrotasks();

fixture.detectChanges();
expect(testComponent.modelValue).toBe(true);
}));

});

describe('with a FormControl', () => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/slide-toggle/slide-toggle.ts
Expand Up @@ -252,6 +252,7 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
/** Toggles the checked state of the slide-toggle. */
toggle(): void {
this.checked = !this.checked;
this._emitChangeEvent();
}

/** Function is called whenever the focus changes for the input element. */
Expand Down

0 comments on commit 4102f25

Please sign in to comment.