summaryrefslogtreecommitdiff
path: root/source/github-helpers/selectors.test.ts
blob: 15a88013fab4a91c650e413a1585b3364815d9d0 (plain) (blame)
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
import mem from 'mem';
import {test, assert, describe} from 'vitest';
import {parseHTML} from 'linkedom';

import * as exports from './selectors.js';

const fetchDocument = mem(async (url: string): Promise<Window> => {
	const request = await fetch(url);
	const contents = await request.text();
	return parseHTML(contents);
});

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', async (name, selector) => {
		// @ts-expect-error Index signature bs
		const urls = exports[name + '_'] as string[];

		assert.isArray(urls, `No URLs defined for "${name}"`);
		await Promise.all(urls.map(async url => {
			const {window} = await fetchDocument(url);
			// It's not equivalent at the moment, but at least the tests don't fail. Let's see how it goes
			assert.isDefined(window.document.querySelector(selector));
		}));
	}, {timeout: 9999});
});