Skip to content

fix(slide-toggle): report change to model before firing a change event #7076

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/lib/slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ describe('MdSlideToggle with forms', () => {
declarations: [
SlideToggleWithForm,
SlideToggleWithModel,
SlideToggleWithFormControl
SlideToggleWithFormControl,
SlideToggleWithModelAndChangeEvent,
]
});

Expand Down Expand Up @@ -795,6 +796,24 @@ describe('MdSlideToggle with forms', () => {
expect(testComponent.isSubmitted).toBe(true);
});
});

describe('with model and change event', () => {
it('should report changes to NgModel before emitting change event', () => {
const fixture = TestBed.createComponent(SlideToggleWithModelAndChangeEvent);
fixture.detectChanges();

const labelEl = fixture.debugElement.query(By.css('label')).nativeElement;

spyOn(fixture.componentInstance, 'onChange').and.callFake(() => {
expect(fixture.componentInstance.checked)
.toBe(true, 'Expected the model value to have changed before the change event fired.');
});

labelEl.click();

expect(fixture.componentInstance.onChange).toHaveBeenCalledTimes(1);
});
});
});

@Component({
Expand Down Expand Up @@ -873,3 +892,11 @@ class SlideToggleWithTabindexAttr {}
class SlideToggleWithoutLabel {
label: string;
}

@Component({
template: `<md-slide-toggle [(ngModel)]="checked" (change)="onChange()"></md-slide-toggle>`
})
class SlideToggleWithModelAndChangeEvent {
checked: boolean;
onChange: () => void = () => {};
}
2 changes: 1 addition & 1 deletion src/lib/slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase implements OnDestroy,
let event = new MdSlideToggleChange();
event.source = this;
event.checked = this.checked;
this.change.emit(event);
this.onChange(this.checked);
this.change.emit(event);
}

_onDragStart() {
Expand Down