-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom_spec.js
122 lines (105 loc) · 3.41 KB
/
dom_spec.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var _ = require('../../../../src/util')
if (_.inBrowser) {
describe('Util - DOM', function () {
var parent, child, target
function div () {
return document.createElement('div')
}
beforeEach(function () {
parent = div()
child = div()
target = div()
parent.appendChild(child)
})
it('inDoc', function () {
expect(_.inDoc(target)).toBe(false)
document.body.appendChild(target)
expect(_.inDoc(target)).toBe(true)
document.body.removeChild(target)
expect(_.inDoc(target)).toBe(false)
})
it('attr', function () {
target.setAttribute('v-test', 'ok')
var val = _.attr(target, 'test')
expect(val).toBe('ok')
expect(target.hasAttribute('v-test')).toBe(false)
})
it('before', function () {
_.before(target, child)
expect(target.parentNode).toBe(parent)
expect(target.nextSibling).toBe(child)
})
it('after', function () {
_.after(target, child)
expect(target.parentNode).toBe(parent)
expect(child.nextSibling).toBe(target)
})
it('after with sibling', function () {
var sibling = div()
parent.appendChild(sibling)
_.after(target, child)
expect(target.parentNode).toBe(parent)
expect(child.nextSibling).toBe(target)
})
it('remove', function () {
_.remove(child)
expect(child.parentNode).toBeNull()
expect(parent.childNodes.length).toBe(0)
})
it('prepend', function () {
_.prepend(target, parent)
expect(target.parentNode).toBe(parent)
expect(parent.firstChild).toBe(target)
})
it('prepend to empty node', function () {
parent.removeChild(child)
_.prepend(target, parent)
expect(target.parentNode).toBe(parent)
expect(parent.firstChild).toBe(target)
})
it('replace', function () {
_.replace(child, target)
expect(parent.childNodes.length).toBe(1)
expect(parent.firstChild).toBe(target)
})
it('on/off', function () {
// IE requires element to be in document to fire events
document.body.appendChild(target)
var spy = jasmine.createSpy()
_.on(target, 'click', spy)
var e = document.createEvent('HTMLEvents')
e.initEvent('click', true, true)
target.dispatchEvent(e)
expect(spy.calls.count()).toBe(1)
expect(spy).toHaveBeenCalledWith(e)
_.off(target, 'click', spy)
target.dispatchEvent(e)
expect(spy.calls.count()).toBe(1)
document.body.removeChild(target)
})
it('addClass/removeClass', function () {
var el = document.createElement('div')
el.className = 'aa bb cc'
_.removeClass(el, 'bb')
expect(el.className).toBe('aa cc')
_.removeClass(el, 'aa')
expect(el.className).toBe('cc')
_.addClass(el, 'bb')
expect(el.className).toBe('cc bb')
_.addClass(el, 'bb')
expect(el.className).toBe('cc bb')
})
it('addClass/removeClass for SVG/IE9', function () {
var el = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
el.setAttribute('class', 'aa bb cc')
_.removeClass(el, 'bb')
expect(el.getAttribute('class')).toBe('aa cc')
_.removeClass(el, 'aa')
expect(el.getAttribute('class')).toBe('cc')
_.addClass(el, 'bb')
expect(el.getAttribute('class')).toBe('cc bb')
_.addClass(el, 'bb')
expect(el.getAttribute('class')).toBe('cc bb')
})
})
}