Skip to content

Commit

Permalink
fix(checkbox): model value not updated when using toggle method
Browse files Browse the repository at this point in the history
Along the same lines as #11812. The checkbox doesn't update its `ControlValueAccessor` value when it is toggled via the `toggle` method.
  • Loading branch information
crisbeto committed Jun 19, 2019
1 parent 4b82786 commit da25796
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/material/checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ describe('MatCheckbox', () => {
let inputElement: HTMLInputElement;
let ngModel: NgModel;

beforeEach(() => {
beforeEach(fakeAsync(() => {
fixture = createComponent(CheckboxWithNgModel);

fixture.componentInstance.isRequired = false;
Expand All @@ -885,7 +885,7 @@ describe('MatCheckbox', () => {
checkboxInstance = checkboxDebugElement.componentInstance;
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
ngModel = checkboxDebugElement.injector.get<NgModel>(NgModel);
});
}));

it('should be pristine, untouched, and valid initially', () => {
expect(ngModel.valid).toBe(true);
Expand Down Expand Up @@ -984,6 +984,18 @@ describe('MatCheckbox', () => {
expect(checkboxInstance.checked).toBe(false);
expect(ngModel.valid).toBe(false);
});

it('should updated the ngModel value when using the `toggle` method', fakeAsync(() => {
const checkbox = fixture.debugElement.query(By.directive(MatCheckbox)).componentInstance;

expect(fixture.componentInstance.isGood).toBe(false);

checkbox.toggle();
fixture.detectChanges();

expect(fixture.componentInstance.isGood).toBe(true);
}));

});

describe('with name attribute', () => {
Expand Down
1 change: 1 addition & 0 deletions src/material/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
/** Toggles the `checked` state of the checkbox. */
toggle(): void {
this.checked = !this.checked;
this._controlValueAccessorChangeFn(this.checked);
}

/**
Expand Down

0 comments on commit da25796

Please sign in to comment.