forked from verdnatura/salix-front
58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
import { jest, describe, expect, it, beforeAll } from '@jest/globals';
|
|
import { createWrapper, axios } from 'app/tests/jest/jestHelpers';
|
|
import Login from '../Login.vue';
|
|
|
|
const mockPush = jest.fn();
|
|
|
|
jest.mock('vue-router', () => ({
|
|
useRouter: () => ({
|
|
push: mockPush,
|
|
currentRoute: { value: 'myCurrentRoute' }
|
|
}),
|
|
}));
|
|
|
|
describe('Login', () => {
|
|
let vm;
|
|
beforeAll(() => {
|
|
vm = createWrapper(Login).vm;
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should successfully set the token into session', async () => {
|
|
const expectedUser = {
|
|
id: 999,
|
|
name: `T'Challa`,
|
|
nickname: 'Black Panther',
|
|
lang: 'en',
|
|
userConfig: {
|
|
darkMode: false,
|
|
}
|
|
}
|
|
jest.spyOn(axios, 'post').mockResolvedValue({ data: { token: 'token' } });
|
|
jest.spyOn(axios, 'get').mockResolvedValue({ data: { roles: [], user: expectedUser } });
|
|
jest.spyOn(vm.quasar, 'notify');
|
|
|
|
expect(vm.session.getToken()).toEqual('');
|
|
|
|
await vm.onSubmit();
|
|
|
|
expect(vm.session.getToken()).toEqual('token');
|
|
expect(vm.quasar.notify).toHaveBeenCalledWith(expect.objectContaining(
|
|
{ 'type': 'positive' }
|
|
));
|
|
vm.session.destroy();
|
|
});
|
|
|
|
it('should not set the token into session if any error occurred', async () => {
|
|
jest.spyOn(axios, 'post').mockReturnValue({ data: null });
|
|
jest.spyOn(vm.quasar, 'notify');
|
|
|
|
await vm.onSubmit();
|
|
|
|
expect(vm.quasar.notify).not.toHaveBeenCalled();
|
|
});
|
|
});
|