0
0
Fork 0
salix-front-mindshore-fork2/test/vitest/__tests__/composables/useAcl.spec.js

58 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-05-16 14:29:42 +00:00
import { vi, describe, expect, it, beforeAll, afterEach, beforeEach } from 'vitest';
import { axios, flushPromises } from 'app/test/vitest/helper';
import { useAcl } from 'src/composables/useAcl';
describe('useAcl', () => {
const acl = useAcl();
const mockAcls = [
{
model: 'Address',
property: '*',
accessType: '*',
permission: 'ALLOW',
principalType: 'ROLE',
principalId: 'employee',
id: 3,
},
{
model: 'Worker',
property: 'holidays',
accessType: 'READ',
permission: 'ALLOW',
principalType: 'ROLE',
principalId: 'employee',
id: 13,
},
];
const expectedAcls = {
Address: { '*': { '*': true } },
Worker: { holidays: { READ: true } },
};
beforeAll(() => {
vi.spyOn(axios, 'get').mockResolvedValue({ data: mockAcls });
vi.spyOn(acl.state, 'setAcls');
});
beforeEach(async () => await acl.fetch());
afterEach(async () => {
await flushPromises();
acl.state.setAcls([]);
});
describe('fetch', () => {
it('should call setUser and setRoles of the state with the expected data', async () => {
expect(acl.state.setAcls).toHaveBeenCalledWith(expectedAcls);
});
});
describe('hasAny', () => {
it('should return true if a role matched', async () =>
expect(acl.hasAny('Address', '*', 'WRITE')).toBeTruthy());
it('should return false if no roles matched', async () => {
expect(acl.hasAny('Worker', '*', 'WRITE')).toBeFalsy();
});
});
});