Add flushPromises for tests

This commit is contained in:
Pig Fang 2018-08-06 09:30:54 +08:00
parent 5e4bc4b564
commit 8a83a176ce
3 changed files with 9 additions and 10 deletions

View File

@ -1,4 +1,5 @@
import { mount } from '@vue/test-utils';
import { flushPromises } from '../../utils';
import ClosetItem from '@/components/user/ClosetItem';
import axios from 'axios';
import { swal } from '@/js/notify';
@ -86,9 +87,7 @@ test('remove texture', async () => {
await wrapper.vm.$nextTick();
button.trigger('click');
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
await flushPromises();
expect(wrapper.emitted()['item-removed'][0][0]).toBe(1);
expect(axios.post).toBeCalledWith('/user/closet/remove', { tid: 1 });
});
@ -117,9 +116,7 @@ test('set as avatar', async () => {
await wrapper.vm.$nextTick();
button.trigger('click');
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
await flushPromises();
expect(axios.post).toBeCalledWith('/user/profile/avatar', { tid: 1 });
expect(window.$).toBeCalledWith('[alt="User Image"]');
});

View File

@ -1,4 +1,5 @@
import { mount } from '@vue/test-utils';
import { flushPromises } from '../../utils';
import Profile from '@/components/user/Profile';
import axios from 'axios';
import toastr from 'toastr';
@ -102,8 +103,7 @@ test('change nickname', async () => {
expect(swal).toBeCalledWith({ type: 'warning', html: 'w' });
button.trigger('click');
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
await flushPromises();
expect(swal).toBeCalledWith({ type: 'success', html: 'o' });
});
@ -146,8 +146,7 @@ test('change email', async () => {
expect(swal).toBeCalledWith({ type: 'warning', text: 'w' });
button.trigger('click');
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick(); // There are two promises, so call it twice.
await flushPromises();
expect(swal).toBeCalledWith({ type: 'success', text: 'o' });
});

View File

@ -0,0 +1,3 @@
export function flushPromises() {
return new Promise(resolve => setImmediate(resolve));
}