-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
dropdown.directive.spec.ts
244 lines (221 loc) · 9.15 KB
/
dropdown.directive.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { ESCAPE } from '@angular/cdk/keycodes';
import { OverlayContainer } from '@angular/cdk/overlay';
import { Component, Provider, Type } from '@angular/core';
import { ComponentFixture, fakeAsync, inject, TestBed, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { dispatchFakeEvent, dispatchKeyboardEvent } from 'ng-zorro-antd/core/testing';
import { NzMenuModule } from 'ng-zorro-antd/menu';
import { NzDropDownDirective } from './dropdown.directive';
import { NzDropDownModule } from './dropdown.module';
describe('dropdown', () => {
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
function createComponent<T>(
component: Type<T>,
providers: Provider[] = [],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declarations: any[] = []
): ComponentFixture<T> {
TestBed.configureTestingModule({
imports: [RouterTestingModule, NzDropDownModule, NzMenuModule, NoopAnimationsModule],
declarations: [component, ...declarations],
providers
})
.compileComponents()
.then();
inject([OverlayContainer], (oc: OverlayContainer) => {
overlayContainer = oc;
overlayContainerElement = oc.getContainerElement();
})();
return TestBed.createComponent<T>(component);
}
afterEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
currentOverlayContainer.ngOnDestroy();
overlayContainer.ngOnDestroy();
}));
it('should hover correct', fakeAsync(() => {
const fixture = createComponent(NzTestDropdownComponent, [], []);
fixture.componentInstance.trigger = 'hover';
fixture.detectChanges();
expect(overlayContainerElement.textContent).toBe('');
expect(() => {
const dropdownElement = fixture.debugElement.query(By.directive(NzDropDownDirective)).nativeElement;
dispatchFakeEvent(dropdownElement, 'mouseenter');
fixture.detectChanges();
tick(1000);
fixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('1st menu item');
}).not.toThrowError();
}));
it('should click correct', fakeAsync(() => {
const fixture = createComponent(NzTestDropdownComponent, [], []);
fixture.componentInstance.trigger = 'click';
fixture.detectChanges();
expect(overlayContainerElement.textContent).toBe('');
expect(() => {
const dropdownElement = fixture.debugElement.query(By.directive(NzDropDownDirective)).nativeElement;
dispatchFakeEvent(dropdownElement, 'click');
fixture.detectChanges();
tick(1000);
fixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('1st menu item');
}).not.toThrowError();
}));
it('should disabled work', fakeAsync(() => {
const fixture = createComponent(NzTestDropdownComponent, [], []);
fixture.componentInstance.disabled = true;
fixture.detectChanges();
expect(overlayContainerElement.textContent).toBe('');
expect(() => {
const dropdownElement = fixture.debugElement.query(By.directive(NzDropDownDirective)).nativeElement;
dispatchFakeEvent(dropdownElement, 'mouseenter');
fixture.detectChanges();
tick(1000);
fixture.detectChanges();
expect(overlayContainerElement.textContent).toBe('');
}).not.toThrowError();
}));
describe('when nzBackdrop=true', () => {
let fixture: ComponentFixture<NzTestDropdownComponent>;
beforeEach(() => {
fixture = createComponent(NzTestDropdownComponent, [], []);
fixture.componentInstance.backdrop = true;
});
it('should disappear if invisible backdrop clicked if nzTrigger=click', fakeAsync(() => {
fixture.componentInstance.trigger = 'click';
fixture.detectChanges();
expect(() => {
const dropdownElement = fixture.debugElement.query(By.directive(NzDropDownDirective)).nativeElement;
dispatchFakeEvent(dropdownElement, 'click');
tick(1000);
fixture.detectChanges();
const backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
expect(backdrop).not.toBeNull();
dispatchFakeEvent(backdrop as Element, 'click');
tick(1000);
fixture.detectChanges();
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeNull();
}).not.toThrowError();
}));
});
it('should disappear if Escape pressed', fakeAsync(() => {
const fixture = createComponent(NzTestDropdownComponent, [], []);
fixture.componentInstance.trigger = 'click';
fixture.componentInstance.backdrop = true;
fixture.detectChanges();
expect(() => {
const dropdownElement = fixture.debugElement.query(By.directive(NzDropDownDirective)).nativeElement;
dispatchFakeEvent(dropdownElement, 'click');
tick(1000);
fixture.detectChanges();
const backdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
expect(backdrop).not.toBeNull();
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
tick(1000);
fixture.detectChanges();
const nullBackdrop = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
expect(nullBackdrop).toBeNull();
}).not.toThrowError();
}));
it('should nzOverlayClassName and nzOverlayStyle work', fakeAsync(() => {
const fixture = createComponent(NzTestDropdownComponent, [], []);
fixture.detectChanges();
expect(() => {
const dropdownElement = fixture.debugElement.query(By.directive(NzDropDownDirective)).nativeElement;
dispatchFakeEvent(dropdownElement, 'mouseenter');
fixture.detectChanges();
tick(1000);
fixture.detectChanges();
expect(overlayContainerElement.querySelector('.ant-dropdown')!.classList).toContain('custom-class');
expect(overlayContainerElement.querySelector<HTMLElement>('.ant-dropdown')!.style.color).toBe('rgb(0, 0, 0)');
}).not.toThrowError();
}));
it('should nzVisible & nzClickHide work', fakeAsync(() => {
const fixture = createComponent(NzTestDropdownVisibleComponent, [], []);
fixture.detectChanges();
expect(fixture.componentInstance.triggerVisible).toHaveBeenCalledTimes(0);
const dropdownElement = fixture.debugElement.query(By.directive(NzDropDownDirective)).nativeElement;
dispatchFakeEvent(dropdownElement, 'mouseenter');
fixture.detectChanges();
tick(1000);
fixture.detectChanges();
expect(fixture.componentInstance.triggerVisible).toHaveBeenCalledTimes(1);
expect(fixture.componentInstance.triggerVisible).toHaveBeenCalledWith(true);
expect(overlayContainerElement.textContent).toContain('Clicking me will not close the menu.');
dispatchFakeEvent(overlayContainerElement.querySelector('.first-menu')!, 'click');
fixture.detectChanges();
tick(1000);
fixture.detectChanges();
expect(fixture.componentInstance.triggerVisible).toHaveBeenCalledTimes(1);
expect(fixture.componentInstance.triggerVisible).toHaveBeenCalledWith(true);
expect(overlayContainerElement.textContent).toContain('Clicking me will not close the menu.');
dispatchFakeEvent(overlayContainerElement.querySelector('.second-menu')!, 'click');
fixture.detectChanges();
tick(1000);
fixture.detectChanges();
expect(fixture.componentInstance.triggerVisible).toHaveBeenCalledTimes(1);
expect(overlayContainerElement.textContent).toContain('Clicking me will not close the menu.');
dispatchFakeEvent(overlayContainerElement.querySelector('.close-menu')!, 'click');
fixture.detectChanges();
tick(2000);
fixture.detectChanges();
expect(fixture.componentInstance.triggerVisible).toHaveBeenCalledTimes(1);
}));
});
@Component({
template: `
<a
nz-dropdown
[nzDropdownMenu]="menu"
[nzTrigger]="trigger"
[nzDisabled]="disabled"
[nzPlacement]="placement"
[nzBackdrop]="backdrop"
[nzOverlayClassName]="className"
[nzOverlayStyle]="overlayStyle"
>
Trigger
</a>
<nz-dropdown-menu #menu="nzDropdownMenu">
<ul nz-menu>
<li nz-menu-item>1st menu item</li>
<li nz-menu-item>2nd menu item</li>
<li nz-menu-item>3rd menu item</li>
</ul>
</nz-dropdown-menu>
`
})
export class NzTestDropdownComponent {
backdrop = false;
trigger = 'hover';
placement = 'bottomLeft';
disabled = false;
className = 'custom-class';
overlayStyle = { color: '#000' };
}
@Component({
template: `
<a
nz-dropdown
[nzDropdownMenu]="menu"
[nzClickHide]="false"
[(nzVisible)]="visible"
(nzVisibleChange)="triggerVisible($event)"
>
Hover me
</a>
<nz-dropdown-menu #menu="nzDropdownMenu">
<ul nz-menu>
<li nz-menu-item class="first-menu">Clicking me will not close the menu.</li>
<li nz-menu-item class="second-menu">Clicking me will not close the menu also.</li>
<li nz-menu-item (click)="visible = false" class="close-menu">Clicking me will close the menu</li>
</ul>
</nz-dropdown-menu>
`
})
export class NzTestDropdownVisibleComponent {
visible = false;
triggerVisible = jasmine.createSpy('visibleChange');
}