Updated unit test
gitea/salix-front/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2022-08-08 11:17:03 +02:00
parent 7b2a4d75c6
commit c16ef7e783
1 changed files with 60 additions and 18 deletions

View File

@ -1,8 +1,12 @@
import { jest, describe, expect, it, beforeAll } from '@jest/globals'; import { jest, describe, expect, it, beforeAll } from '@jest/globals';
import { createWrapper } from 'app/tests/jest/jestHelpers'; import { createWrapper } from 'app/tests/jest/jestHelpers';
import App from '../App.vue'; import App from '../App.vue';
import { useSession } from 'src/composables/useSession';
const mockPush = jest.fn(); const mockPush = jest.fn();
const mockLoggedIn = jest.fn();
const mockDestroy = jest.fn();
const session = useSession();
jest.mock('vue-router', () => ({ jest.mock('vue-router', () => ({
useRouter: () => ({ useRouter: () => ({
@ -11,12 +15,19 @@ jest.mock('vue-router', () => ({
}), }),
})); }));
// jest.mock('vue-i18n', () => ({ jest.mock('src/composables/useSession', () => ({
// createI18n: () => { }, useSession: () => ({
// useI18n: () => ({ isLoggedIn: mockLoggedIn,
// t: () => { } destroy: mockDestroy
// }), }),
// })); }));
jest.mock('vue-i18n', () => ({
createI18n: () => { },
useI18n: () => ({
t: () => { }
}),
}));
describe('App', () => { describe('App', () => {
let vm; let vm;
@ -27,33 +38,64 @@ describe('App', () => {
} }
}; };
vm = createWrapper(App, options).vm; vm = createWrapper(App, options).vm;
}); });
it('should not set the token into session if any error occurred', async () => { it('should return a login error message', async () => {
// jest.spyOn(vm.routerView).mockReturnValue(routerView); jest.spyOn(vm.quasar, 'notify');
jest.spyOn(vm.quasar, 'notify')
session.isLoggedIn.mockReturnValue(false);
const response = {
response: {
status: 401
}
};
let error; let error;
try { try {
const response = {
response: {
status: 403
}
};
await vm.responseError(response); await vm.responseError(response);
} catch (e) { } catch (e) {
error = e; error = e;
} }
expect(error).toEqual(expect.objectContaining(response));
//await vm.onSubmit();
expect(vm.quasar.notify).toHaveBeenCalledWith(expect.objectContaining( expect(vm.quasar.notify).toHaveBeenCalledWith(expect.objectContaining(
{ {
type: 'negative', type: 'negative',
message: 'test' message: 'login.loginError'
} }
)); ));
}); });
it('should return an unauthorized error message', async () => {
jest.spyOn(vm.quasar, 'notify');
session.isLoggedIn.mockReturnValue(true);
const response = {
response: {
status: 401
}
};
let error;
try {
await vm.responseError(response);
} catch (e) {
error = e;
}
expect(error).toEqual(expect.objectContaining(response));
expect(vm.quasar.notify).toHaveBeenCalledWith(expect.objectContaining(
{
type: 'negative',
message: 'errors.statusUnauthorized'
}
));
expect(session.destroy).toHaveBeenCalled();
});
}); });