feat: Agent assignment policy index page with CRUD actions (#12373)
# Pull Request Template ## Description This PR incudes new Agent assignment policy index page with CRUD actions. Fixes https://linear.app/chatwoot/issue/CW-5570/feat-assignment-policy-index-page-with-actions ## Type of change - [x] New feature (non-breaking change which adds functionality) ## How Has This Been Tested? ### Loom https://www.loom.com/share/17ab5ceca4854f179628a3b53f347e5a?sid=cb64e881-57fd-4ae1-921b-7648653cca33 ### Screenshots **Light mode** <img width="1428" height="888" alt="image" src="https://github.com/user-attachments/assets/fdbb83e9-1f4f-4432-9e8a-4a8f1b810d31" /> **Dark mode** <img width="1428" height="888" alt="image" src="https://github.com/user-attachments/assets/f1fb38b9-1150-482c-ba62-3fe63ee1c7d4" /> <img width="726" height="495" alt="image" src="https://github.com/user-attachments/assets/90a6ad55-9ab6-4adb-93a7-2327f5f09c79" /> ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
import axios from 'axios';
|
||||
import { actions } from '../../assignmentPolicies';
|
||||
import types from '../../../mutation-types';
|
||||
import assignmentPoliciesList, { camelCaseFixtures } from './fixtures';
|
||||
import camelcaseKeys from 'camelcase-keys';
|
||||
|
||||
const commit = vi.fn();
|
||||
|
||||
global.axios = axios;
|
||||
vi.mock('axios');
|
||||
vi.mock('camelcase-keys');
|
||||
vi.mock('../../../utils/api');
|
||||
|
||||
describe('#actions', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('#get', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
axios.get.mockResolvedValue({ data: assignmentPoliciesList });
|
||||
camelcaseKeys.mockReturnValue(camelCaseFixtures);
|
||||
|
||||
await actions.get({ commit });
|
||||
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(assignmentPoliciesList);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetching: true }],
|
||||
[types.SET_ASSIGNMENT_POLICIES, camelCaseFixtures],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetching: false }],
|
||||
]);
|
||||
});
|
||||
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.get.mockRejectedValue({ message: 'Incorrect header' });
|
||||
|
||||
await actions.get({ commit });
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetching: true }],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetching: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#show', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
const policyData = assignmentPoliciesList[0];
|
||||
const camelCasedPolicy = camelCaseFixtures[0];
|
||||
|
||||
axios.get.mockResolvedValue({ data: policyData });
|
||||
camelcaseKeys.mockReturnValue(camelCasedPolicy);
|
||||
|
||||
await actions.show({ commit }, 1);
|
||||
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(policyData);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetchingItem: true }],
|
||||
[types.EDIT_ASSIGNMENT_POLICY, camelCasedPolicy],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetchingItem: false }],
|
||||
]);
|
||||
});
|
||||
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.get.mockRejectedValue({ message: 'Not found' });
|
||||
|
||||
await actions.show({ commit }, 1);
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetchingItem: true }],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isFetchingItem: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#create', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
const newPolicy = assignmentPoliciesList[0];
|
||||
const camelCasedData = camelCaseFixtures[0];
|
||||
|
||||
axios.post.mockResolvedValue({ data: newPolicy });
|
||||
camelcaseKeys.mockReturnValue(camelCasedData);
|
||||
|
||||
const result = await actions.create({ commit }, newPolicy);
|
||||
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(newPolicy);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isCreating: true }],
|
||||
[types.ADD_ASSIGNMENT_POLICY, camelCasedData],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isCreating: false }],
|
||||
]);
|
||||
expect(result).toEqual(newPolicy);
|
||||
});
|
||||
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.post.mockRejectedValue(new Error('Validation error'));
|
||||
|
||||
await expect(actions.create({ commit }, {})).rejects.toThrow(Error);
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isCreating: true }],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isCreating: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#update', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
const updateParams = { id: 1, name: 'Updated Policy' };
|
||||
const responseData = {
|
||||
...assignmentPoliciesList[0],
|
||||
name: 'Updated Policy',
|
||||
};
|
||||
const camelCasedData = {
|
||||
...camelCaseFixtures[0],
|
||||
name: 'Updated Policy',
|
||||
};
|
||||
|
||||
axios.patch.mockResolvedValue({ data: responseData });
|
||||
camelcaseKeys.mockReturnValue(camelCasedData);
|
||||
|
||||
const result = await actions.update({ commit }, updateParams);
|
||||
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(responseData);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isUpdating: true }],
|
||||
[types.EDIT_ASSIGNMENT_POLICY, camelCasedData],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isUpdating: false }],
|
||||
]);
|
||||
expect(result).toEqual(responseData);
|
||||
});
|
||||
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.patch.mockRejectedValue(new Error('Validation error'));
|
||||
|
||||
await expect(
|
||||
actions.update({ commit }, { id: 1, name: 'Test' })
|
||||
).rejects.toThrow(Error);
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isUpdating: true }],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isUpdating: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#delete', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
const policyId = 1;
|
||||
axios.delete.mockResolvedValue({});
|
||||
|
||||
await actions.delete({ commit }, policyId);
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isDeleting: true }],
|
||||
[types.DELETE_ASSIGNMENT_POLICY, policyId],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isDeleting: false }],
|
||||
]);
|
||||
});
|
||||
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.delete.mockRejectedValue(new Error('Not found'));
|
||||
|
||||
await expect(actions.delete({ commit }, 1)).rejects.toThrow(Error);
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isDeleting: true }],
|
||||
[types.SET_ASSIGNMENT_POLICIES_UI_FLAG, { isDeleting: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getInboxes', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
const policyId = 1;
|
||||
const inboxData = {
|
||||
inboxes: [
|
||||
{ id: 1, name: 'Support' },
|
||||
{ id: 2, name: 'Sales' },
|
||||
],
|
||||
};
|
||||
const camelCasedInboxes = [
|
||||
{ id: 1, name: 'Support' },
|
||||
{ id: 2, name: 'Sales' },
|
||||
];
|
||||
|
||||
axios.get.mockResolvedValue({ data: inboxData });
|
||||
camelcaseKeys.mockReturnValue(camelCasedInboxes);
|
||||
|
||||
await actions.getInboxes({ commit }, policyId);
|
||||
|
||||
expect(camelcaseKeys).toHaveBeenCalledWith(inboxData.inboxes);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, { isFetching: true }],
|
||||
[
|
||||
types.SET_ASSIGNMENT_POLICIES_INBOXES,
|
||||
{ policyId, inboxes: camelCasedInboxes },
|
||||
],
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, { isFetching: false }],
|
||||
]);
|
||||
});
|
||||
|
||||
it('sends correct actions if API fails', async () => {
|
||||
axios.get.mockRejectedValue(new Error('API Error'));
|
||||
|
||||
await expect(actions.getInboxes({ commit }, 1)).rejects.toThrow(Error);
|
||||
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, { isFetching: true }],
|
||||
[types.SET_ASSIGNMENT_POLICIES_INBOXES_UI_FLAG, { isFetching: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user