-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
103 lines (76 loc) · 2.34 KB
/
test.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
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
import test from 'tape';
import {$, $$, lastElement, elementExists, expectElement, ElementNotFoundError} from './index.js';
document.body.innerHTML = `
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Qux</li>
</ul>
<ul>
<li>Lorem</li>
<li>Ipsum</li>
</ul>
`;
test('selects one element', t => {
t.plan(1);
const li = document.querySelector('ul li');
t.equal($('ul li'), li);
});
test('selects one element within an ancestor', t => {
t.plan(1);
const li = document.querySelector('ul li');
t.equal($('li', $('ul')), li);
});
test('expects at least one element', t => {
t.plan(2);
const li = document.querySelector('ul li');
t.equal(expectElement('ul li'), li);
t.throws(() => expectElement('lololol'));
});
test('expects one element within an ancestor', t => {
t.plan(2);
const li = document.querySelector('ul li');
t.equal(expectElement('li', expectElement('ul')), li);
t.throws(() => expectElement('ul', expectElement('li')), error => error instanceof ElementNotFoundError);
});
test('selects the last element', t => {
t.plan(1);
const li = [...document.querySelectorAll('ul li')].pop();
t.equal(lastElement('ul li'), li);
});
test('selects the last element within an ancestor', t => {
t.plan(1);
const li = [...document.querySelectorAll('ul li')].pop();
t.equal(lastElement('li', lastElement('ul')), li);
});
test('tests existence of one element', t => {
t.plan(2);
t.true(elementExists('ul li'));
t.false(elementExists('lololol'));
});
test('tests existence of one element within an ancestor', t => {
t.plan(3);
t.true(elementExists('li', $('ul')));
t.false(elementExists('ul', $('li')));
t.false(elementExists('ul', $('lololol')));
});
test('selects all elements', t => {
t.plan(1);
const li = document.querySelectorAll('ul li');
t.deepEqual($$('ul li'), [...li]);
});
test('selects all elements within an ancestor', t => {
t.plan(1);
const li = document.querySelector('ul').querySelectorAll('ul li');
t.deepEqual($$('li', $('ul')), [...li]);
});
test('selects all elements within an array of ancestors', t => {
t.plan(1);
const li = document.querySelectorAll('ul li');
t.deepEqual($$('li', $$('ul')), [...li]);
});
test('selects all elements within an array of ancestors without duplicates', t => {
t.plan(1);
const li = document.querySelector('ul').querySelectorAll('li');
t.deepEqual($$('li', [$('ul'), $('ul')]), [...li]);
});