salix-front/test/cypress/support/component.js

188 lines
5.4 KiB
JavaScript
Raw Normal View History

2024-10-19 23:46:20 +00:00
import { installQuasarPlugin } from '@quasar/quasar-app-extension-testing-e2e-cypress';
2024-05-29 09:11:48 +00:00
import { createTestingPinia } from '@pinia/testing';
import { mount } from 'cypress/vue';
import { i18n } from 'src/boot/i18n';
2024-10-19 23:59:50 +00:00
import { createPinia, setActivePinia } from 'pinia';
2024-10-19 23:46:20 +00:00
import axios from 'axios';
2024-10-19 23:59:50 +00:00
import 'quasar/dist/quasar.css';
import { createRouter, createMemoryHistory } from 'vue-router';
import { useArrayDataStore } from 'src/stores/useArrayDataStore';
import { createWebHistory } from 'vue-router';
import { Quasar, Dialog, Notify } from 'quasar';
2024-10-19 23:46:20 +00:00
installQuasarPlugin({ plugins: { Dialog, Notify } });
2024-10-19 23:59:50 +00:00
setActivePinia(createPinia());
2024-10-19 23:46:20 +00:00
axios.defaults.baseURL = Cypress.env('baseUrl');
2024-10-19 23:59:50 +00:00
2024-10-19 23:46:20 +00:00
Cypress.Commands.add('mount', (component, options = {}) => {
const pinia = createTestingPinia(options.piniaOptions);
const router = createRouter({
history: createWebHistory(),
routes: [],
});
2024-05-29 09:11:48 +00:00
const defaultOptions = {
global: {
2024-10-19 23:46:20 +00:00
mocks: {
t: (tKey) => tKey,
$t: (tKey) => tKey,
2024-10-19 23:59:50 +00:00
$axios: axios,
2024-10-19 23:46:20 +00:00
},
plugins: [
Quasar,
(app) => {
app.use(router);
app.use(i18n);
app.use(pinia);
},
2024-10-19 23:59:50 +00:00
],
2024-10-19 23:46:20 +00:00
},
};
const mountOptions = Object.assign({}, defaultOptions, options);
if (options.global) {
mountOptions.global.plugins = defaultOptions.global.plugins;
}
return mount(component, mountOptions);
});
function createWrapper(component, options = {}) {
const defaultOptions = {
global: {
plugins: [
Quasar,
i18n,
createTestingPinia({
createSpy: () => cy.spy(),
}),
],
2024-10-18 11:51:41 +00:00
stubs: [
'router-link',
'router-view',
'vue-router',
'vnPaginate',
'useState',
'arrayData',
'arrayDataStore',
'useArrayData',
'useArrayDataStore',
'useStateStore',
'vue-i18n',
],
2024-05-29 09:11:48 +00:00
},
mocks: {
t: (tKey) => tKey,
$t: (tKey) => tKey,
},
};
const mountOptions = Object.assign({}, defaultOptions);
2024-10-19 23:46:20 +00:00
options.global = options?.global || {};
options.global.plugins = options.global?.plugins || [];
// options.global.plugins.push([Quasar, {}]);
// options.global.plugins.push([i18n]);
// options.global.plugins.push(createTestingPinia());
2024-05-29 09:11:48 +00:00
if (options instanceof Object) {
Object.assign(mountOptions, options);
if (options.global) {
mountOptions.global.plugins = defaultOptions.global.plugins;
}
}
2024-10-18 11:51:41 +00:00
if (!options.router) {
options.router = createRouter({
routes: [],
history: createMemoryHistory(),
});
}
mountOptions.global.plugins.push({
install(app) {
app.use(i18n);
app.use(options.router);
},
});
2024-10-19 23:46:20 +00:00
2024-05-29 09:11:48 +00:00
const wrapper = mount(component, mountOptions);
const vm = wrapper.vm;
return wrapper;
}
Cypress.Commands.add('createWrapper', createWrapper);
2024-10-18 11:51:41 +00:00
Cypress.Commands.add('_vnMount', (component, options = {}) => {
const globalConfig = {
global: {
2024-10-19 23:46:20 +00:00
plugins: [Quasar, i18n],
2024-10-18 11:51:41 +00:00
mocks: { t: (key) => key },
},
};
options.global = options.global || {};
// options.global.stubs = options.global.stubs || {};
// options.global.stubs.transition = false;
// options.global.components = options.global.components || {};
options.global.plugins = options.global.plugins || [];
options.global.plugins.push({
install(app) {
app.use(i18n);
},
});
const finalOptions = {
...globalConfig,
...options,
attrs: {
...options.attrs,
},
};
console.log(finalOptions);
const cmp = mount(component, finalOptions);
return cmp;
});
2024-10-19 23:59:50 +00:00
2024-10-18 11:51:41 +00:00
Cypress.Commands.add('vue', () => {
return cy.wrap(Cypress.vueWrapper);
});
2024-10-19 23:59:50 +00:00
2024-10-18 11:51:41 +00:00
Cypress.Commands.add('_mount', (component, options) => {
// Wrap any parent components needed
// ie: return mount(<MyProvider>{component}</MyProvider>, options)
options.global = options.global || {};
options.global.plugins = options.global.plugins || [];
options.global.plugins.push([Quasar, {}]);
options.global.plugins.push([i18n]);
return mount(component, options);
});
Cypress.Commands.add('login', (user) => {
cy.request({
method: 'POST',
2024-10-19 23:59:50 +00:00
url: 'api/accounts/login',
2024-10-18 11:51:41 +00:00
body: {
user: user,
password: 'nightmare',
},
}).then((response) => {
window.localStorage.setItem('token', response.body.token);
cy.request({
method: 'GET',
2024-10-19 23:59:50 +00:00
url: 'api/VnUsers/ShareToken',
2024-10-18 11:51:41 +00:00
headers: {
Authorization: window.localStorage.getItem('token'),
},
}).then(({ body }) => {
window.localStorage.setItem('tokenMultimedia', body.multimediaToken.id);
});
});
});
2024-10-19 23:46:20 +00:00
Cypress.Commands.add('stubUseArrayDataStore', (stubData = {}) => {
const arrayDataStore = useArrayDataStore();
if (arrayDataStore.get.restore) {
arrayDataStore.get.restore();
}
cy.stub(arrayDataStore, 'get').returns(stubData).as('getStub');
});