-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
73 lines (48 loc) · 1.79 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
var test = require('tape')
, document = require('global/document')
, body = document.body
, cssPath = require('./css-path')
test('the body element', function (t) {
t.equal(cssPath(document.body), 'html > body')
t.end()
})
test('elements not attached to the body', function (t) {
var div = document.createElement('div')
, span = document.createElement('span')
t.equal(cssPath(div), 'div')
div.setAttribute('class', 'beep boop')
t.equal(cssPath(div), 'div.beep.boop')
div.setAttribute('id', 'hello')
t.equal(cssPath(div), 'div#hello')
div.appendChild(span)
t.equal(cssPath(span), 'div#hello > span:nth-child(1)')
t.equal(cssPath(span, div), 'span:nth-child(1)')
t.end()
})
test('attributes with some whitespace', function (t) {
var elm = document.createElement('div')
elm.setAttribute('class', ' foo\tbar\n')
t.equal(cssPath(elm), 'div.foo.bar')
elm.setAttribute('class', '')
t.equal(cssPath(elm), 'div')
elm.setAttribute('id', ' bong ')
t.equal(cssPath(elm), 'div#bong')
t.end()
})
test('elements attached to the body', function (t) {
var div = document.createElement('div')
, div2 = document.createElement('div')
, text = document.createTextNode('hello, world!')
// use insertBefore so that it works nice with testling
body.insertBefore(div, body.childNodes[0])
div.setAttribute('class', 'foo bar')
t.equal(cssPath(div), 'html > body > div.foo.bar:nth-child(1)')
body.insertBefore(text, body.childNodes[1])
body.insertBefore(div2, body.childNodes[2])
t.equal(cssPath(div2), 'html > body > div:nth-child(2)')
t.equal(cssPath(div2, document.documentElement), 'body > div:nth-child(2)')
t.equal(cssPath(div2, body), 'div:nth-child(2)')
div.setAttribute('id', 'identifier')
t.equal(cssPath(div), 'div#identifier')
t.end()
})