Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(slide-toggle): not updating model from toggle method #11846

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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