summaryrefslogtreecommitdiff
path: root/source/helpers/select-has.test.ts
blob: 1b30c55974f778df5d2ff48b06f6642d488b28c1 (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
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
import {parseHTML} from 'linkedom';
import {test, assert} from 'vitest';

import select from './select-has.js';

test('basic :has() support', () => {
	const {document: fragment} = parseHTML(`
		<a>Home</a>
		<a><strong>Contacts</a>
	`);

	assert.propertyVal(select('a:has(strong)', fragment), 'textContent', 'Contacts');
});

test('returns undefined if not found', () => {
	const {document: fragment} = parseHTML(`
		<a>Home</a>
		<a><strong>Contacts</strong></a>
	`);

	assert.equal(select('a:has(em)', fragment), undefined);
});

test('supports looking for descendants in base element', () => {
	const {document: fragment} = parseHTML(`
		<a>Home</a>
		<a><em>Contacts</em> <i>icon</i></a>
	`);

	assert.propertyVal(select('a:has(em) i', fragment), 'textContent', 'icon');
});

test('supports looking for direct children in base element', () => {
	const {document: fragment} = parseHTML(`
		<a><em><span>Home <i></i></span></em></a>
		<a><span><em>Contacts <i></i></em></span></a>
	`);

	assert.propertyVal(select('a:has(> span i)', fragment), 'textContent', 'Contacts ');
});

test('throws error when there’s a space before :has()', () => {
	const {document: fragment} = parseHTML(`
		<a>Home</a>
	`);

	assert.throws(() => {
		select('a :has(em)', fragment);
	}, 'No spaces before :has() supported');
});

test('throws error when there is more than one :has()', () => {
	const {document: fragment} = parseHTML(`
		<a>Home</a>
	`);

	assert.throws(() => {
		select('a:has(em) b:has(strong)', fragment);
	}, 'Only one `:has()` required/allowed, found 2');
});

test('throws on sibling selectors', () => {
	const {document: fragment} = parseHTML(`
		<a>Home</a>
	`);

	assert.throws(() => {
		select('a:has(+a)', fragment);
	}, 'This polyfill only supports looking into the children of the base element');
});