-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.spec.js
86 lines (68 loc) · 2.1 KB
/
test.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
import {ref, onMounted, defineComponent} from 'vue';
import {it, expect} from 'vitest';
import {mount, flushPromises} from '@vue/test-utils';
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const TestAsync = defineComponent({
template: '<div><div>{{ mountText }}</div><div>{{ asyncText }}</div></div>',
props: {
done: Function,
},
setup({ done }) {
const mountText = ref();
const asyncText = ref();
onMounted(() => {
mountText.value = 'mounted';
});
sleep(0).then(() => {
asyncText.value = 'async';
done?.();
});
return {
mountText,
asyncText,
};
},
});
it('should show onMount text', () => new Promise(done => {
const wrapper = mount(TestAsync);
wrapper.vm.$nextTick(() => {
expect(wrapper.html()).toMatch('mounted')
done();
});
}));
it('should show async text', async () => {
let renderedAsyncResolve;
const renderedAsync = new Promise(resolve => renderedAsyncResolve = resolve);
const wrapper = mount(TestAsync, {
propsData: { done: renderedAsyncResolve }
});
await renderedAsync;
expect(wrapper.html()).toMatch('async');
});
it('should show async text with nextTick', () => new Promise(async (done) => {
let renderedAsyncResolve;
const renderedAsync = new Promise(resolve => renderedAsyncResolve = resolve);
const wrapper = mount(TestAsync, {
propsData: { done: renderedAsyncResolve }
});
await renderedAsync;
wrapper.vm.$nextTick(() => {
expect(wrapper.html()).toMatch('async');
done();
});
}));
it('with flushPromises', () => new Promise(async (done) => {
let renderedAsyncResolve;
const renderedAsync = new Promise(resolve => renderedAsyncResolve = resolve);
const wrapper = mount(TestAsync, {
propsData: { done: renderedAsyncResolve }
});
await renderedAsync;
await flushPromises();
wrapper.vm.$nextTick(() => {
expect(wrapper.html()).toMatch('async');
done();
});
}));