77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
import { vi, describe, expect, it } from 'vitest';
|
|
import { axios, flushPromises } from 'app/test/vitest/helper';
|
|
import { useRole } from 'composables/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'];
|
|
vi.spyOn(axios, 'get').mockResolvedValue({
|
|
data: { roles: rolesData, user: fetchedUser },
|
|
});
|
|
|
|
vi.spyOn(role.state, 'setUser');
|
|
vi.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);
|
|
});
|
|
});
|
|
});
|