/
unique-selector.js
62 lines (55 loc) · 1.91 KB
/
unique-selector.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
var unique = require('unique-selector');
function assert(expr, msg) {
if (!expr) throw new Error(msg || 'failed');
}
describe('unique-selector', function() {
describe('with undefined', function() {
it('should return a TypeError', function() {
try {
unique();
assert(false);
} catch(e) {
assert(e instanceof TypeError);
}
});
});
describe('with Object', function() {
it('should return a TypeError', function() {
try {
unique({});
assert(false);
} catch(e) {
assert(e instanceof TypeError);
}
});
});
var selectors = {
// Selector: expected
'HTML > BODY':
'html > body',
'HTML > BODY > DIV#fixture':
'#fixture',
'HTML > BODY > DIV#fixture > H3 > SMALL':
'#fixture > h3 > small',
'HTML > BODY > DIV#fixture > UL#nav > LI:nth-child(1)':
'#nav > li.first.item',
'HTML > BODY > DIV#fixture > UL#nav > LI:nth-child(2)':
'#nav > li.collapsed.item:nth-child(2)',
'HTML > BODY > DIV#fixture > UL#nav > LI:nth-child(3)':
'#nav > li.last.collapsed.item',
'HTML > BODY > DIV#fixture > UL#nav > LI.item > UL#nested > LI.child > IMG':
'#nested > li.child > img[alt="Some Title"]'
};
for (selector in selectors) {
describe('with ' + selector, function() {
var expected = selectors[selector];
var actual = unique(document.querySelector(selector));
it('should return `' + expected + '`', function() {
assert(expected === actual, actual);
});
it('should have only 1 match', function() {
assert(1 === document.querySelectorAll(selector).length);
});
});
}
});