-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathstatic.spec.ts
325 lines (270 loc) · 10.7 KB
/
static.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { describe, it, expect, beforeEach } from 'vitest';
import { cheerio, food, eleven } from './__fixtures__/fixtures.js';
import { type CheerioAPI } from './index.js';
describe('cheerio', () => {
describe('.html', () => {
it('() : should return innerHTML; $.html(obj) should return outerHTML', () => {
const $div = cheerio(
'div',
'<div><span>foo</span><span>bar</span></div>',
);
const span = $div.children()[1];
expect(cheerio(span).html()).toBe('bar');
expect(cheerio.html(span)).toBe('<span>bar</span>');
});
it('(<obj>) : should accept an object, an array, or a cheerio object', () => {
const $span = cheerio('<span>foo</span>');
expect(cheerio.html($span[0])).toBe('<span>foo</span>');
expect(cheerio.html($span)).toBe('<span>foo</span>');
});
it('(<value>) : should be able to set to an empty string', () => {
const $elem = cheerio('<span>foo</span>').html('');
expect(cheerio.html($elem)).toBe('<span></span>');
});
it('(<root>) : does not render the root element', () => {
const $ = cheerio.load('');
expect(cheerio.html($.root())).toBe(
'<html><head></head><body></body></html>',
);
});
it('(<elem>, <root>, <elem>) : does not render the root element', () => {
const $ = cheerio.load('<div>a div</div><span>a span</span>');
const $collection = $('div').add($.root()).add('span');
const expected =
'<html><head></head><body><div>a div</div><span>a span</span></body></html><div>a div</div><span>a span</span>';
expect(cheerio.html($collection)).toBe(expected);
});
it('() : does not crash with `null` as `this` value', () => {
const { html } = cheerio;
expect(html.call(null as never)).toBe('');
expect(html.call(null as never, '#nothing')).toBe('');
});
});
describe('.text', () => {
it('(cheerio object) : should return the text contents of the specified elements', () => {
const $ = cheerio.load('<a>This is <em>content</em>.</a>');
expect(cheerio.text($('a'))).toBe('This is content.');
});
it('(cheerio object) : should omit comment nodes', () => {
const $ = cheerio.load(
'<a>This is <!-- a comment --> not a comment.</a>',
);
expect(cheerio.text($('a'))).toBe('This is not a comment.');
});
it('(cheerio object) : should include text contents of children recursively', () => {
const $ = cheerio.load(
'<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>',
);
expect(cheerio.text($('a'))).toBe(
'This is a child with another child and not a comment followed by one last child and some final text.',
);
});
it('() : should return the rendered text content of the root', () => {
const $ = cheerio.load(
'<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>',
);
expect(cheerio.text($.root())).toBe(
'This is a child with another child and not a comment followed by one last child and some final text.',
);
});
it('(cheerio object) : should not omit script tags', () => {
const $ = cheerio.load('<script>console.log("test")</script>');
expect(cheerio.text($.root())).toBe('console.log("test")');
});
it('(cheerio object) : should omit style tags', () => {
const $ = cheerio.load(
'<style type="text/css">.cf-hidden { display: none; }</style>',
);
expect($.text()).toBe('.cf-hidden { display: none; }');
});
it('() : does not crash with `null` as `this` value', () => {
const { text } = cheerio;
expect(text.call(null as never)).toBe('');
});
});
describe('.parseHTML', () => {
const $ = cheerio.load('');
it('() : returns null', () => {
expect($.parseHTML()).toBe(null);
});
it('(null) : returns null', () => {
expect($.parseHTML(null)).toBe(null);
});
it('("") : returns null', () => {
expect($.parseHTML('')).toBe(null);
});
it('(largeHtmlString) : parses large HTML strings', () => {
const html = '<div></div>'.repeat(10);
const nodes = $.parseHTML(html);
expect(nodes.length).toBe(10);
expect(nodes).toBeInstanceOf(Array);
});
it('("<script>") : ignores scripts by default', () => {
const html = '<script>undefined()</script>';
expect($.parseHTML(html)).toHaveLength(0);
});
it('("<script>", true) : preserves scripts when requested', () => {
const html = '<script>undefined()</script>';
expect($.parseHTML(html, true)[0]).toHaveProperty('tagName', 'script');
});
it('("scriptAndNonScript) : preserves non-script nodes', () => {
const html = '<script>undefined()</script><div></div>';
expect($.parseHTML(html)[0]).toHaveProperty('tagName', 'div');
});
it('(scriptAndNonScript, true) : Preserves script position', () => {
const html = '<script>undefined()</script><div></div>';
expect($.parseHTML(html, true)[0]).toHaveProperty('tagName', 'script');
});
it('(text) : returns a text node', () => {
expect($.parseHTML('text')[0].type).toBe('text');
});
it('(<tab>>text) : preserves leading whitespace', () => {
expect($.parseHTML('\t<div></div>')[0]).toHaveProperty('data', '\t');
});
it('( text) : Leading spaces are treated as text nodes', () => {
expect($.parseHTML(' <div/> ')[0].type).toBe('text');
});
it('(html) : should preserve content', () => {
const html = '<div>test div</div>';
expect(cheerio($.parseHTML(html)[0]).html()).toBe('test div');
});
it('(malformedHtml) : should not break', () => {
expect($.parseHTML('<span><span>')).toHaveLength(1);
});
it('(garbageInput) : should not cause an error', () => {
expect(
$.parseHTML('<#if><tr><p>This is a test.</p></tr><#/if>'),
).toBeTruthy();
});
it('(text) : should return an array that is not effected by DOM manipulation methods', () => {
const $div = cheerio.load('<div>');
const elems = $div.parseHTML('<b></b><i></i>');
$div('div').append(elems);
expect(elems).toHaveLength(2);
});
it('(html, context) : should ignore context argument', () => {
const $div = cheerio.load('<div>');
const elems = $div.parseHTML('<script>foo</script><a>', { foo: 123 });
$div('div').append(elems);
expect(elems).toHaveLength(1);
});
it('(html, context, keepScripts) : should ignore context argument', () => {
const $div = cheerio.load('<div>');
const elems = $div.parseHTML(
'<script>foo</script><a>',
{ foo: 123 },
true,
);
$div('div').append(elems);
expect(elems).toHaveLength(2);
});
});
describe('.merge', () => {
const $ = cheerio.load('');
it('should be a function', () => {
expect(typeof $.merge).toBe('function');
});
it('(arraylike, arraylike) : should modify the first array, but not the second', () => {
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const ret = $.merge(arr1, arr2);
expect(typeof ret).toBe('object');
expect(Array.isArray(ret)).toBe(true);
expect(ret).toBe(arr1);
expect(arr1).toHaveLength(6);
expect(arr2).toHaveLength(3);
});
it('(arraylike, arraylike) : should handle objects that arent arrays, but are arraylike', () => {
const arr1: ArrayLike<string> = {
length: 3,
0: 'a',
1: 'b',
2: 'c',
};
const arr2 = {
length: 3,
0: 'd',
1: 'e',
2: 'f',
};
$.merge(arr1, arr2);
expect(arr1).toHaveLength(6);
expect(arr1[3]).toBe('d');
expect(arr1[4]).toBe('e');
expect(arr1[5]).toBe('f');
expect(arr2).toHaveLength(3);
});
it('(?, ?) : should gracefully reject invalid inputs', () => {
expect($.merge([4], 3 as never)).toBeFalsy();
expect($.merge({} as never, {} as never)).toBeFalsy();
expect($.merge([], {} as never)).toBeFalsy();
expect($.merge({} as never, [])).toBeFalsy();
const fakeArray1 = { length: 3, 0: 'a', 1: 'b', 3: 'd' };
expect($.merge(fakeArray1, [])).toBeFalsy();
expect($.merge([], fakeArray1)).toBeFalsy();
expect($.merge({ length: '7' } as never, [])).toBeFalsy();
expect($.merge({ length: -1 }, [])).toBeFalsy();
});
it('(?, ?) : should no-op on invalid inputs', () => {
const fakeArray1 = { length: 3, 0: 'a', 1: 'b', 3: 'd' };
$.merge(fakeArray1, []);
expect(fakeArray1).toHaveLength(3);
expect(fakeArray1[0]).toBe('a');
expect(fakeArray1[1]).toBe('b');
expect(fakeArray1[3]).toBe('d');
$.merge([], fakeArray1);
expect(fakeArray1).toHaveLength(3);
expect(fakeArray1[0]).toBe('a');
expect(fakeArray1[1]).toBe('b');
expect(fakeArray1[3]).toBe('d');
});
});
describe('.contains', () => {
let $: CheerioAPI;
beforeEach(() => {
$ = cheerio.load(food);
});
it('(container, contained) : should correctly detect the provided element', () => {
const $food = $('#food');
const $fruits = $('#fruits');
const $apple = $('.apple');
expect($.contains($food[0], $fruits[0])).toBe(true);
expect($.contains($food[0], $apple[0])).toBe(true);
});
it('(container, other) : should not detect elements that are not contained', () => {
const $fruits = $('#fruits');
const $vegetables = $('#vegetables');
const $apple = $('.apple');
expect($.contains($vegetables[0], $apple[0])).toBe(false);
expect($.contains($fruits[0], $vegetables[0])).toBe(false);
expect($.contains($vegetables[0], $fruits[0])).toBe(false);
expect($.contains($fruits[0], $fruits[0])).toBe(false);
expect($.contains($vegetables[0], $vegetables[0])).toBe(false);
});
});
describe('.root', () => {
it('() : should return a cheerio-wrapped root object', () => {
const $ = cheerio.load('<html><head></head><body>foo</body></html>');
$.root().append('<div id="test"></div>');
expect($.html()).toBe(
'<html><head></head><body>foo</body></html><div id="test"></div>',
);
});
});
describe('.extract', () => {
it('() : should extract values for selectors', () => {
const $ = cheerio.load(eleven);
expect(
$.extract({
red: [{ selector: '.red', value: 'outerHTML' }],
}),
).toStrictEqual({
red: [
'<li class="red">Four</li>',
'<li class="red">Five</li>',
'<li class="red sel">Nine</li>',
],
});
});
});
});