134 lines
4.1 KiB
JavaScript
134 lines
4.1 KiB
JavaScript
|
import { describe, expect, it, jest } from '@jest/globals';
|
||
|
import { useSession } from '../useSession';
|
||
|
import { useState } from '../useState';
|
||
|
import { axios } from 'app/tests/jest/jestHelpers';
|
||
|
|
||
|
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 () => {
|
||
|
const expectedToken = ''
|
||
|
|
||
|
const token = session.getToken();
|
||
|
|
||
|
expect(token).toEqual(expectedToken);
|
||
|
});
|
||
|
|
||
|
it('should return the token stored in local or session storage', async () => {
|
||
|
const expectedToken = 'myToken'
|
||
|
const data = {
|
||
|
token: expectedToken,
|
||
|
keepLogin: false
|
||
|
}
|
||
|
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,
|
||
|
}
|
||
|
const expectedUser = {
|
||
|
id: 0,
|
||
|
name: '',
|
||
|
nickname: '',
|
||
|
lang: '',
|
||
|
darkMode: null,
|
||
|
}
|
||
|
let user = state.getUser();
|
||
|
|
||
|
localStorage.setItem('token', 'tokenToBeGone');
|
||
|
state.setUser(previousUser)
|
||
|
|
||
|
expect(localStorage.getItem('token')).toEqual('tokenToBeGone');
|
||
|
expect(user.value).toEqual(previousUser);
|
||
|
|
||
|
|
||
|
session.destroy();
|
||
|
|
||
|
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,
|
||
|
}
|
||
|
}
|
||
|
const rolesData = [
|
||
|
{
|
||
|
role: {
|
||
|
name: 'salesPerson'
|
||
|
}
|
||
|
},
|
||
|
{
|
||
|
role: {
|
||
|
name: 'admin'
|
||
|
}
|
||
|
}
|
||
|
];
|
||
|
|
||
|
it('should fetch the user roles and then set token in the sessionStorage', async () => {
|
||
|
const expectedRoles = ['salesPerson', 'admin']
|
||
|
jest.spyOn(axios, 'get').mockResolvedValue({
|
||
|
data: { roles: rolesData, user: expectedUser }
|
||
|
});
|
||
|
|
||
|
const expectedToken = 'mySessionToken'
|
||
|
const keepLogin = false
|
||
|
|
||
|
await session.login(expectedToken, keepLogin);
|
||
|
|
||
|
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);
|
||
|
|
||
|
session.destroy() // this clears token and user for any other test
|
||
|
});
|
||
|
|
||
|
it('should fetch the user roles and then set token in the localStorage', async () => {
|
||
|
const expectedRoles = ['salesPerson', 'admin']
|
||
|
jest.spyOn(axios, 'get').mockResolvedValue({
|
||
|
data: { roles: rolesData, user: expectedUser }
|
||
|
});
|
||
|
|
||
|
const expectedToken = 'myLocalToken'
|
||
|
const keepLogin = true
|
||
|
|
||
|
await session.login(expectedToken, keepLogin);
|
||
|
|
||
|
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();
|
||
|
|
||
|
session.destroy() // this clears token and user for any other test
|
||
|
});
|
||
|
});
|
||
|
});
|