salix-front/test/vitest/__tests__/App.spec.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-12-22 12:46:43 +00:00
import { vi, describe, expect, it, beforeAll } from 'vitest';
2023-01-03 13:17:22 +00:00
import { createWrapper } from 'app/test/vitest/helper';
2022-12-22 12:46:43 +00:00
import App from 'src/App.vue';
import { useSession } from 'src/composables/useSession';
const mockLoggedIn = vi.fn();
const mockDestroy = vi.fn();
const session = useSession();
vi.mock('src/composables/useSession', () => ({
useSession: () => ({
isLoggedIn: mockLoggedIn,
destroy: mockDestroy,
}),
}));
describe('App', () => {
let vm;
beforeAll(() => {
const options = {
global: {
stubs: ['router-view'],
},
};
2023-01-03 13:17:22 +00:00
vm = createWrapper(App, options).vm;
2022-12-22 12:46:43 +00:00
});
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);
2023-01-03 13:17:22 +00:00
2022-12-22 12:46:43 +00:00
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();
});
});