File tree 2 files changed +22
-5
lines changed
2 files changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -155,14 +155,25 @@ describe('AutofillMonitor', () => {
155
155
let animationStartCallback : Function = ( ) => { } ;
156
156
inputEl . addEventListener . and . callFake ( ( _ , cb ) => animationStartCallback = cb ) ;
157
157
const autofillStream = autofillMonitor . monitor ( inputEl ) ;
158
- const spy = jasmine . createSpy ( 'zone spy' ) ;
158
+ const spy = jasmine . createSpy ( 'autofill spy' ) ;
159
159
160
160
autofillStream . subscribe ( ( ) => spy ( NgZone . isInAngularZone ( ) ) ) ;
161
161
expect ( spy ) . not . toHaveBeenCalled ( ) ;
162
162
163
163
animationStartCallback ( { animationName : 'cdk-text-field-autofill-start' , target : inputEl } ) ;
164
164
expect ( spy ) . toHaveBeenCalledWith ( true ) ;
165
165
} ) ;
166
+
167
+ it ( 'should not emit on init if input is unfilled' , ( ) => {
168
+ const inputEl = testComponent . input1 . nativeElement ;
169
+ let animationStartCallback : Function = ( ) => { } ;
170
+ inputEl . addEventListener . and . callFake ( ( _ , cb ) => animationStartCallback = cb ) ;
171
+ const autofillStream = autofillMonitor . monitor ( inputEl ) ;
172
+ const spy = jasmine . createSpy ( 'autofill spy' ) ;
173
+ autofillStream . subscribe ( ( ) => spy ( ) ) ;
174
+ animationStartCallback ( { animationName : 'cdk-text-field-autofill-end' , target : inputEl } ) ;
175
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
176
+ } ) ;
166
177
} ) ;
167
178
168
179
describe ( 'cdkAutofill' , ( ) => {
Original file line number Diff line number Diff line change @@ -68,12 +68,18 @@ export class AutofillMonitor implements OnDestroy {
68
68
}
69
69
70
70
const result = new Subject < AutofillEvent > ( ) ;
71
+ const cssClass = 'cdk-text-field-autofilled' ;
71
72
const listener = ( event : AnimationEvent ) => {
72
- if ( event . animationName === 'cdk-text-field-autofill-start' ) {
73
- element . classList . add ( 'cdk-text-field-autofilled' ) ;
73
+ // Animation events fire on initial element render, we check for the presence of the autofill
74
+ // CSS class to make sure this is a real change in state, not just the initial render before
75
+ // we fire off events.
76
+ if ( event . animationName === 'cdk-text-field-autofill-start' &&
77
+ ! element . classList . contains ( cssClass ) ) {
78
+ element . classList . add ( cssClass ) ;
74
79
this . _ngZone . run ( ( ) => result . next ( { target : event . target as Element , isAutofilled : true } ) ) ;
75
- } else if ( event . animationName === 'cdk-text-field-autofill-end' ) {
76
- element . classList . remove ( 'cdk-text-field-autofilled' ) ;
80
+ } else if ( event . animationName === 'cdk-text-field-autofill-end' &&
81
+ element . classList . contains ( cssClass ) ) {
82
+ element . classList . remove ( cssClass ) ;
77
83
this . _ngZone . run ( ( ) => result . next ( { target : event . target as Element , isAutofilled : false } ) ) ;
78
84
}
79
85
} ;
You can’t perform that action at this time.
0 commit comments