Skip to content

Commit

Permalink
fix(slide-toggle): not updating model from toggle method (#11846)
Browse files Browse the repository at this point in the history
  • Loading branch information
crisbeto authored and josephperrott committed Jun 28, 2018
1 parent 42dd824 commit e69cf76
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
28 changes: 21 additions & 7 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 @@ -251,14 +250,17 @@ describe('MatSlideToggle without forms', () => {
expect(testComponent.lastEvent.checked).toBe(true);
}));

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

slideToggle.toggle();
labelElement.click();
fixture.detectChanges();
});
tick();

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

it('should show a ripple when focused by a keyboard action', fakeAsync(() => {
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length)
Expand Down Expand Up @@ -739,6 +741,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.onChange(this.checked);
}

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

0 comments on commit e69cf76

Please sign in to comment.