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

136 lines
4.4 KiB
JavaScript
Raw Normal View History

2023-01-03 13:17:22 +00:00
import { vi, describe, expect, it } from 'vitest';
import { axios } from 'app/test/vitest/helper';
import { useSession } from 'composables/useSession';
import { useState } from 'composables/useState';
2022-12-20 11:30:25 +00:00
const session = useSession();
const state = useState();
describe('session', () => {
describe('getToken / setToken', () => {
it('should return an empty string if no token is found in local or session storage', async () => {
2022-12-22 12:46:43 +00:00
const expectedToken = '';
2022-12-20 11:30:25 +00:00
const token = session.getToken();
expect(token).toEqual(expectedToken);
});
it('should return the token stored in local or session storage', async () => {
2022-12-22 12:46:43 +00:00
const expectedToken = 'myToken';
2022-12-20 11:30:25 +00:00
const data = {
token: expectedToken,
2022-12-22 12:46:43 +00:00
keepLogin: false,
};
2022-12-20 11:30:25 +00:00
session.setToken(data);
const token = session.getToken();
expect(token).toEqual(expectedToken);
});
});
describe('destroy', () => {
it('should remove the token from the local storage and set a blank user', async () => {
const previousUser = {
id: 999,
name: `T'Challa`,
nickname: 'Black Panther',
lang: 'en',
darkMode: false,
2022-12-22 12:46:43 +00:00
};
2022-12-20 11:30:25 +00:00
const expectedUser = {
id: 0,
name: '',
nickname: '',
lang: '',
darkMode: null,
2022-12-22 12:46:43 +00:00
};
2022-12-20 11:30:25 +00:00
let user = state.getUser();
localStorage.setItem('token', 'tokenToBeGone');
2022-12-22 12:46:43 +00:00
state.setUser(previousUser);
2022-12-20 11:30:25 +00:00
expect(localStorage.getItem('token')).toEqual('tokenToBeGone');
expect(user.value).toEqual(previousUser);
2024-02-28 12:15:59 +00:00
vi.spyOn(axios, 'post').mockResolvedValue({ data: true });
await session.destroy();
2022-12-20 11:30:25 +00:00
user = state.getUser();
expect(localStorage.getItem('token')).toBeNull();
expect(user.value).toEqual(expectedUser);
});
});
describe('login', () => {
const expectedUser = {
id: 999,
name: `T'Challa`,
nickname: 'Black Panther',
lang: 'en',
userConfig: {
darkMode: false,
2022-12-22 12:46:43 +00:00
},
};
2022-12-20 11:30:25 +00:00
const rolesData = [
{
role: {
2022-12-22 12:46:43 +00:00
name: 'salesPerson',
},
2022-12-20 11:30:25 +00:00
},
{
role: {
2022-12-22 12:46:43 +00:00
name: 'admin',
},
},
2022-12-20 11:30:25 +00:00
];
it('should fetch the user roles and then set token in the sessionStorage', async () => {
2022-12-22 12:46:43 +00:00
const expectedRoles = ['salesPerson', 'admin'];
2023-01-03 13:17:22 +00:00
vi.spyOn(axios, 'get').mockResolvedValue({
2022-12-22 12:46:43 +00:00
data: { roles: rolesData, user: expectedUser },
2022-12-20 11:30:25 +00:00
});
2022-12-22 12:46:43 +00:00
const expectedToken = 'mySessionToken';
2024-02-28 12:15:59 +00:00
const expectedTokenMultimedia = 'mySessionTokenMultimedia';
2022-12-22 12:46:43 +00:00
const keepLogin = false;
2022-12-20 11:30:25 +00:00
2024-02-28 12:15:59 +00:00
await session.login(expectedToken,expectedTokenMultimedia, keepLogin);
2022-12-20 11:30:25 +00:00
const roles = state.getRoles();
const localToken = localStorage.getItem('token');
const sessionToken = sessionStorage.getItem('token');
expect(roles.value).toEqual(expectedRoles);
expect(localToken).toBeNull();
expect(sessionToken).toEqual(expectedToken);
2024-02-28 12:15:59 +00:00
await session.destroy(); // this clears token and user for any other test
2022-12-20 11:30:25 +00:00
});
it('should fetch the user roles and then set token in the localStorage', async () => {
2022-12-22 12:46:43 +00:00
const expectedRoles = ['salesPerson', 'admin'];
2023-01-03 13:17:22 +00:00
vi.spyOn(axios, 'get').mockResolvedValue({
2022-12-22 12:46:43 +00:00
data: { roles: rolesData, user: expectedUser },
2022-12-20 11:30:25 +00:00
});
2022-12-22 12:46:43 +00:00
const expectedToken = 'myLocalToken';
2024-02-28 12:15:59 +00:00
const expectedTokenMultimedia = 'myLocalTokenMultimedia';
2022-12-22 12:46:43 +00:00
const keepLogin = true;
2022-12-20 11:30:25 +00:00
2024-02-28 12:15:59 +00:00
await session.login(expectedToken, expectedTokenMultimedia, keepLogin);
2022-12-20 11:30:25 +00:00
const roles = state.getRoles();
const localToken = localStorage.getItem('token');
const sessionToken = sessionStorage.getItem('token');
expect(roles.value).toEqual(expectedRoles);
expect(localToken).toEqual(expectedToken);
expect(sessionToken).toBeNull();
2024-02-28 12:15:59 +00:00
await session.destroy(); // this clears token and user for any other test
2022-12-20 11:30:25 +00:00
});
});
});