0
0
Fork 0
salix-front-mindshore-fork2/test/vitest/__tests__/App.spec.js

91 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-12-22 12:46:43 +00:00
import { installQuasar } from '@quasar/quasar-app-extension-testing-unit-vitest';
import { vi, describe, expect, it, beforeAll } from 'vitest';
// import { createWrapper } from 'app/test/jest/jestHelpers';
import { mount } from '@vue/test-utils';
import { i18n } from 'src/boot/i18n';
import { Notify, Dialog } from 'quasar';
import App from 'src/App.vue';
import { useSession } from 'src/composables/useSession';
installQuasar({
plugins: {
Notify,
Dialog,
},
});
const mockPush = vi.fn();
const mockLoggedIn = vi.fn();
const mockDestroy = vi.fn();
const session = useSession();
vi.mock('vue-router', () => ({
useRouter: () => ({
push: mockPush,
currentRoute: { value: 'myCurrentRoute' },
}),
}));
vi.mock('src/composables/useSession', () => ({
useSession: () => ({
isLoggedIn: mockLoggedIn,
destroy: mockDestroy,
}),
}));
describe('App', () => {
let vm;
beforeAll(() => {
const options = {
global: {
stubs: ['router-view'],
plugins: [i18n],
},
};
vm = mount(App, options).vm;
});
it('should return a login error message', async () => {
vi.spyOn(vm.quasar, 'notify');
session.isLoggedIn.mockReturnValue(false);
const response = {
response: {
status: 401,
},
};
expect(vm.responseError(response)).rejects.toEqual(expect.objectContaining(response));
expect(vm.quasar.notify).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Invalid username or password',
type: 'negative',
})
);
});
it('should return an unauthorized error message', async () => {
vi.spyOn(vm.quasar, 'notify');
session.isLoggedIn.mockReturnValue(true);
const response = {
response: {
status: 401,
},
};
expect(vm.responseError(response)).rejects.toEqual(expect.objectContaining(response));
expect(vm.quasar.notify).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Access denied',
type: 'negative',
})
);
expect(session.destroy).toHaveBeenCalled();
});
});