|
6 | 6 | flushMicrotasks,
|
7 | 7 | } from '@angular/core/testing';
|
8 | 8 | import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
|
9 |
| -import {Component, DebugElement, ViewChild, Type} from '@angular/core'; |
| 9 | +import {Component, DebugElement, ViewChild, Type, ChangeDetectionStrategy} from '@angular/core'; |
10 | 10 | import {By} from '@angular/platform-browser';
|
11 | 11 | import {dispatchFakeEvent} from '@angular/cdk/testing';
|
12 | 12 | import {MatCheckbox, MatCheckboxChange, MatCheckboxModule} from './index';
|
@@ -689,7 +689,7 @@ describe('MatCheckbox', () => {
|
689 | 689 | }));
|
690 | 690 | });
|
691 | 691 |
|
692 |
| - describe('aria-label ', () => { |
| 692 | + describe('aria-label', () => { |
693 | 693 | let checkboxDebugElement: DebugElement;
|
694 | 694 | let checkboxNativeElement: HTMLElement;
|
695 | 695 | let inputElement: HTMLInputElement;
|
@@ -916,6 +916,31 @@ describe('MatCheckbox', () => {
|
916 | 916 | expect(ngModel.touched).toBe(true);
|
917 | 917 | }));
|
918 | 918 |
|
| 919 | + it('should mark the element as touched on blur when inside an OnPush parent', fakeAsync(() => { |
| 920 | + fixture.destroy(); |
| 921 | + TestBed.resetTestingModule(); |
| 922 | + fixture = createComponent(CheckboxWithNgModelAndOnPush); |
| 923 | + checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox)); |
| 924 | + checkboxNativeElement = checkboxDebugElement.nativeElement; |
| 925 | + checkboxInstance = checkboxDebugElement.componentInstance; |
| 926 | + inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input'); |
| 927 | + ngModel = checkboxDebugElement.injector.get<NgModel>(NgModel); |
| 928 | + |
| 929 | + inputElement.click(); |
| 930 | + fixture.detectChanges(); |
| 931 | + flush(); |
| 932 | + |
| 933 | + expect(checkboxNativeElement.classList).not.toContain('ng-touched'); |
| 934 | + |
| 935 | + dispatchFakeEvent(inputElement, 'blur'); |
| 936 | + fixture.detectChanges(); |
| 937 | + flushMicrotasks(); |
| 938 | + fixture.detectChanges(); |
| 939 | + |
| 940 | + expect(checkboxNativeElement.classList).toContain('ng-touched'); |
| 941 | + })); |
| 942 | + |
| 943 | + |
919 | 944 | it('should not throw an error when disabling while focused', fakeAsync(() => {
|
920 | 945 | expect(() => {
|
921 | 946 | // Focus the input element because after disabling, the `blur` event should automatically
|
@@ -1176,6 +1201,12 @@ class CheckboxWithNgModel {
|
1176 | 1201 | isRequired: boolean = true;
|
1177 | 1202 | }
|
1178 | 1203 |
|
| 1204 | +@Component({ |
| 1205 | + template: `<mat-checkbox [required]="isRequired" [(ngModel)]="isGood">Be good</mat-checkbox>`, |
| 1206 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 1207 | +}) |
| 1208 | +class CheckboxWithNgModelAndOnPush extends CheckboxWithNgModel {} |
| 1209 | + |
1179 | 1210 | /** Simple test component with multiple checkboxes. */
|
1180 | 1211 | @Component(({
|
1181 | 1212 | template: `
|
|
0 commit comments