2023-01-03 13:17:22 +00:00
|
|
|
import { vi, describe, expect, it } from 'vitest';
|
|
|
|
import { axios, flushPromises } from 'app/test/vitest/helper';
|
|
|
|
import { useRole } from 'composables/useRole';
|
2022-12-20 11:30:25 +00:00
|
|
|
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',
|
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',
|
2022-12-22 12:46:43 +00:00
|
|
|
};
|
|
|
|
const expectedRoles = ['salesPerson', 'admin'];
|
2023-05-11 07:05:48 +00:00
|
|
|
vi.spyOn(axios, 'get')
|
|
|
|
.mockResolvedValueOnce({
|
2022-12-22 12:46:43 +00:00
|
|
|
data: { roles: rolesData, user: fetchedUser },
|
2023-05-11 07:05:48 +00:00
|
|
|
})
|
2022-12-20 11:30:25 +00:00
|
|
|
|
2023-01-03 13:17:22 +00:00
|
|
|
vi.spyOn(role.state, 'setUser');
|
|
|
|
vi.spyOn(role.state, 'setRoles');
|
2022-12-20 11:30:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
2023-01-03 13:17:22 +00:00
|
|
|
|
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
|
|
|
});
|