|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {supportsPassiveEventListeners} from '@angular/cdk/platform'; |
| 10 | +import {Component, ElementRef, ViewChild} from '@angular/core'; |
| 11 | +import {ComponentFixture, inject, TestBed} from '@angular/core/testing'; |
| 12 | +import {empty as observableEmpty} from 'rxjs/observable/empty'; |
| 13 | +import {AutofillEvent, AutofillMonitor} from './autofill'; |
| 14 | +import {MatInputModule} from './input-module'; |
| 15 | + |
| 16 | + |
| 17 | +const listenerOptions: any = supportsPassiveEventListeners() ? {passive: true} : false; |
| 18 | + |
| 19 | + |
| 20 | +describe('AutofillMonitor', () => { |
| 21 | + let autofillMonitor: AutofillMonitor; |
| 22 | + let fixture: ComponentFixture<Inputs>; |
| 23 | + let testComponent: Inputs; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + TestBed.configureTestingModule({ |
| 27 | + imports: [MatInputModule], |
| 28 | + declarations: [Inputs], |
| 29 | + }).compileComponents(); |
| 30 | + }); |
| 31 | + |
| 32 | + beforeEach(inject([AutofillMonitor], (afm: AutofillMonitor) => { |
| 33 | + autofillMonitor = afm; |
| 34 | + fixture = TestBed.createComponent(Inputs); |
| 35 | + testComponent = fixture.componentInstance; |
| 36 | + |
| 37 | + for (const input of [testComponent.input1, testComponent.input2, testComponent.input3]) { |
| 38 | + spyOn(input.nativeElement, 'addEventListener'); |
| 39 | + spyOn(input.nativeElement, 'removeEventListener'); |
| 40 | + } |
| 41 | + |
| 42 | + fixture.detectChanges(); |
| 43 | + })); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + // Call destroy to make sure we clean up all listeners. |
| 47 | + autofillMonitor.ngOnDestroy(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should add monitored class and listener upon monitoring', () => { |
| 51 | + const inputEl = testComponent.input1.nativeElement; |
| 52 | + expect(inputEl.addEventListener).not.toHaveBeenCalled(); |
| 53 | + |
| 54 | + autofillMonitor.monitor(inputEl); |
| 55 | + expect(inputEl.classList).toContain('mat-input-autofill-monitored'); |
| 56 | + expect(inputEl.addEventListener) |
| 57 | + .toHaveBeenCalledWith('animationstart', jasmine.any(Function), listenerOptions); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should not add multiple listeners to the same element', () => { |
| 61 | + const inputEl = testComponent.input1.nativeElement; |
| 62 | + expect(inputEl.addEventListener).not.toHaveBeenCalled(); |
| 63 | + |
| 64 | + autofillMonitor.monitor(inputEl); |
| 65 | + autofillMonitor.monitor(inputEl); |
| 66 | + expect(inputEl.addEventListener).toHaveBeenCalledTimes(1); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should remove monitored class and listener upon stop monitoring', () => { |
| 70 | + const inputEl = testComponent.input1.nativeElement; |
| 71 | + autofillMonitor.monitor(inputEl); |
| 72 | + expect(inputEl.classList).toContain('mat-input-autofill-monitored'); |
| 73 | + expect(inputEl.removeEventListener).not.toHaveBeenCalled(); |
| 74 | + |
| 75 | + autofillMonitor.stopMonitoring(inputEl); |
| 76 | + expect(inputEl.classList).not.toContain('mat-input-autofill-monitored'); |
| 77 | + expect(inputEl.removeEventListener) |
| 78 | + .toHaveBeenCalledWith('animationstart', jasmine.any(Function), listenerOptions); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should stop monitoring all monitored elements upon destroy', () => { |
| 82 | + const inputEl1 = testComponent.input1.nativeElement; |
| 83 | + const inputEl2 = testComponent.input2.nativeElement; |
| 84 | + const inputEl3 = testComponent.input3.nativeElement; |
| 85 | + autofillMonitor.monitor(inputEl1); |
| 86 | + autofillMonitor.monitor(inputEl2); |
| 87 | + autofillMonitor.monitor(inputEl3); |
| 88 | + expect(inputEl1.removeEventListener).not.toHaveBeenCalled(); |
| 89 | + expect(inputEl2.removeEventListener).not.toHaveBeenCalled(); |
| 90 | + expect(inputEl3.removeEventListener).not.toHaveBeenCalled(); |
| 91 | + |
| 92 | + autofillMonitor.ngOnDestroy(); |
| 93 | + expect(inputEl1.removeEventListener).toHaveBeenCalled(); |
| 94 | + expect(inputEl2.removeEventListener).toHaveBeenCalled(); |
| 95 | + expect(inputEl3.removeEventListener).toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should emit and add filled class upon start animation', () => { |
| 99 | + const inputEl = testComponent.input1.nativeElement; |
| 100 | + let animationStartCallback: Function = () => {}; |
| 101 | + let autofillStreamEvent: AutofillEvent | null = null; |
| 102 | + inputEl.addEventListener.and.callFake((_, cb) => animationStartCallback = cb); |
| 103 | + const autofillStream = autofillMonitor.monitor(inputEl); |
| 104 | + autofillStream.subscribe(event => autofillStreamEvent = event); |
| 105 | + expect(autofillStreamEvent).toBeNull(); |
| 106 | + expect(inputEl.classList).not.toContain('mat-input-autofilled'); |
| 107 | + |
| 108 | + animationStartCallback({animationName: 'mat-input-autofill-start', target: inputEl}); |
| 109 | + expect(inputEl.classList).toContain('mat-input-autofilled'); |
| 110 | + expect(autofillStreamEvent).toEqual({target: inputEl, isAutofilled: true} as any); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should emit and remove filled class upon end animation', () => { |
| 114 | + const inputEl = testComponent.input1.nativeElement; |
| 115 | + let animationStartCallback: Function = () => {}; |
| 116 | + let autofillStreamEvent: AutofillEvent | null = null; |
| 117 | + inputEl.addEventListener.and.callFake((_, cb) => animationStartCallback = cb); |
| 118 | + const autofillStream = autofillMonitor.monitor(inputEl); |
| 119 | + autofillStream.subscribe(event => autofillStreamEvent = event); |
| 120 | + animationStartCallback({animationName: 'mat-input-autofill-start', target: inputEl}); |
| 121 | + expect(inputEl.classList).toContain('mat-input-autofilled'); |
| 122 | + expect(autofillStreamEvent).toEqual({target: inputEl, isAutofilled: true} as any); |
| 123 | + |
| 124 | + animationStartCallback({animationName: 'mat-input-autofill-end', target: inputEl}); |
| 125 | + expect(inputEl.classList).not.toContain('mat-input-autofilled'); |
| 126 | + expect(autofillStreamEvent).toEqual({target: inputEl, isAutofilled: false} as any); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should cleanup filled class if monitoring stopped in autofilled state', () => { |
| 130 | + const inputEl = testComponent.input1.nativeElement; |
| 131 | + let animationStartCallback: Function = () => {}; |
| 132 | + inputEl.addEventListener.and.callFake((_, cb) => animationStartCallback = cb); |
| 133 | + autofillMonitor.monitor(inputEl); |
| 134 | + animationStartCallback({animationName: 'mat-input-autofill-start', target: inputEl}); |
| 135 | + expect(inputEl.classList).toContain('mat-input-autofilled'); |
| 136 | + |
| 137 | + autofillMonitor.stopMonitoring(inputEl); |
| 138 | + expect(inputEl.classlist).not.toContain('mat-input-autofilled'); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe('matAutofill', () => { |
| 143 | + let autofillMonitor: AutofillMonitor; |
| 144 | + let fixture: ComponentFixture<InputWithMatAutofilled>; |
| 145 | + let testComponent: InputWithMatAutofilled; |
| 146 | + |
| 147 | + beforeEach(() => { |
| 148 | + TestBed.configureTestingModule({ |
| 149 | + imports: [MatInputModule], |
| 150 | + declarations: [InputWithMatAutofilled], |
| 151 | + }).compileComponents(); |
| 152 | + }); |
| 153 | + |
| 154 | + beforeEach(inject([AutofillMonitor], (afm: AutofillMonitor) => { |
| 155 | + autofillMonitor = afm; |
| 156 | + spyOn(autofillMonitor, 'monitor').and.returnValue(observableEmpty()); |
| 157 | + spyOn(autofillMonitor, 'stopMonitoring'); |
| 158 | + fixture = TestBed.createComponent(InputWithMatAutofilled); |
| 159 | + testComponent = fixture.componentInstance; |
| 160 | + fixture.detectChanges(); |
| 161 | + })); |
| 162 | + |
| 163 | + it('should monitor host element on init', () => { |
| 164 | + expect(autofillMonitor.monitor).toHaveBeenCalledWith(testComponent.input.nativeElement); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should stop monitoring host element on destroy', () => { |
| 168 | + expect(autofillMonitor.stopMonitoring).not.toHaveBeenCalled(); |
| 169 | + fixture.destroy(); |
| 170 | + expect(autofillMonitor.stopMonitoring).toHaveBeenCalledWith(testComponent.input.nativeElement); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +@Component({ |
| 175 | + template: ` |
| 176 | + <input #input1> |
| 177 | + <input #input2> |
| 178 | + <input #input3> |
| 179 | + ` |
| 180 | +}) |
| 181 | +class Inputs { |
| 182 | + @ViewChild('input1') input1: ElementRef; |
| 183 | + @ViewChild('input2') input2: ElementRef; |
| 184 | + @ViewChild('input3') input3: ElementRef; |
| 185 | +} |
| 186 | + |
| 187 | +@Component({ |
| 188 | + template: `<input #input matAutofill>` |
| 189 | +}) |
| 190 | +class InputWithMatAutofilled { |
| 191 | + @ViewChild('input') input: ElementRef; |
| 192 | +} |
0 commit comments