salix-front/test/vitest/__tests__/composables/useRole.spec.js

74 lines
2.1 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',
};
const expectedUser = {
id: 999,
name: `T'Challa`,
nickname: 'Black Panther',
lang: 'en',
};
const expectedRoles = ['salesPerson', 'admin'];
vi.spyOn(axios, 'get')
.mockResolvedValueOnce({
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);
});
});
});