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 23, 2018
1 parent c276e26 commit ba4fe2f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/lib/checkbox/checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,15 +877,15 @@ describe('MatCheckbox', () => {
let checkboxInstance: MatCheckbox;
let inputElement: HTMLInputElement;

beforeEach(() => {
beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(CheckboxWithFormDirectives);
fixture.detectChanges();

checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
checkboxNativeElement = checkboxDebugElement.nativeElement;
checkboxInstance = checkboxDebugElement.componentInstance;
inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
});
}));

it('should be in pristine, untouched, and valid states initially', fakeAsync(() => {
flush();
Expand Down Expand Up @@ -914,6 +914,17 @@ describe('MatCheckbox', () => {

expect(checkboxInstance.checked).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 required ngModel', () => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,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 ba4fe2f

Please sign in to comment.