-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom_spec.js
201 lines (172 loc) · 6.5 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* We are not testing transition-related stuff here,
* those are tested in transition_spec.js.
*/
var Vue = require('../../../../src/vue')
var _ = require('../../../../src/util')
if (_.inBrowser) {
describe('DOM API', function () {
var vm, vm2, parent, target, sibling, empty, spy
beforeEach(function () {
spy = jasmine.createSpy('dom')
parent = document.createElement('div')
target = document.createElement('div')
sibling = document.createElement('div')
empty = document.createElement('div')
parent.appendChild(target)
parent.appendChild(sibling)
var el = document.createElement('div')
vm = new Vue({ el: el })
// block instance
var frag = document.createDocumentFragment()
frag.appendChild(document.createElement('p'))
frag.appendChild(document.createElement('span'))
vm2 = new Vue({
el: frag
})
})
describe('$appendTo', function () {
it('normal instance', function () {
vm.$appendTo(parent, spy)
expect(parent.childNodes.length).toBe(3)
expect(parent.lastChild).toBe(vm.$el)
expect(spy.calls.count()).toBe(1)
})
it('block instance', function () {
vm2.$appendTo(parent, spy)
expect(parent.childNodes.length).toBe(6)
expect(parent.childNodes[2]).toBe(vm2._blockStart)
expect(parent.childNodes[2]).toBe(vm2.$el)
expect(parent.childNodes[3].tagName).toBe('P')
expect(parent.childNodes[4].tagName).toBe('SPAN')
expect(parent.childNodes[5]).toBe(vm2._blockEnd)
expect(spy.calls.count()).toBe(1)
})
})
describe('$prependTo', function () {
it('normal instance', function () {
vm.$prependTo(parent, spy)
expect(parent.childNodes.length).toBe(3)
expect(parent.firstChild).toBe(vm.$el)
expect(spy.calls.count()).toBe(1)
vm.$prependTo(empty, spy)
expect(empty.childNodes.length).toBe(1)
expect(empty.firstChild).toBe(vm.$el)
expect(spy.calls.count()).toBe(2)
})
it('block instance', function () {
vm2.$prependTo(parent, spy)
expect(parent.childNodes.length).toBe(6)
expect(parent.childNodes[0]).toBe(vm2._blockStart)
expect(parent.childNodes[0]).toBe(vm2.$el)
expect(parent.childNodes[1].tagName).toBe('P')
expect(parent.childNodes[2].tagName).toBe('SPAN')
expect(parent.childNodes[3]).toBe(vm2._blockEnd)
expect(spy.calls.count()).toBe(1)
// empty
vm2.$prependTo(empty, spy)
expect(empty.childNodes.length).toBe(4)
expect(empty.childNodes[0]).toBe(vm2._blockStart)
expect(empty.childNodes[0]).toBe(vm2.$el)
expect(empty.childNodes[1].tagName).toBe('P')
expect(empty.childNodes[2].tagName).toBe('SPAN')
expect(empty.childNodes[3]).toBe(vm2._blockEnd)
expect(spy.calls.count()).toBe(2)
})
})
describe('$before', function () {
it('normal instance', function () {
vm.$before(sibling, spy)
expect(parent.childNodes.length).toBe(3)
expect(parent.childNodes[1]).toBe(vm.$el)
expect(spy.calls.count()).toBe(1)
})
it('block instance', function () {
vm2.$before(sibling, spy)
expect(parent.childNodes.length).toBe(6)
expect(parent.childNodes[1]).toBe(vm2._blockStart)
expect(parent.childNodes[1]).toBe(vm2.$el)
expect(parent.childNodes[2].tagName).toBe('P')
expect(parent.childNodes[3].tagName).toBe('SPAN')
expect(parent.childNodes[4]).toBe(vm2._blockEnd)
expect(spy.calls.count()).toBe(1)
})
})
describe('$after', function () {
it('normal instance', function () {
vm.$after(target, spy)
expect(parent.childNodes.length).toBe(3)
expect(parent.childNodes[1]).toBe(vm.$el)
expect(spy.calls.count()).toBe(1)
})
it('normal instance no next sibling', function () {
vm.$after(sibling, spy)
expect(parent.childNodes.length).toBe(3)
expect(parent.lastChild).toBe(vm.$el)
expect(spy.calls.count()).toBe(1)
})
it('block instance', function () {
vm2.$after(target, spy)
expect(parent.childNodes.length).toBe(6)
expect(parent.childNodes[1]).toBe(vm2._blockStart)
expect(parent.childNodes[1]).toBe(vm2.$el)
expect(parent.childNodes[2].tagName).toBe('P')
expect(parent.childNodes[3].tagName).toBe('SPAN')
expect(parent.childNodes[4]).toBe(vm2._blockEnd)
expect(spy.calls.count()).toBe(1)
})
it('block instance no next sibling', function () {
vm2.$after(sibling, spy)
expect(parent.childNodes.length).toBe(6)
expect(parent.childNodes[2]).toBe(vm2._blockStart)
expect(parent.childNodes[2]).toBe(vm2.$el)
expect(parent.childNodes[3].tagName).toBe('P')
expect(parent.childNodes[4].tagName).toBe('SPAN')
expect(parent.childNodes[5]).toBe(vm2._blockEnd)
expect(spy.calls.count()).toBe(1)
})
})
describe('$remove', function () {
it('normal instance', function () {
vm.$before(sibling)
expect(parent.childNodes.length).toBe(3)
expect(parent.childNodes[1]).toBe(vm.$el)
vm.$remove(spy)
expect(parent.childNodes.length).toBe(2)
expect(parent.childNodes[0]).toBe(target)
expect(parent.childNodes[1]).toBe(sibling)
expect(spy.calls.count()).toBe(1)
})
it('block instance', function () {
vm2.$before(sibling)
expect(parent.childNodes.length).toBe(6)
expect(parent.childNodes[1]).toBe(vm2._blockStart)
expect(parent.childNodes[1]).toBe(vm2.$el)
expect(parent.childNodes[2].tagName).toBe('P')
expect(parent.childNodes[3].tagName).toBe('SPAN')
expect(parent.childNodes[4]).toBe(vm2._blockEnd)
vm2.$remove(spy)
expect(parent.childNodes.length).toBe(2)
expect(parent.childNodes[0]).toBe(target)
expect(parent.childNodes[1]).toBe(sibling)
expect(spy.calls.count()).toBe(1)
})
})
describe('$nextTick', function () {
it('should work', function (done) {
var context
var called = false
vm.$nextTick(function () {
called = true
context = this
})
expect(called).toBe(false)
_.nextTick(function () {
expect(called).toBe(true)
expect(context).toBe(vm)
done()
})
})
})
})
}