You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.0 KiB
56 lines
1.0 KiB
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 => {
|
|
const queue = initialCb ? [initialCb] : []
|
|
|
|
function shift () {
|
|
const job = queue.shift()
|
|
if (queue.length) {
|
|
let hasError = false
|
|
try {
|
|
job()
|
|
} catch (e) {
|
|
hasError = true
|
|
const done = queue[queue.length - 1]
|
|
if (done && done.fail) {
|
|
done.fail(e)
|
|
}
|
|
}
|
|
if (!hasError) {
|
|
if (queue.length) {
|
|
Vue.nextTick(shift)
|
|
}
|
|
}
|
|
} else if (job && job.fail) {
|
|
job() // done
|
|
}
|
|
}
|
|
|
|
Vue.nextTick(() => {
|
|
if (!queue.length || !queue[queue.length - 1].fail) {
|
|
console.warn('waitForUpdate chain is missing .then(done)')
|
|
}
|
|
shift()
|
|
})
|
|
|
|
const chainer = {
|
|
then: nextCb => {
|
|
queue.push(nextCb)
|
|
return chainer
|
|
}
|
|
}
|
|
|
|
return chainer
|
|
}
|
|
|