-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransition.js
359 lines (293 loc) · 11.3 KB
/
transition.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
describe('Transition', function () {
var transition = require('vue/src/transition'),
config = require('vue/src/config'),
codes = transition.codes,
endEvents = transition.sniff(),
enterClass = config.enterClass,
leaveClass = config.leaveClass,
nextTick = Vue.nextTick
describe('General', function () {
it('should skip if compiler is in init stage', function () {
var c = mockChange(),
compiler = mockCompiler()
compiler.init = true
var code = transition(null, 1, c.change, compiler)
assert.ok(c.called)
assert.strictEqual(code, codes.INIT)
assert.ok(compiler.attached)
})
it('should skip if no transition is found on the node', function () {
var c = mockChange(),
compiler = mockCompiler(),
code = transition(mockEl(), 1, c.change, compiler)
assert.ok(c.called)
assert.strictEqual(code, codes.SKIP)
assert.ok(compiler.attached)
})
})
describe('CSS Transitions', function () {
if (!endEvents.trans) { // IE9 only test case
it('should skip if transition is not available', function () {
var c = mockChange(),
compiler = mockCompiler(),
code = transition(mockEl('css'), 1, c.change, compiler)
assert.ok(c.called)
assert.strictEqual(code, codes.CSS_SKIP)
assert.ok(compiler.attached)
})
// skip the rest
return
}
describe('enter: transition', function () {
var el = mockEl('css'),
c = mockChange(function () {
c.called = true
assert.ok(el.classList.contains(enterClass))
}),
compiler = mockCompiler(),
code,
cbCalled = false
el.vue_trans_cb = function () {
cbCalled = true
}
el.addEventListener(endEvents.trans, el.vue_trans_cb)
it('should add the class before calling changeState()', function () {
code = transition(el, 1, c.change, compiler)
assert.ok(c.called)
})
it('should remove unfinished leave callback if exists', function () {
assert.notOk(el.vue_trans_cb)
var e = mockHTMLEvent(endEvents.trans)
el.dispatchEvent(e)
assert.notOk(cbCalled)
})
it('should remove the v-leave class if the leave callback exists', function () {
var el = mockEl('css')
document.body.appendChild(el)
el.style.width = '1px'
code = transition(el, -1, function(){}, compiler)
code = transition(el, 1, function(){}, compiler)
assert.notOk(el.classList.contains(leaveClass))
})
it('should remove the class afterwards', function (done) {
nextTick(function () {
assert.notOk(el.classList.contains(enterClass))
done()
})
})
it('should return correct code', function () {
assert.strictEqual(code, codes.CSS_E)
})
it('should have called attached hook', function () {
assert.ok(compiler.attached)
})
})
describe('enter: animation', function () {
var el = mockEl('css'),
c = mockChange(function () {
c.called = true
assert.ok(el.classList.contains(enterClass))
}),
compiler = mockCompiler(),
code
// mark it to use CSS animation instead of transition
el.vue_anim = ''
before(function () {
document.body.appendChild(el)
})
after(function () {
document.body.removeChild(el)
})
it('should add enterClass before calling changeState()', function () {
code = transition(el, 1, c.change, compiler)
assert.ok(c.called)
})
it('should still have the class on nextTick', function (done) {
nextTick(function () {
assert.ok(el.classList.contains(enterClass))
done()
})
})
it('should remove the class when the animation is done', function (done) {
el.addEventListener(endEvents.anim, function () {
assert.notOk(el.vue_trans_cb)
assert.notOk(el.classList.contains(enterClass))
done()
})
var e = mockHTMLEvent(endEvents.anim)
el.dispatchEvent(e)
})
})
describe('leave', function () {
var el = mockEl('css'),
c = mockChange(),
compiler = mockCompiler(),
code
before(function () {
document.body.appendChild(el)
})
after(function () {
document.body.removeChild(el)
})
it('should call change immediately if el is invisible', function () {
var el = mockEl('css'),
c = mockChange(),
compiler = mockCompiler()
code = transition(el, -1, c.change, compiler)
assert.ok(c.called)
assert.ok(compiler.detached)
})
it('should attach an ontransitionend listener', function () {
el.style.width = '1px'
code = transition(el, -1, c.change, compiler)
assert.ok(typeof el.vue_trans_cb === 'function')
})
it('should add the class', function () {
assert.ok(el.classList.contains(leaveClass))
})
it('should call changeState on transitionend', function () {
var e = mockHTMLEvent(endEvents.trans)
el.dispatchEvent(e)
assert.ok(c.called)
})
it('should remove the callback after called', function () {
assert.notOk(el.vue_trans_cb)
var e = mockHTMLEvent(endEvents.trans)
el.dispatchEvent(e)
assert.strictEqual(c.n, 1)
})
it('should remove the class after called', function () {
assert.notOk(el.classList.contains(leaveClass))
})
it('should return correct code', function () {
assert.strictEqual(code, codes.CSS_L)
})
it('should have called detached hook', function () {
assert.ok(compiler.detached)
})
})
})
describe('JavaScript Transitions', function () {
it('should skip if correspinding option is not defined', function () {
var c = mockChange(),
compiler = mockCompiler(),
code = transition(mockEl('js'), 1, c.change, compiler)
assert.ok(c.called)
assert.strictEqual(code, codes.JS_SKIP)
assert.ok(compiler.attached)
})
it('should skip if the option is given but the enter/leave func is not defined', function () {
var c = mockChange(),
compiler = mockCompiler({}),
code = transition(mockEl('js'), 1, c.change, compiler)
assert.ok(c.called)
assert.strictEqual(code, codes.JS_SKIP_E)
assert.ok(compiler.attached)
c = mockChange()
compiler = mockCompiler({})
code = transition(mockEl('js'), -1, c.change, compiler)
assert.ok(c.called)
assert.strictEqual(code, codes.JS_SKIP_L)
assert.ok(compiler.detached)
})
describe('enter', function () {
var code,
c = mockChange(),
el = mockEl('js'),
def = {
enter: function (element, change) {
assert.strictEqual(el, element)
change()
}
},
compiler = mockCompiler(def)
it('should call the enter function', function () {
code = transition(el, 1, c.change, compiler)
assert.ok(c.called)
})
it('should return correct code', function () {
assert.strictEqual(code, codes.JS_E)
})
it('should have called attached hook', function () {
assert.ok(compiler.attached)
})
})
describe('leave', function () {
var code,
c = mockChange(),
el = mockEl('js'),
def = {
leave: function (element, change) {
assert.strictEqual(el, element)
change()
}
},
compiler = mockCompiler(def)
it('should call the leave function', function () {
code = transition(el, -1, c.change, compiler)
assert.ok(c.called)
})
it('should return correct code', function () {
assert.strictEqual(code, codes.JS_L)
})
it('should have called detached hook', function () {
assert.ok(compiler.detached)
})
})
describe('wrapped timeout', function () {
var el = mockEl('js'),
c = mockChange(),
timerFired = false,
def = {
enter: function (el, change, timeout) {
change()
timeout(function () {
timerFired = true
}, 0)
},
leave: function () {}
},
compiler = mockCompiler(def)
it('should cancel previous unfired timers', function (done) {
transition(el, 1, c.change, compiler)
assert.strictEqual(el.vue_timeouts.length, 1)
transition(el, -1, c.change, compiler)
assert.strictEqual(el.vue_timeouts.length, 0)
setTimeout(function () {
assert.notOk(timerFired)
done()
}, 0)
})
})
})
function mockChange (change) {
var c = {
called: false,
n: 0,
change: change || function () {
c.called = true
c.n += 1
}
}
return c
}
function mockEl (type) {
var el = document.createElement('div')
if (type === 'css') {
el.vue_trans = ''
} else if (type === 'js') {
el.vue_effect = 'test'
}
return el
}
function mockCompiler (opt) {
return {
getOption: function () {
return opt
},
execHook: function (hook) {
this[hook] = true
}
}
}
})