From b6aeb5e0e9d09129fae14ef56de1c635ae0b12bc Mon Sep 17 00:00:00 2001 From: carlosjr Date: Fri, 8 Apr 2022 15:53:21 +0200 Subject: [PATCH] composables refactor and their unit tests --- src/composables/__tests__/useRole.spec.js | 61 +++++++++ src/composables/__tests__/useSession.spec.js | 134 +++++++++++++++++++ src/composables/useRole.js | 8 +- src/composables/useSession.js | 9 +- 4 files changed, 205 insertions(+), 7 deletions(-) create mode 100644 src/composables/__tests__/useRole.spec.js create mode 100644 src/composables/__tests__/useSession.spec.js diff --git a/src/composables/__tests__/useRole.spec.js b/src/composables/__tests__/useRole.spec.js new file mode 100644 index 000000000..bf2129107 --- /dev/null +++ b/src/composables/__tests__/useRole.spec.js @@ -0,0 +1,61 @@ +import { describe, expect, it, jest } from '@jest/globals'; +import { axios, flushPromises } from 'app/tests/jest/jestHelpers'; +import { useRole } from '../useRole'; +const role = useRole(); + +describe('useRole', () => { + + describe('fetch', () => { + it('should call setUser and setRoles of the state with the expected data', async () => { + const rolesData = [ + { + role: { + name: 'salesPerson' + } + }, + { + role: { + name: 'admin' + } + } + ]; + const expectedUser = { id: 1 } + const expectedRoles = ['salesPerson', 'admin'] + jest.spyOn(axios, 'get').mockResolvedValue({ + data: { roles: rolesData, user: expectedUser } + }); + + jest.spyOn(role.state, 'setUser'); + jest.spyOn(role.state, 'setRoles'); + + role.fetch(); + + await flushPromises(); + + expect(role.state.setUser).toHaveBeenCalledWith(expectedUser); + expect(role.state.setRoles).toHaveBeenCalledWith(expectedRoles); + + role.state.setRoles([]) + }); + }); + describe('hasAny', () => { + it('should return true if a role matched', async () => { + role.state.setRoles(['admin']) + const hasRole = role.hasAny(['admin']); + + await flushPromises(); + + expect(hasRole).toBe(true); + + role.state.setRoles([]) + }); + + it('should return false if no roles matched', async () => { + const hasRole = role.hasAny(['admin']); + + await flushPromises(); + + expect(hasRole).toBe(false); + }); + }); +}); \ No newline at end of file diff --git a/src/composables/__tests__/useSession.spec.js b/src/composables/__tests__/useSession.spec.js new file mode 100644 index 000000000..52b31f59c --- /dev/null +++ b/src/composables/__tests__/useSession.spec.js @@ -0,0 +1,134 @@ +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', + } + const expectedUser = { + id: 0, + name: '', + nickname: '', + } + 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', () => { + it('should fetch the user roles and then set token in the sessionStorage', async () => { + const rolesData = [ + { + role: { + name: 'salesPerson' + } + }, + { + role: { + name: 'admin' + } + } + ]; + const expectedUser = { id: 1 } + 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 rolesData = [ + { + role: { + name: 'salesPerson' + } + }, + { + role: { + name: 'admin' + } + } + ]; + const expectedUser = { id: 1 } + 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 + }); + }); +}); diff --git a/src/composables/useRole.js b/src/composables/useRole.js index 664dc2f66..a34e81a81 100644 --- a/src/composables/useRole.js +++ b/src/composables/useRole.js @@ -1,9 +1,9 @@ import { useState } from './useState'; import axios from 'axios'; -const state = useState(); - export function useRole() { + const state = useState(); + async function fetch() { const { data } = await axios.get('/api/accounts/acl'); const roles = data.roles.map(userRoles => userRoles.role.name); @@ -25,6 +25,6 @@ export function useRole() { return { fetch, hasAny, + state }; -} - +} \ No newline at end of file diff --git a/src/composables/useSession.js b/src/composables/useSession.js index e52829ba7..d897b0dd7 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -18,14 +18,17 @@ export function useSession() { } function destroy() { - localStorage.removeItem('token'); - sessionStorage.getItem('token'); + if (localStorage.getItem('token')) + localStorage.removeItem('token') + + if (sessionStorage.getItem('token')) + sessionStorage.removeItem('token'); const { setUser } = useState(); setUser({ id: 0, - username: '', + name: '', nickname: '', }); }