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): invalid change events with no new value #3555

Merged
merged 1 commit into from
Mar 14, 2017
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
16 changes: 16 additions & 0 deletions src/lib/slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,22 @@ describe('MdSlideToggle', () => {
expect(testComponent.lastEvent.checked).toBe(true);
}));

it('should not emit a change event when the value did not change', fakeAsync(() => {
expect(slideToggle.checked).toBe(false);

gestureConfig.emitEventForElement('slidestart', slideThumbContainer);
gestureConfig.emitEventForElement('slide', slideThumbContainer, { deltaX: 0 });
gestureConfig.emitEventForElement('slideend', slideThumbContainer);

// Flush the timeout for the slide ending.
tick();

expect(slideThumbContainer.classList).not.toContain('mat-dragging');
expect(slideToggle.checked).toBe(false);
expect(testComponent.lastEvent)
.toBeFalsy('Expected the slide-toggle to not emit a change event.');
}));

it('should update the checked property of the input', fakeAsync(() => {
expect(inputElement.checked).toBe(false);

Expand Down
6 changes: 5 additions & 1 deletion src/lib/slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,12 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor {

_onDragEnd() {
if (this._slideRenderer.dragging) {
let _previousChecked = this.checked;
this.checked = this._slideRenderer.dragPercentage > 50;
this._emitChangeEvent();

if (_previousChecked !== this.checked) {
this._emitChangeEvent();
}

// The drag should be stopped outside of the current event handler, because otherwise the
// click event will be fired before and will revert the drag change.
Expand Down