-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait-for-update.js
73 lines (67 loc) · 1.43 KB
/
wait-for-update.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
import Vue from 'vue'
// helper for async assertions.
// Use like this:
//
// vm.a = 123
// waitForUpdate(() => {
// expect(vm.$el.textContent).toBe('123')
// vm.a = 234
// })
// .then(() => {
// // more assertions...
// })
// .then(done)
window.waitForUpdate = initialCb => {
let end
const queue = initialCb ? [initialCb] : []
function shift () {
const job = queue.shift()
if (queue.length) {
let hasError = false
try {
job.wait ? job(shift) : job()
} catch (e) {
hasError = true
const done = queue[queue.length - 1]
if (done && done.fail) {
done.fail(e)
}
}
if (!hasError && !job.wait) {
if (queue.length) {
Vue.nextTick(shift)
}
}
} else if (job && (job.fail || job === end)) {
job() // done
}
}
Vue.nextTick(() => {
if (!queue.length || (!end && !queue[queue.length - 1].fail)) {
throw new Error('waitForUpdate chain is missing .then(done)')
}
shift()
})
const chainer = {
then: nextCb => {
queue.push(nextCb)
return chainer
},
thenWaitFor: (wait) => {
if (typeof wait === 'number') {
wait = timeout(wait)
}
wait.wait = true
queue.push(wait)
return chainer
},
end: endFn => {
queue.push(endFn)
end = endFn
}
}
return chainer
}
function timeout (n) {
return next => setTimeout(next, n)
}