-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-selector-lite.spec.js
50 lines (49 loc) · 2.01 KB
/
query-selector-lite.spec.js
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
const fs = require('fs').promises;
const Document = require('./query-selector-lite');
describe('queryselectorlite', () => {
const testCases = [
{
file: 'test.html',
test: [
{ selector: 'h3', expect: 'tagTest' },
{ selector: '.abcdef', expect: 'classTest' },
{ selector: '.abcdef, .secondClass', expect: 'classTest' },
{ selector: '.not-found, .secondClass', expect: 'classTest' },
{ selector: '#testid', expect: 'idTest' },
{ selector: '.secondClass', expect: 'classTest' },
{ selector: '#testid', expect: 'idTest' },
{ selector: 'not-found', expect: null },
{ selector: '.not-found', expect: null },
{ selector: '#not-found', expect: null },
],
},
];
for (const testCase of testCases) {
for (const test of testCase.test) {
it(`should test querySelector ${test.selector} in file ${testCase.file}`, async () => {
const doc = new Document();
await doc.loadFile(testCase.file);
const result = doc.querySelector(test.selector);
expect(result ? result.innerText : null).toBe(test.expect);
});
it(`should test querySelectorAll ${test.selector} in file ${testCase.file}`, async () => {
const doc = new Document();
await doc.loadFile(testCase.file);
const result = doc.querySelectorAll(test.selector)[0];
expect(result ? result.innerText : null).toBe(test.expect);
});
it(`should test querySelectorAll ${test.selector} in file ${testCase.file} pre loaded`, async () => {
const html = await (await fs.readFile(testCase.file)).toString();
const doc = new Document(html);
const result = doc.querySelectorAll(test.selector)[0];
expect(result ? result.innerText : null).toBe(test.expect);
});
}
}
it('should properly use grouping selectrs', async () => {
const doc = new Document();
await doc.loadFile('test.html');
const result = doc.querySelectorAll('div,h3');
expect(result.length).toBe(5)
});
});