0
0
Fork 0

fix: refs #6598 fix unit tests

This commit is contained in:
Jorge Penadés 2024-05-16 14:05:39 +02:00
parent b63735a709
commit 74e330e523
2 changed files with 11 additions and 11 deletions

View File

@ -1,5 +1,5 @@
import { vi, describe, expect, it, beforeAll, beforeEach } from 'vitest'; import { vi, describe, expect, it, beforeAll, beforeEach } from 'vitest';
import { axios, flushPromises } from 'app/test/vitest/helper'; import { axios } from 'app/test/vitest/helper';
import { useSession } from 'composables/useSession'; import { useSession } from 'composables/useSession';
import { useState } from 'composables/useState'; import { useState } from 'composables/useState';
@ -7,6 +7,7 @@ const session = useSession();
const state = useState(); const state = useState();
describe('session', () => { describe('session', () => {
describe('getToken / setToken', () => { describe('getToken / setToken', () => {
it('should return an empty string if no token is found in local or session storage', async () => { it('should return an empty string if no token is found in local or session storage', async () => {
const expectedToken = ''; const expectedToken = '';
@ -87,13 +88,15 @@ describe('session', () => {
}, },
}, },
]; ];
beforeEach(() => {
vi.spyOn(axios, 'get').mockImplementation((url) => {
if(url === 'VnUsers/acls') return Promise.resolve({data: []});
return Promise.resolve({data: { roles: rolesData, user: expectedUser }});
});
})
it('should fetch the user roles and then set token in the sessionStorage', async () => { it('should fetch the user roles and then set token in the sessionStorage', async () => {
const expectedRoles = ['salesPerson', 'admin']; const expectedRoles = ['salesPerson', 'admin'];
vi.spyOn(axios, 'get').mockResolvedValue({
data: { roles: rolesData, user: expectedUser },
});
const expectedToken = 'mySessionToken'; const expectedToken = 'mySessionToken';
const expectedTokenMultimedia = 'mySessionTokenMultimedia'; const expectedTokenMultimedia = 'mySessionTokenMultimedia';
const keepLogin = false; const keepLogin = false;
@ -117,10 +120,6 @@ describe('session', () => {
it('should fetch the user roles and then set token in the localStorage', async () => { it('should fetch the user roles and then set token in the localStorage', async () => {
const expectedRoles = ['salesPerson', 'admin']; const expectedRoles = ['salesPerson', 'admin'];
vi.spyOn(axios, 'get').mockResolvedValue({
data: { roles: rolesData, user: expectedUser },
});
const expectedToken = 'myLocalToken'; const expectedToken = 'myLocalToken';
const expectedTokenMultimedia = 'myLocalTokenMultimedia'; const expectedTokenMultimedia = 'myLocalTokenMultimedia';
const keepLogin = true; const keepLogin = true;

View File

@ -23,8 +23,9 @@ describe('Login', () => {
}, },
}; };
vi.spyOn(axios, 'post').mockResolvedValueOnce({ data: { token: 'token' } }); vi.spyOn(axios, 'post').mockResolvedValueOnce({ data: { token: 'token' } });
vi.spyOn(axios, 'get').mockResolvedValue({ vi.spyOn(axios, 'get').mockImplementation((url) => {
data: { roles: [], user: expectedUser , multimediaToken: {id:'multimediaToken' }}, if (url === 'VnUsers/acls') return Promise.resolve({ data: [] });
return Promise.resolve({data: { roles: [], user: expectedUser , multimediaToken: {id:'multimediaToken' }}});
}); });
vi.spyOn(vm.quasar, 'notify'); vi.spyOn(vm.quasar, 'notify');