0
0
Fork 0
salix-front-mindshore-fork2/test/vitest/helper.js

95 lines
2.0 KiB
JavaScript

import { installQuasarPlugin } from '@quasar/quasar-app-extension-testing-unit-vitest';
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';
import * as useValidator from 'src/composables/useValidator';
installQuasarPlugin({
plugins: {
Notify,
Dialog,
},
});
axios.defaults.baseURL = 'http://localhost:9000/api/';
const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false });
const mockPush = vi.fn();
vi.mock('vue-router', () => ({
useRouter: () => ({
push: mockPush,
currentRoute: {
value: {
params: {
id: 1,
},
},
},
}),
useRoute: () => ({
matched: [],
query: {},
params: {},
}),
}));
vi.spyOn(useValidator, 'useValidator').mockImplementation(() => {
return { validate: vi.fn(), fetch: vi.fn() };
});
class FormDataMock {
append() {
vi.fn();
}
delete() {
vi.fn();
}
get() {
vi.fn();
}
getAll() {
vi.fn();
}
has() {
vi.fn();
}
set() {
vi.fn();
}
forEach() {
vi.fn();
}
}
global.FormData = FormDataMock;
export function createWrapper(component, options) {
const defaultOptions = {
global: {
plugins: [i18n, pinia],
},
mocks: {
t: (tKey) => tKey,
$t: (tKey) => tKey,
},
};
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 };