feat: Add the ability to create custom attribute (#2903)

This commit is contained in:
Sivin Varghese
2021-08-31 13:54:34 +05:30
committed by GitHub
parent 75329e5de1
commit fdcc322660
12 changed files with 550 additions and 3 deletions

View File

@@ -0,0 +1,91 @@
import * as MutationHelpers from 'shared/helpers/vuex/mutationHelpers';
import types from '../mutation-types';
import AttributeAPI from '../../api/attributes';
export const state = {
records: [],
uiFlags: {
isFetching: false,
isCreating: false,
},
};
export const getters = {
getUIFlags(_state) {
return _state.uiFlags;
},
getAttributes: _state => attributeType => {
return _state.records.filter(
record => record.attribute_display_type === attributeType
);
},
};
export const actions = {
get: async function getAttributes({ commit }) {
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isFetching: true });
try {
const response = await AttributeAPI.get();
commit(types.SET_CUSTOM_ATTRIBUTE, response.data);
} catch (error) {
// Ignore error
} finally {
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isFetching: false });
}
},
create: async function createAttribute({ commit }, attributeObj) {
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isCreating: true });
try {
const response = await AttributeAPI.create(attributeObj);
commit(types.ADD_CUSTOM_ATTRIBUTE, response.data);
} catch (error) {
throw new Error(error);
} finally {
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isCreating: false });
}
},
update: async ({ commit }, { id, ...updateObj }) => {
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isUpdating: true });
try {
const response = await AttributeAPI.update(id, updateObj);
commit(types.EDIT_CUSTOM_ATTRIBUTE, response.data);
} catch (error) {
throw new Error(error);
} finally {
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isUpdating: false });
}
},
delete: async ({ commit }, id) => {
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isDeleting: true });
try {
await AttributeAPI.delete(id);
commit(types.DELETE_CUSTOM_ATTRIBUTE, id);
} catch (error) {
throw new Error(error);
} finally {
commit(types.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isDeleting: false });
}
},
};
export const mutations = {
[types.SET_CUSTOM_ATTRIBUTE_UI_FLAG](_state, data) {
_state.uiFlags = {
..._state.uiFlags,
...data,
};
},
[types.ADD_CUSTOM_ATTRIBUTE]: MutationHelpers.create,
[types.SET_CUSTOM_ATTRIBUTE]: MutationHelpers.set,
[types.EDIT_CUSTOM_ATTRIBUTE]: MutationHelpers.update,
[types.DELETE_CUSTOM_ATTRIBUTE]: MutationHelpers.destroy,
};
export default {
namespaced: true,
actions,
state,
getters,
mutations,
};

View File

@@ -0,0 +1,93 @@
import axios from 'axios';
import { actions } from '../../attributes';
import * as types from '../../../mutation-types';
import attributesList from './fixtures';
const commit = jest.fn();
global.axios = axios;
jest.mock('axios');
describe('#actions', () => {
describe('#get', () => {
it('sends correct actions if API is success', async () => {
axios.get.mockResolvedValue({ data: attributesList });
await actions.get({ commit }, { inboxId: 23 });
expect(commit.mock.calls).toEqual([
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isFetching: true }],
[types.default.SET_CUSTOM_ATTRIBUTE, attributesList],
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isFetching: false }],
]);
});
it('sends correct actions if API is error', async () => {
axios.get.mockRejectedValue({ message: 'Incorrect header' });
await actions.get({ commit }, { inboxId: 23 });
expect(commit.mock.calls).toEqual([
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isFetching: true }],
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isFetching: false }],
]);
});
});
describe('#create', () => {
it('sends correct actions if API is success', async () => {
axios.post.mockResolvedValue({ data: attributesList[0] });
await actions.create({ commit }, attributesList[0]);
expect(commit.mock.calls).toEqual([
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isCreating: true }],
[types.default.ADD_CUSTOM_ATTRIBUTE, attributesList[0]],
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isCreating: false }],
]);
});
it('sends correct actions if API is error', async () => {
axios.post.mockRejectedValue({ message: 'Incorrect header' });
await expect(actions.create({ commit })).rejects.toThrow(Error);
expect(commit.mock.calls).toEqual([
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isCreating: true }],
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isCreating: false }],
]);
});
});
describe('#update', () => {
it('sends correct actions if API is success', async () => {
axios.patch.mockResolvedValue({ data: attributesList[0] });
await actions.update({ commit }, attributesList[0]);
expect(commit.mock.calls).toEqual([
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isUpdating: true }],
[types.default.EDIT_CUSTOM_ATTRIBUTE, attributesList[0]],
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isUpdating: false }],
]);
});
it('sends correct actions if API is error', async () => {
axios.patch.mockRejectedValue({ message: 'Incorrect header' });
await expect(
actions.update({ commit }, attributesList[0])
).rejects.toThrow(Error);
expect(commit.mock.calls).toEqual([
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isUpdating: true }],
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isUpdating: false }],
]);
});
});
describe('#delete', () => {
it('sends correct actions if API is success', async () => {
axios.delete.mockResolvedValue({ data: attributesList[0] });
await actions.delete({ commit }, attributesList[0].id);
expect(commit.mock.calls).toEqual([
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isDeleting: true }],
[types.default.DELETE_CUSTOM_ATTRIBUTE, attributesList[0].id],
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isDeleting: false }],
]);
});
it('sends correct actions if API is error', async () => {
axios.delete.mockRejectedValue({ message: 'Incorrect header' });
await expect(
actions.delete({ commit }, attributesList[0].id)
).rejects.toThrow(Error);
expect(commit.mock.calls).toEqual([
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isDeleting: true }],
[types.default.SET_CUSTOM_ATTRIBUTE_UI_FLAG, { isDeleting: false }],
]);
});
});
});

