-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
avatar.spec.ts
300 lines (275 loc) · 9.21 KB
/
avatar.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { Component, DebugElement, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { createFakeEvent } from 'ng-zorro-antd/core/testing';
import { NzIconTestModule } from 'ng-zorro-antd/icon/testing';
import { NzAvatarGroupComponent } from './avatar-group.component';
import { NzAvatarComponent } from './avatar.component';
import { NzAvatarModule } from './avatar.module';
const imageBase64 =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==';
function getType(dl: DebugElement): string {
const el = dl.nativeElement as HTMLElement;
if (el.querySelector('img') != null) {
return 'image';
}
if (el.querySelector('.anticon') != null) {
return 'icon';
}
return el.innerText.trim().length === 0 ? '' : 'text';
}
describe('avatar group', () => {
let fixture: ComponentFixture<TestAvatarGroupComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NzAvatarModule],
declarations: [TestAvatarGroupComponent]
}).compileComponents();
fixture = TestBed.createComponent(TestAvatarGroupComponent);
fixture.detectChanges();
});
it('should avatar group work', () => {
fixture.detectChanges();
const avatarGroup = fixture.debugElement.query(By.directive(NzAvatarGroupComponent));
expect(avatarGroup.nativeElement.classList).toContain('ant-avatar-group');
});
});
describe('avatar', () => {
let fixture: ComponentFixture<TestAvatarComponent>;
let context: TestAvatarComponent;
let dl: DebugElement;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NzAvatarModule, NzIconTestModule],
declarations: [TestAvatarComponent]
}).compileComponents();
fixture = TestBed.createComponent(TestAvatarComponent);
context = fixture.componentInstance;
dl = fixture.debugElement;
fixture.detectChanges();
});
describe('#nzSrc', () => {
it('#nzSrc', () => {
expect(context).not.toBeNull();
});
it('should tolerate error src', fakeAsync(() => {
const event = createFakeEvent('error');
expect(getType(dl)).toBe('image');
expect(context.comp.hasSrc).toBe(true);
// Manually dispatch error.
context.nzSrc = '';
context.comp.imgError(event);
tick();
fixture.detectChanges();
expect(getType(dl)).toBe('icon');
expect(context.comp.hasSrc).toBe(false);
context.nzSrc = imageBase64;
tick();
fixture.detectChanges();
expect(context.comp.hasSrc).toBe(true);
expect(getType(dl)).toBe('image');
tick();
}));
it('should prevent default fallback when error src', fakeAsync(() => {
const event = createFakeEvent('error');
event.preventDefault();
expect(getType(dl)).toBe('image');
expect(context.comp.hasSrc).toBe(true);
// Manually dispatch error.
context.nzSrc = 'Invalid image src';
context.comp.imgError(event);
tick();
fixture.detectChanges();
expect(getType(dl)).toBe('image');
expect(context.comp.hasSrc).toBe(true);
context.nzSrc = imageBase64;
tick();
fixture.detectChanges();
expect(context.comp.hasSrc).toBe(true);
expect(getType(dl)).toBe('image');
tick();
}));
it('#nzSrcSet', () => {
context.nzSrcSet = '1.png';
fixture.detectChanges();
const el = dl.query(By.css(`img`)).nativeElement as HTMLImageElement;
expect(el.srcset).toBe(context.nzSrcSet);
});
it('#nzAlt', () => {
context.nzAlt = 'alt';
fixture.detectChanges();
const el = dl.query(By.css(`img`)).nativeElement as HTMLImageElement;
expect(el.alt).toBe(context.nzAlt);
});
});
it('#nzIcon', () => {
context.nzSrc = null;
context.nzText = null;
fixture.detectChanges();
expect(getType(dl)).toBe('icon');
});
describe('#nzText', () => {
beforeEach(() => {
context.nzSrc = null;
context.nzIcon = null;
fixture.detectChanges();
});
it('property', () => {
expect(getType(dl)).toBe('text');
});
it('should be normal font-size', fakeAsync(() => {
context.nzText = 'a';
fixture.detectChanges();
tick();
const scale = getScaleFromCSSTransform(dl.nativeElement.querySelector('.ant-avatar-string')!.style.transform!);
expect(scale).toBe(1);
}));
it('should be auto set font-size', fakeAsync(() => {
context.nzText = 'LongUsername';
fixture.detectChanges();
tick();
const scale = getScaleFromCSSTransform(dl.nativeElement.querySelector('.ant-avatar-string')!.style.transform!);
expect(scale).toBeLessThan(1);
}));
describe('nzGap', () => {
let firstScale: number;
let avatarText: HTMLElement;
beforeEach(fakeAsync(() => {
context.nzGap = 4;
context.nzText = 'Username';
fixture.detectChanges();
tick();
avatarText = dl.nativeElement.querySelector('.ant-avatar-string')!;
firstScale = getScaleFromCSSTransform(avatarText.style.transform);
}));
it('should be set gap', fakeAsync(() => {
context.nzGap = 8;
fixture.detectChanges();
tick();
let scale = getScaleFromCSSTransform(avatarText.style.transform);
expect(scale).toBeLessThan(firstScale);
context.nzGap = 2;
fixture.detectChanges();
tick();
scale = getScaleFromCSSTransform(avatarText.style.transform);
expect(scale).toBeGreaterThan(firstScale);
}));
it('Should be set to default when the limit is exceeded', fakeAsync(() => {
context.nzGap = 1000;
fixture.detectChanges();
tick();
let scale = getScaleFromCSSTransform(avatarText.style.transform);
expect(scale).toEqual(firstScale);
context.nzGap = -1000;
fixture.detectChanges();
tick();
scale = getScaleFromCSSTransform(avatarText.style.transform);
expect(scale).toEqual(1);
}));
});
});
describe('#nzShape', () => {
for (const type of ['square', 'circle']) {
it(type, () => {
context.nzShape = type;
fixture.detectChanges();
expect(dl.query(By.css(`.ant-avatar-${type}`)) !== null).toBe(true);
});
}
});
describe('#nzSize', () => {
for (const item of [
{ size: 'large', cls: 'lg' },
{ size: 'small', cls: 'sm' }
]) {
it(item.size, () => {
context.nzSize = item.size;
fixture.detectChanges();
expect(dl.query(By.css(`.ant-avatar-${item.cls}`)) !== null).toBe(true);
});
}
it('custom size', () => {
context.nzSize = 64;
context.nzIcon = null;
context.nzSrc = null;
fixture.detectChanges();
const size = `${64}px`;
const hostStyle = dl.nativeElement.querySelector('nz-avatar').style;
expect(hostStyle.height === size).toBe(true);
expect(hostStyle.width === size).toBe(true);
expect(hostStyle.lineHeight === size).toBe(true);
expect(hostStyle.fontSize === ``).toBe(true);
context.nzIcon = 'user';
fixture.detectChanges();
expect(hostStyle.fontSize === `${context.nzSize / 2}px`).toBe(true);
});
});
describe('order: image > icon > text', () => {
it('image priority', () => {
expect(getType(dl)).toBe('image');
});
it('should be show icon when image loaded error and icon exists', fakeAsync(() => {
const event = createFakeEvent('error');
expect(getType(dl)).toBe('image');
context.comp.imgError(event);
tick();
fixture.detectChanges();
expect(getType(dl)).toBe('icon');
}));
it('should be show text when image loaded error and icon not exists', fakeAsync(() => {
const event = createFakeEvent('error');
expect(getType(dl)).toBe('image');
context.nzIcon = null;
fixture.detectChanges();
context.comp.imgError(event);
tick();
fixture.detectChanges();
expect(getType(dl)).toBe('text');
}));
it('should be show empty when image loaded error and icon & text not exists', fakeAsync(() => {
const event = createFakeEvent('error');
expect(getType(dl)).toBe('image');
context.nzIcon = null;
context.nzText = null;
fixture.detectChanges();
context.comp.imgError(event);
tick();
fixture.detectChanges();
expect(getType(dl)).toBe('');
}));
});
});
function getScaleFromCSSTransform(transform: string): number {
return +/(\w+)\(([^)]*)\)/g.exec(transform)![2];
}
@Component({
template: `
<nz-avatar
#comp
[nzShape]="nzShape"
[nzSize]="nzSize"
[nzIcon]="nzIcon"
[nzText]="nzText"
[nzGap]="nzGap"
[nzSrc]="nzSrc"
[nzSrcSet]="nzSrcSet"
[nzAlt]="nzAlt"
></nz-avatar>
`,
styleUrls: ['./style/index.less']
})
class TestAvatarComponent {
@ViewChild('comp', { static: false }) comp!: NzAvatarComponent;
nzShape = 'square';
nzSize: string | number = 'large';
nzGap = 4;
nzIcon: string | null = 'user';
nzText: string | null = 'A';
nzSrc: string | null = imageBase64;
nzSrcSet?: string;
nzAlt?: string;
}
@Component({
template: ` <nz-avatar-group></nz-avatar-group> `
})
class TestAvatarGroupComponent {}