|
1 | 1 | import {dispatchFakeEvent} from '@angular/cdk/testing';
|
2 |
| -import {ChangeDetectionStrategy, Component, DebugElement, Type, ViewChild} from '@angular/core'; |
| 2 | +import {ChangeDetectionStrategy, Component, DebugElement, Type} from '@angular/core'; |
3 | 3 | import {
|
4 | 4 | ComponentFixture,
|
5 | 5 | fakeAsync,
|
@@ -68,6 +68,28 @@ describe('MatCheckbox', () => {
|
68 | 68 | expect(inputElement.checked).toBe(false);
|
69 | 69 | }));
|
70 | 70 |
|
| 71 | + |
| 72 | + it('should toggle checkbox ripple disabledness correctly', fakeAsync(() => { |
| 73 | + const rippleSelector = '.mat-ripple-element:not(.mat-checkbox-persistent-ripple)'; |
| 74 | + |
| 75 | + testComponent.isDisabled = true; |
| 76 | + fixture.detectChanges(); |
| 77 | + dispatchFakeEvent(labelElement, 'mousedown'); |
| 78 | + dispatchFakeEvent(labelElement, 'mouseup'); |
| 79 | + labelElement.click(); |
| 80 | + expect(checkboxNativeElement.querySelectorAll(rippleSelector).length).toBe(0); |
| 81 | + |
| 82 | + flush(); |
| 83 | + testComponent.isDisabled = false; |
| 84 | + fixture.detectChanges(); |
| 85 | + dispatchFakeEvent(labelElement, 'mousedown'); |
| 86 | + dispatchFakeEvent(labelElement, 'mouseup'); |
| 87 | + labelElement.click(); |
| 88 | + expect(checkboxNativeElement.querySelectorAll(rippleSelector).length).toBe(1); |
| 89 | + |
| 90 | + flush(); |
| 91 | + })); |
| 92 | + |
71 | 93 | it('should add and remove indeterminate state', fakeAsync(() => {
|
72 | 94 | expect(inputElement.checked).toBe(false);
|
73 | 95 | expect(inputElement.indeterminate).toBe(false);
|
@@ -164,15 +186,6 @@ describe('MatCheckbox', () => {
|
164 | 186 | expect(testComponent.isIndeterminate).toBe(true);
|
165 | 187 | }));
|
166 | 188 |
|
167 |
| - it('should change native element checked when check programmatically', fakeAsync(() => { |
168 |
| - expect(inputElement.checked).toBe(false); |
169 |
| - |
170 |
| - checkboxInstance.checked = true; |
171 |
| - fixture.detectChanges(); |
172 |
| - |
173 |
| - expect(inputElement.checked).toBe(true); |
174 |
| - })); |
175 |
| - |
176 | 189 | it('should toggle checked state on click', fakeAsync(() => {
|
177 | 190 | expect(checkboxInstance.checked).toBe(false);
|
178 | 191 |
|
@@ -698,64 +711,6 @@ describe('MatCheckbox', () => {
|
698 | 711 | }));
|
699 | 712 | });
|
700 | 713 |
|
701 |
| - describe('using ViewChild', () => { |
702 |
| - let checkboxDebugElement: DebugElement; |
703 |
| - let checkboxNativeElement: HTMLElement; |
704 |
| - let testComponent: CheckboxUsingViewChild; |
705 |
| - |
706 |
| - beforeEach(() => { |
707 |
| - fixture = createComponent(CheckboxUsingViewChild); |
708 |
| - fixture.detectChanges(); |
709 |
| - |
710 |
| - checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox)); |
711 |
| - checkboxNativeElement = checkboxDebugElement.nativeElement; |
712 |
| - testComponent = fixture.debugElement.componentInstance; |
713 |
| - }); |
714 |
| - |
715 |
| - it('should toggle checkbox disabledness correctly', fakeAsync(() => { |
716 |
| - const checkboxInstance = checkboxDebugElement.componentInstance; |
717 |
| - const inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input'); |
718 |
| - expect(checkboxInstance.disabled).toBe(false); |
719 |
| - expect(inputElement.tabIndex).toBe(0); |
720 |
| - expect(inputElement.disabled).toBe(false); |
721 |
| - |
722 |
| - testComponent.isDisabled = true; |
723 |
| - fixture.detectChanges(); |
724 |
| - |
725 |
| - expect(checkboxInstance.disabled).toBe(true); |
726 |
| - expect(inputElement.disabled).toBe(true); |
727 |
| - |
728 |
| - testComponent.isDisabled = false; |
729 |
| - fixture.detectChanges(); |
730 |
| - |
731 |
| - expect(checkboxInstance.disabled).toBe(false); |
732 |
| - expect(inputElement.tabIndex).toBe(0); |
733 |
| - expect(inputElement.disabled).toBe(false); |
734 |
| - })); |
735 |
| - |
736 |
| - it('should toggle checkbox ripple disabledness correctly', fakeAsync(() => { |
737 |
| - const rippleSelector = '.mat-ripple-element:not(.mat-checkbox-persistent-ripple)'; |
738 |
| - const labelElement = checkboxNativeElement.querySelector('label') as HTMLLabelElement; |
739 |
| - |
740 |
| - testComponent.isDisabled = true; |
741 |
| - fixture.detectChanges(); |
742 |
| - dispatchFakeEvent(labelElement, 'mousedown'); |
743 |
| - dispatchFakeEvent(labelElement, 'mouseup'); |
744 |
| - labelElement.click(); |
745 |
| - expect(checkboxNativeElement.querySelectorAll(rippleSelector).length).toBe(0); |
746 |
| - |
747 |
| - flush(); |
748 |
| - testComponent.isDisabled = false; |
749 |
| - fixture.detectChanges(); |
750 |
| - dispatchFakeEvent(labelElement, 'mousedown'); |
751 |
| - dispatchFakeEvent(labelElement, 'mouseup'); |
752 |
| - labelElement.click(); |
753 |
| - expect(checkboxNativeElement.querySelectorAll(rippleSelector).length).toBe(1); |
754 |
| - |
755 |
| - flush(); |
756 |
| - })); |
757 |
| - }); |
758 |
| - |
759 | 714 | describe('with multiple checkboxes', () => {
|
760 | 715 | beforeEach(() => {
|
761 | 716 | fixture = createComponent(MultipleCheckboxes);
|
@@ -855,11 +810,9 @@ describe('MatCheckbox', () => {
|
855 | 810 | // fire and not result in a changed after checked exception. Related: #12323
|
856 | 811 | inputElement.focus();
|
857 | 812 |
|
858 |
| - // Flush the two nested timeouts from the FocusMonitor that are being created on `focus`. |
859 |
| - flush(); |
860 |
| - |
861 |
| - checkboxInstance.disabled = true; |
| 813 | + fixture.componentInstance.isDisabled = true; |
862 | 814 | fixture.detectChanges();
|
| 815 | + |
863 | 816 | flushMicrotasks();
|
864 | 817 | }).not.toThrow();
|
865 | 818 | }));
|
@@ -1005,11 +958,13 @@ class SingleCheckbox {
|
1005 | 958 |
|
1006 | 959 | /** Simple component for testing an MatCheckbox with required ngModel. */
|
1007 | 960 | @Component({
|
1008 |
| - template: `<mat-checkbox [required]="isRequired" [(ngModel)]="isGood">Be good</mat-checkbox>`, |
| 961 | + template: `<mat-checkbox [required]="isRequired" [(ngModel)]="isGood" |
| 962 | + [disabled]="isDisabled">Be good</mat-checkbox>`, |
1009 | 963 | })
|
1010 | 964 | class CheckboxWithNgModel {
|
1011 | 965 | isGood: boolean = false;
|
1012 | 966 | isRequired: boolean = true;
|
| 967 | + isDisabled: boolean = false; |
1013 | 968 | }
|
1014 | 969 |
|
1015 | 970 | @Component({
|
@@ -1043,20 +998,6 @@ class CheckboxWithTabIndex {
|
1043 | 998 | isDisabled: boolean = false;
|
1044 | 999 | }
|
1045 | 1000 |
|
1046 |
| - |
1047 |
| -/** Simple test component that accesses MatCheckbox using ViewChild. */ |
1048 |
| -@Component({ |
1049 |
| - template: ` |
1050 |
| - <mat-checkbox></mat-checkbox>`, |
1051 |
| -}) |
1052 |
| -class CheckboxUsingViewChild { |
1053 |
| - @ViewChild(MatCheckbox, {static: false}) checkbox: MatCheckbox; |
1054 |
| - |
1055 |
| - set isDisabled(value: boolean) { |
1056 |
| - this.checkbox.disabled = value; |
1057 |
| - } |
1058 |
| -} |
1059 |
| - |
1060 | 1001 | /** Simple test component with an aria-label set. */
|
1061 | 1002 | @Component({template: `<mat-checkbox aria-label="Super effective"></mat-checkbox>`})
|
1062 | 1003 | class CheckboxWithAriaLabel {
|
|
0 commit comments