2023-03-02 09:47:06 +00:00
|
|
|
import { installQuasarPlugin } from '@quasar/quasar-app-extension-testing-unit-vitest';
|
2023-01-03 13:17:22 +00:00
|
|
|
import { mount, flushPromises } from '@vue/test-utils';
|
|
|
|
import { createTestingPinia } from '@pinia/testing';
|
|
|
|
import { vi } from 'vitest';
|
|
|
|
import { i18n } from 'src/boot/i18n';
|
|
|
|
import { Notify, Dialog } from 'quasar';
|
|
|
|
import axios from 'axios';
|
2023-08-25 08:56:03 +00:00
|
|
|
import * as useValidator from 'src/composables/useValidator';
|
2023-01-03 13:17:22 +00:00
|
|
|
|
2023-03-02 09:47:06 +00:00
|
|
|
installQuasarPlugin({
|
2023-01-03 13:17:22 +00:00
|
|
|
plugins: {
|
|
|
|
Notify,
|
|
|
|
Dialog,
|
|
|
|
},
|
|
|
|
});
|
2023-02-24 12:21:37 +00:00
|
|
|
const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false });
|
2023-01-03 13:17:22 +00:00
|
|
|
const mockPush = vi.fn();
|
2024-06-18 13:20:07 +00:00
|
|
|
const mockReplace = vi.fn();
|
2023-02-24 12:21:37 +00:00
|
|
|
|
2023-01-03 13:17:22 +00:00
|
|
|
vi.mock('vue-router', () => ({
|
|
|
|
useRouter: () => ({
|
|
|
|
push: mockPush,
|
2024-06-18 13:20:07 +00:00
|
|
|
replace: mockReplace,
|
2023-02-22 13:55:29 +00:00
|
|
|
currentRoute: {
|
|
|
|
value: {
|
|
|
|
params: {
|
|
|
|
id: 1,
|
|
|
|
},
|
2024-05-28 11:11:38 +00:00
|
|
|
meta: { moduleName: 'mockName' },
|
2024-06-18 13:20:07 +00:00
|
|
|
matched: [{ path: 'mockName/list' }],
|
2023-02-22 13:55:29 +00:00
|
|
|
},
|
|
|
|
},
|
2023-01-03 13:17:22 +00:00
|
|
|
}),
|
|
|
|
useRoute: () => ({
|
|
|
|
matched: [],
|
2023-02-24 12:21:37 +00:00
|
|
|
query: {},
|
|
|
|
params: {},
|
2024-05-28 11:11:38 +00:00
|
|
|
meta: { moduleName: 'mockName' },
|
2024-06-18 13:20:07 +00:00
|
|
|
path: 'mockSection/list',
|
2023-01-03 13:17:22 +00:00
|
|
|
}),
|
2024-07-04 15:31:40 +00:00
|
|
|
onBeforeRouteLeave: () => {},
|
2023-01-03 13:17:22 +00:00
|
|
|
}));
|
|
|
|
|
2023-10-04 13:46:15 +00:00
|
|
|
vi.mock('axios');
|
|
|
|
|
2023-08-25 08:56:03 +00:00
|
|
|
vi.spyOn(useValidator, 'useValidator').mockImplementation(() => {
|
2023-10-04 13:46:15 +00:00
|
|
|
return { validate: vi.fn() };
|
2023-08-25 08:56:03 +00:00
|
|
|
});
|
|
|
|
|
2023-02-23 14:20:07 +00:00
|
|
|
class FormDataMock {
|
|
|
|
append() {
|
|
|
|
vi.fn();
|
|
|
|
}
|
|
|
|
delete() {
|
|
|
|
vi.fn();
|
|
|
|
}
|
|
|
|
get() {
|
|
|
|
vi.fn();
|
|
|
|
}
|
|
|
|
getAll() {
|
|
|
|
vi.fn();
|
|
|
|
}
|
|
|
|
has() {
|
|
|
|
vi.fn();
|
|
|
|
}
|
|
|
|
set() {
|
|
|
|
vi.fn();
|
|
|
|
}
|
|
|
|
forEach() {
|
|
|
|
vi.fn();
|
|
|
|
}
|
|
|
|
}
|
2024-10-28 12:24:46 +00:00
|
|
|
|
2023-02-23 14:20:07 +00:00
|
|
|
global.FormData = FormDataMock;
|
2023-10-03 07:25:28 +00:00
|
|
|
global.URL = class URL {};
|
2024-10-28 12:24:46 +00:00
|
|
|
global.Date.vnNew = () => new Date(Date.UTC(2001, 0, 1, 11));
|
2023-02-22 13:55:29 +00:00
|
|
|
|
2023-01-03 13:17:22 +00:00
|
|
|
export function createWrapper(component, options) {
|
|
|
|
const defaultOptions = {
|
|
|
|
global: {
|
|
|
|
plugins: [i18n, pinia],
|
|
|
|
},
|
2023-08-21 13:14:19 +00:00
|
|
|
mocks: {
|
|
|
|
t: (tKey) => tKey,
|
|
|
|
$t: (tKey) => tKey,
|
|
|
|
},
|
2023-01-03 13:17:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const mountOptions = Object.assign({}, defaultOptions);
|
|
|
|
|
|
|
|
if (options instanceof Object) {
|
|
|
|
Object.assign(mountOptions, options);
|
|
|
|
|
|
|
|
if (options.global) {
|
|
|
|
mountOptions.global.plugins = defaultOptions.global.plugins;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = mount(component, mountOptions);
|
|
|
|
const vm = wrapper.vm;
|
|
|
|
|
|
|
|
return { vm, wrapper };
|
|
|
|
}
|
|
|
|
|
|
|
|
export { axios, flushPromises };
|