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 fetchedUser = { id: 999, name: `T'Challa`, nickname: 'Black Panther', lang: 'en', userConfig: { darkMode: false, } } const expectedUser = { id: 999, name: `T'Challa`, nickname: 'Black Panther', lang: 'en', darkMode: false, } const expectedRoles = ['salesPerson', 'admin'] jest.spyOn(axios, 'get').mockResolvedValue({ data: { roles: rolesData, user: fetchedUser } }); 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); }); }); });