salix-front/src/composables/__tests__/useRole.spec.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-12-20 11:30:25 +00:00
import { describe, expect, it, jest } from '@jest/globals';
2022-12-22 12:46:43 +00:00
import { axios, flushPromises } from 'app/test/jest/jestHelpers';
2022-12-20 11:30:25 +00:00
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: {
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
];
const fetchedUser = {
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 expectedUser = {
id: 999,
name: `T'Challa`,
nickname: 'Black Panther',
lang: 'en',
darkMode: false,
2022-12-22 12:46:43 +00:00
};
const expectedRoles = ['salesPerson', 'admin'];
2022-12-20 11:30:25 +00:00
jest.spyOn(axios, 'get').mockResolvedValue({
2022-12-22 12:46:43 +00:00
data: { roles: rolesData, user: fetchedUser },
2022-12-20 11:30:25 +00:00
});
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);
2022-12-22 12:46:43 +00:00
role.state.setRoles([]);
2022-12-20 11:30:25 +00:00
});
});
describe('hasAny', () => {
it('should return true if a role matched', async () => {
2022-12-22 12:46:43 +00:00
role.state.setRoles(['admin']);
2022-12-20 11:30:25 +00:00
const hasRole = role.hasAny(['admin']);
await flushPromises();
expect(hasRole).toBe(true);
2022-12-22 12:46:43 +00:00
role.state.setRoles([]);
2022-12-20 11:30:25 +00:00
});
it('should return false if no roles matched', async () => {
const hasRole = role.hasAny(['admin']);
await flushPromises();
expect(hasRole).toBe(false);
});
});
2022-12-22 12:46:43 +00:00
});