View File

@@ -0,0 +1,16 @@
export default [
{
attribute_display_name: 'Language',
attribute_display_type: 0,
attribute_description: 'The conversation language',
attribute_key: 'language',
attribute_model: 0,
},
{
attribute_display_name: 'Language one',
attribute_display_type: 1,
attribute_description: 'The conversation language one',
attribute_key: 'language_one',
attribute_model: 3,
},
];

View File

@@ -0,0 +1,30 @@
import { getters } from '../../attributes';
import attributesList from './fixtures';
describe('#getters', () => {
it('getAttributes', () => {
const state = { records: attributesList };
expect(getters.getAttributes(state)(1)).toEqual([
{
attribute_display_name: 'Language one',
attribute_display_type: 1,
attribute_description: 'The conversation language one',
attribute_key: 'language_one',
attribute_model: 3,
},
]);
});
it('getUIFlags', () => {
const state = {
uiFlags: {
isFetching: true,
isCreating: false,
},
};
expect(getters.getUIFlags(state)).toEqual({
isFetching: true,
isCreating: false,
});
});
});

View File

@@ -0,0 +1,44 @@
import types from '../../../mutation-types';
import { mutations } from '../../attributes';
import attributesList from './fixtures';
describe('#mutations', () => {
describe('#SET_CUSTOM_ATTRIBUTE', () => {
it('set attribute records', () => {
const state = { records: [] };
mutations[types.SET_CUSTOM_ATTRIBUTE](state, attributesList);
expect(state.records).toEqual(attributesList);
});
});
describe('#ADD_CUSTOM_ATTRIBUTE', () => {
it('push newly created attributes to the store', () => {
const state = { records: [attributesList[0]] };
mutations[types.ADD_CUSTOM_ATTRIBUTE](state, attributesList[1]);
expect(state.records).toEqual([attributesList[0], attributesList[1]]);
});
});
describe('#EDIT_CUSTOM_ATTRIBUTE', () => {
it('update attribute record', () => {
const state = { records: [attributesList[0]] };
mutations[types.EDIT_CUSTOM_ATTRIBUTE](state, {
attribute_display_name: 'Language',
attribute_display_type: 0,
attribute_description: 'The conversation language',
attribute_key: 'language',
attribute_model: 0,
});
expect(state.records[0].attribute_description).toEqual(
'The conversation language'
);
});
});
describe('#DELETE_CUSTOM_ATTRIBUTE', () => {
it('delete attribute record', () => {
const state = { records: [attributesList[0]] };
mutations[types.DELETE_CUSTOM_ATTRIBUTE](state, attributesList[0]);
expect(state.records).toEqual([attributesList[0]]);
});
});
});