-
Notifications
You must be signed in to change notification settings - Fork 667
/
ellipsis.spec.ts
227 lines (201 loc) · 6.91 KB
/
ellipsis.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
import { Component, DebugElement, ViewChild } from '@angular/core';
import { fakeAsync, tick, ComponentFixture, TestBed } from '@angular/core/testing';
import { configureTestSuite } from '@delon/testing';
import { By } from '@angular/platform-browser';
import { NzTooltipDirective } from 'ng-zorro-antd';
import { EllipsisComponent } from './ellipsis.component';
import { EllipsisModule } from './ellipsis.module';
describe('abc: ellipsis', () => {
let fixture: ComponentFixture<TestBaseComponent>;
let dl: DebugElement;
let context: TestBaseComponent;
let page: PageObject;
function genModule() {
TestBed.configureTestingModule({
imports: [EllipsisModule],
declarations: [TestLengthComponent, TestLineComponent],
});
}
describe('', () => {
configureTestSuite(genModule);
it('should be not length & line', () => {
fixture = TestBed.createComponent(TestLengthComponent);
dl = fixture.debugElement;
context = fixture.componentInstance;
context.lines = null;
context.length = null;
fixture.detectChanges();
page = new PageObject();
fixture.detectChanges();
page.checkCls('.ellipsis', 1);
});
describe('#length', () => {
beforeEach(() => {
fixture = TestBed.createComponent(TestLengthComponent);
dl = fixture.debugElement;
context = fixture.componentInstance;
page = new PageObject();
});
it('should working', fakeAsync(() => {
page.tick().check('There were...');
}));
it('should be tooltip', fakeAsync(() => {
context.tooltip = true;
page
.tick()
.hasTooltip()
.check('There were...');
}));
it('should be auto hide tail', fakeAsync(() => {
context.length = 4;
context.text = 'asdf';
page.tick().check('asdf');
context.length = 1;
context.text = 'as';
page.tick().check('...');
}));
it('#fullWidthRecognition', fakeAsync(() => {
context.fullWidthRecognition = true;
context.text = 'cipchk,你好吗';
page.tick().check('cipchk,你...');
}));
});
describe('#line', () => {
beforeEach(() => {
fixture = TestBed.createComponent(TestLineComponent);
dl = fixture.debugElement;
context = fixture.componentInstance;
page = new PageObject();
});
describe('when support line clamp', () => {
beforeEach(fakeAsync(() => {
// tslint:disable-next-line:no-string-literal
page.comp['isSupportLineClamp'] = true;
context.lines = 1;
page.tick();
}));
it('should working', () => {
// tslint:disable-next-line:no-string-literal
expect(+page.getEl('.ellipsis')!.style!['webkitLineClamp']).toBe(context!.lines as number);
});
});
describe('when not support line clamp', () => {
beforeEach(fakeAsync(() => {
spyOn(window, 'getComputedStyle').and.returnValue({ lineHeight: 20 } as any);
// tslint:disable-next-line:no-string-literal
page.comp['isSupportLineClamp'] = false;
context.lines = 1;
page.tick();
}));
it('should working', fakeAsync(() => {
context.lines = 2;
page.tick();
expect((dl.nativeElement as HTMLElement).innerHTML).toContain('...');
}));
it('should be not innerText', fakeAsync(() => {
const el = page.getEl('.ellipsis__shadow');
spyOnProperty(el!, 'innerText').and.returnValue(null);
context.lines = 2;
page.tick();
expect((dl.nativeElement as HTMLElement).innerHTML).toContain('...');
}));
it('should be raw response when html offsetHeight is smallest', () => {
const el = page.getEl('.ellipsis__shadow');
spyOnProperty(el!, 'offsetHeight').and.returnValue(1);
// tslint:disable-next-line:no-string-literal
page.comp['gen']();
expect(page.getText()).not.toBe('There');
});
});
});
});
describe('**slow**', () => {
it('should be throw error when include html element', fakeAsync(() => {
expect(() => {
genModule();
TestBed.overrideTemplate(TestLengthComponent, `<ellipsis length="1"><p>asdf</p></ellipsis>`);
fixture = TestBed.createComponent(TestLengthComponent);
dl = fixture.debugElement;
context = fixture.componentInstance;
fixture.detectChanges();
tick();
fixture.detectChanges();
}).toThrowError();
}));
});
class PageObject {
comp: EllipsisComponent;
constructor() {
this.comp = context.comp;
}
get root(): HTMLElement {
return dl.nativeElement;
}
getEl(cls: string): HTMLElement | null {
return this.root.querySelector(cls);
}
checkCls(cls: string, count = 1): this {
expect(this.root.querySelectorAll(cls).length).toBe(count);
return this;
}
get text(): string {
return this.comp.text;
}
checkText(text: string): this {
expect(this.comp.text).toBe(text);
return this;
}
getText(): string {
return this.text.substring(0, this.comp.targetCount);
}
hasTooltip(result = true): this {
const list = dl.queryAll(By.directive(NzTooltipDirective));
if (result) {
expect(list.length).toBeGreaterThan(0);
} else {
expect(list.length).toBe(0);
}
return this;
}
check(text: string): this {
expect(this.root.innerText).toBe(text);
return this;
}
tick(): this {
fixture.detectChanges();
tick(1000);
fixture.detectChanges();
return this;
}
}
});
class TestBaseComponent {
@ViewChild('comp', { static: true }) comp: EllipsisComponent;
tooltip = false;
length: number | null = 10;
lines: number | null = 3;
fullWidthRecognition = false;
tail = '...';
text = `There were injuries alleged in three cases in 2015, and a fourth incident in September, according to the safety recall report. After meeting with US regulators in October, the firm decided to issue a voluntary recall.`;
html = `<p>There were injuries alleged in three <a href="#cover">cases in 2015</a>, and a fourth incident in September, according to the safety recall report. After meeting with US regulators in October, the firm decided to issue a voluntary recall.</p>`;
}
@Component({
template: `
<ellipsis #comp [tooltip]="tooltip" [length]="length" [fullWidthRecognition]="fullWidthRecognition" [tail]="tail">{{ text }}</ellipsis>
`,
})
class TestLengthComponent extends TestBaseComponent {}
@Component({
template: `
<ellipsis
#comp
[tooltip]="tooltip"
[lines]="lines"
[fullWidthRecognition]="fullWidthRecognition"
[tail]="tail"
style="width: 1px; display: block;"
><div [innerHTML]="html"></div
></ellipsis>
`,
})
class TestLineComponent extends TestBaseComponent {}