forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautocomplete.zone.spec.ts
182 lines (162 loc) · 5.66 KB
/
autocomplete.zone.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {OverlayModule} from '@angular/cdk/overlay';
import {dispatchFakeEvent} from '@angular/cdk/testing/private';
import {
Component,
NgZone,
OnDestroy,
Provider,
QueryList,
Type,
ViewChild,
ViewChildren,
provideZoneChangeDetection,
} from '@angular/core';
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
import {Subscription} from 'rxjs';
import {MATERIAL_ANIMATIONS, MatOption} from '../core';
import {MatFormField, MatFormFieldModule} from '../form-field';
import {MatInputModule} from '../input';
import {MatAutocomplete} from './autocomplete';
import {MatAutocompleteTrigger} from './autocomplete-trigger';
import {MatAutocompleteModule} from './module';
describe('MatAutocomplete Zone.js integration', () => {
// Creates a test component fixture.
function createComponent<T>(component: Type<T>, providers: Provider[] = []) {
TestBed.configureTestingModule({
imports: [
MatAutocompleteModule,
MatFormFieldModule,
MatInputModule,
FormsModule,
ReactiveFormsModule,
OverlayModule,
],
providers: [
provideZoneChangeDetection(),
...providers,
{provide: MATERIAL_ANIMATIONS, useValue: {animationsDisabled: true}},
],
declarations: [component],
});
return TestBed.createComponent<T>(component);
}
describe('panel toggling', () => {
let fixture: ComponentFixture<SimpleAutocomplete>;
beforeEach(() => {
fixture = createComponent(SimpleAutocomplete);
fixture.detectChanges();
});
it('should show the panel when the first open is after the initial zone stabilization', waitForAsync(() => {
// Note that we're running outside the Angular zone, in order to be able
// to test properly without the subscription from `_subscribeToClosingActions`
// giving us a false positive.
fixture.ngZone!.runOutsideAngular(() => {
fixture.componentInstance.trigger.openPanel();
Promise.resolve().then(() => {
expect(fixture.componentInstance.panel.showPanel)
.withContext(`Expected panel to be visible.`)
.toBe(true);
});
});
}));
});
it('should emit from `autocomplete.closed` after click outside inside the NgZone', waitForAsync(async () => {
const inZoneSpy = jasmine.createSpy('in zone spy');
const fixture = createComponent(SimpleAutocomplete);
fixture.detectChanges();
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
await new Promise(r => setTimeout(r));
const subscription = fixture.componentInstance.trigger.autocomplete.closed.subscribe(() =>
inZoneSpy(NgZone.isInAngularZone()),
);
await new Promise(r => setTimeout(r));
dispatchFakeEvent(document, 'click');
expect(inZoneSpy).toHaveBeenCalledWith(true);
subscription.unsubscribe();
}));
});
const SIMPLE_AUTOCOMPLETE_TEMPLATE = `
<mat-form-field [floatLabel]="floatLabel" [style.width.px]="width" [color]="theme">
@if (hasLabel) {
<mat-label>State</mat-label>
}
<input
matInput
placeholder="State"
[matAutocomplete]="auto"
[matAutocompletePosition]="position"
[matAutocompleteDisabled]="autocompleteDisabled"
[formControl]="stateCtrl">
</mat-form-field>
<mat-autocomplete
#auto="matAutocomplete"
[class]="panelClass"
[displayWith]="displayFn"
[disableRipple]="disableRipple"
[requireSelection]="requireSelection"
[aria-label]="ariaLabel"
[aria-labelledby]="ariaLabelledby"
(opened)="openedSpy()"
(closed)="closedSpy()">
@for (state of filteredStates; track state) {
<mat-option
[value]="state"
[style.height.px]="state.height"
[disabled]="state.disabled">
<span>{{ state.code }}: {{ state.name }}</span>
</mat-option>
}
</mat-autocomplete>
`;
@Component({template: SIMPLE_AUTOCOMPLETE_TEMPLATE, standalone: false})
class SimpleAutocomplete implements OnDestroy {
stateCtrl = new FormControl<{name: string; code: string} | string | null>(null);
filteredStates: any[];
valueSub: Subscription;
floatLabel = 'auto';
position = 'auto';
width: number;
disableRipple = false;
autocompleteDisabled = false;
hasLabel = true;
requireSelection = false;
ariaLabel: string;
ariaLabelledby: string;
panelClass = 'class-one class-two';
theme: string;
openedSpy = jasmine.createSpy('autocomplete opened spy');
closedSpy = jasmine.createSpy('autocomplete closed spy');
@ViewChild(MatAutocompleteTrigger, {static: true}) trigger: MatAutocompleteTrigger;
@ViewChild(MatAutocomplete) panel: MatAutocomplete;
@ViewChild(MatFormField) formField: MatFormField;
@ViewChildren(MatOption) options: QueryList<MatOption>;
states: {code: string; name: string; height?: number; disabled?: boolean}[] = [
{code: 'AL', name: 'Alabama'},
{code: 'CA', name: 'California'},
{code: 'FL', name: 'Florida'},
{code: 'KS', name: 'Kansas'},
{code: 'MA', name: 'Massachusetts'},
{code: 'NY', name: 'New York'},
{code: 'OR', name: 'Oregon'},
{code: 'PA', name: 'Pennsylvania'},
{code: 'TN', name: 'Tennessee'},
{code: 'VA', name: 'Virginia'},
{code: 'WY', name: 'Wyoming'},
];
constructor() {
this.filteredStates = this.states;
this.valueSub = this.stateCtrl.valueChanges.subscribe(val => {
this.filteredStates = val
? this.states.filter(s => s.name.match(new RegExp(val as string, 'gi')))
: this.states;
});
}
displayFn(value: any): string {
return value ? value.name : value;
}
ngOnDestroy() {
this.valueSub.unsubscribe();
}
}