forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectors.test.ts
More file actions
75 lines (69 loc) · 1.96 KB
/
selectors.test.ts
File metadata and controls
75 lines (69 loc) · 1.96 KB
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
import pMemoize from 'p-memoize';
import {test, assert, describe} from 'vitest';
import {parseHTML} from 'linkedom';
import filenamify from 'filenamify';
import {
writeFile,
mkdir,
unlink,
readFile,
access,
} from 'node:fs/promises';
import * as exports from './selectors.js';
const fsCache = {
async get(path: string): Promise<string | undefined> {
try {
const value = await readFile(path, 'utf8');
return value;
} catch {
return undefined;
}
},
async set(path: string, contents: string): Promise<void> {
await mkdir('./test/.cache', {recursive: true});
await writeFile(path, contents);
},
async has(path: string): Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
},
async delete(path: string): Promise<void> {
await unlink(path);
},
} as const;
const fetchDocument = pMemoize(async (url: string): Promise<string> => {
const request = await fetch(url, {
headers: {
Accept: 'text/html',
},
});
return request.text();
}, {
cacheKey: ([url]) => `./test/.cache/${filenamify(url.replace('https://github.com', ''))}.html`,
cache: fsCache,
});
describe.concurrent('selectors', () => {
// Exclude URL arrays
const selectors: Array<[name: string, selector: string]> = [];
for (const [name, selector] of Object.entries(exports)) {
if (!Array.isArray(selector)) {
selectors.push([name, selector]);
}
}
test.each(selectors)('%s', {timeout: 9999}, async (name, selector: string) => {
// @ts-expect-error Index signature bs
const urls = exports[name + '_'] as exports.UrlMatch[];
assert.isArray(urls, `No URLs defined for "${name}"`);
await Promise.all(urls.map(async ([expectations, url]) => {
const html = await fetchDocument(url);
const {document} = parseHTML(html);
// TODO: ? Use snapshot with outerHTML[]
const matches = document.querySelectorAll(selector);
assert.equal(matches.length, expectations, `Got wrong number of matches on ${url}:\n${selector}`);
}));
});
});