1
1
import { MutationObserverFactory } from '@angular/cdk/observers' ;
2
2
import { dispatchFakeEvent } from '@angular/cdk/testing' ;
3
3
import { Component } from '@angular/core' ;
4
- import { ComponentFixture , fakeAsync , flushMicrotasks , TestBed , tick } from '@angular/core/testing' ;
4
+ import {
5
+ ComponentFixture ,
6
+ fakeAsync ,
7
+ flush ,
8
+ flushMicrotasks ,
9
+ TestBed ,
10
+ tick ,
11
+ } from '@angular/core/testing' ;
5
12
import { FormControl , FormsModule , NgModel , ReactiveFormsModule } from '@angular/forms' ;
6
13
import { defaultRippleAnimationConfig } from '@angular/material/core' ;
7
14
import { By , HAMMER_GESTURE_CONFIG } from '@angular/platform-browser' ;
@@ -773,7 +780,7 @@ describe('MatSlideToggle with forms', () => {
773
780
expect ( slideToggleElement . classList ) . toContain ( 'mat-checked' ) ;
774
781
} ) ) ;
775
782
776
- it ( 'should have the correct control state initially and after interaction' , ( ) => {
783
+ it ( 'should have the correct control state initially and after interaction' , fakeAsync ( ( ) => {
777
784
// The control should start off valid, pristine, and untouched.
778
785
expect ( slideToggleModel . valid ) . toBe ( true ) ;
779
786
expect ( slideToggleModel . pristine ) . toBe ( true ) ;
@@ -795,13 +802,31 @@ describe('MatSlideToggle with forms', () => {
795
802
// also turn touched.
796
803
dispatchFakeEvent ( inputElement , 'blur' ) ;
797
804
fixture . detectChanges ( ) ;
805
+ flushMicrotasks ( ) ;
798
806
799
807
expect ( slideToggleModel . valid ) . toBe ( true ) ;
800
808
expect ( slideToggleModel . pristine ) . toBe ( false ) ;
801
809
expect ( slideToggleModel . touched ) . toBe ( true ) ;
802
- } ) ;
810
+ } ) ) ;
811
+
812
+ it ( 'should not throw an error when disabling while focused' , fakeAsync ( ( ) => {
813
+ expect ( ( ) => {
814
+ // Focus the input element because after disabling, the `blur` event should automatically
815
+ // fire and not result in a changed after checked exception. Related: #12323
816
+ inputElement . focus ( ) ;
817
+
818
+ // Flush the two nested timeouts from the FocusMonitor that are being created on `focus`.
819
+ flush ( ) ;
820
+
821
+ slideToggle . disabled = true ;
822
+ fixture . detectChanges ( ) ;
823
+ flushMicrotasks ( ) ;
824
+ } ) . not . toThrow ( ) ;
825
+ } ) ) ;
826
+
827
+ it ( 'should not set the control to touched when changing the state programmatically' ,
828
+ fakeAsync ( ( ) => {
803
829
804
- it ( 'should not set the control to touched when changing the state programmatically' , ( ) => {
805
830
// The control should start off with being untouched.
806
831
expect ( slideToggleModel . touched ) . toBe ( false ) ;
807
832
@@ -815,10 +840,11 @@ describe('MatSlideToggle with forms', () => {
815
840
// also turn touched.
816
841
dispatchFakeEvent ( inputElement , 'blur' ) ;
817
842
fixture . detectChanges ( ) ;
843
+ flushMicrotasks ( ) ;
818
844
819
845
expect ( slideToggleModel . touched ) . toBe ( true ) ;
820
846
expect ( slideToggleElement . classList ) . toContain ( 'mat-checked' ) ;
821
- } ) ;
847
+ } ) ) ;
822
848
823
849
it ( 'should not set the control to touched when changing the model' , fakeAsync ( ( ) => {
824
850
// The control should start off with being untouched.
0 commit comments