@@ -42,6 +42,17 @@ export const actions = {
|
||||
commit(types.SET_CAMPAIGN_UI_FLAG, { isCreating: false });
|
||||
}
|
||||
},
|
||||
update: async ({ commit }, { id, ...updateObj }) => {
|
||||
commit(types.SET_CAMPAIGN_UI_FLAG, { isUpdating: true });
|
||||
try {
|
||||
const response = await CampaignsAPI.update(id, updateObj);
|
||||
commit(types.EDIT_CAMPAIGN, response.data);
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
} finally {
|
||||
commit(types.SET_CAMPAIGN_UI_FLAG, { isUpdating: false });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export const mutations = {
|
||||
@@ -54,6 +65,7 @@ export const mutations = {
|
||||
|
||||
[types.ADD_CAMPAIGN]: MutationHelpers.create,
|
||||
[types.SET_CAMPAIGNS]: MutationHelpers.set,
|
||||
[types.EDIT_CAMPAIGN]: MutationHelpers.update,
|
||||
};
|
||||
|
||||
export default {
|
||||
|
||||
@@ -46,4 +46,26 @@ describe('#actions', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#update', () => {
|
||||
it('sends correct actions if API is success', async () => {
|
||||
axios.patch.mockResolvedValue({ data: campaignList[0] });
|
||||
await actions.update({ commit }, campaignList[0]);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.default.SET_CAMPAIGN_UI_FLAG, { isUpdating: true }],
|
||||
[types.default.EDIT_CAMPAIGN, campaignList[0]],
|
||||
[types.default.SET_CAMPAIGN_UI_FLAG, { isUpdating: false }],
|
||||
]);
|
||||
});
|
||||
it('sends correct actions if API is error', async () => {
|
||||
axios.patch.mockRejectedValue({ message: 'Incorrect header' });
|
||||
await expect(actions.update({ commit }, campaignList[0])).rejects.toThrow(
|
||||
Error
|
||||
);
|
||||
expect(commit.mock.calls).toEqual([
|
||||
[types.default.SET_CAMPAIGN_UI_FLAG, { isUpdating: true }],
|
||||
[types.default.SET_CAMPAIGN_UI_FLAG, { isUpdating: false }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export default [
|
||||
{
|
||||
id: 8,
|
||||
id: 1,
|
||||
title: 'Welcome',
|
||||
description: null,
|
||||
account_id: 1,
|
||||
@@ -115,7 +115,7 @@ export default [
|
||||
updated_at: '2021-05-03T04:53:36.354Z',
|
||||
},
|
||||
{
|
||||
id: 11,
|
||||
id: 2,
|
||||
title: 'Onboarding Campaign',
|
||||
description: null,
|
||||
account_id: 1,
|
||||
@@ -231,7 +231,7 @@ export default [
|
||||
updated_at: '2021-05-03T08:15:35.828Z',
|
||||
},
|
||||
{
|
||||
id: 12,
|
||||
id: 3,
|
||||
title: 'Thanks',
|
||||
description: null,
|
||||
account_id: 1,
|
||||
|
||||
@@ -18,4 +18,16 @@ describe('#mutations', () => {
|
||||
expect(state.records).toEqual([campaigns[0], campaigns[1]]);
|
||||
});
|
||||
});
|
||||
describe('#EDIT_CAMPAIGN', () => {
|
||||
it('update campaign record', () => {
|
||||
const state = { records: [campaigns[0]] };
|
||||
mutations[types.EDIT_CAMPAIGN](state, {
|
||||
id: 1,
|
||||
title: 'Welcome',
|
||||
account_id: 1,
|
||||
message: 'Hey, What brings you today',
|
||||
});
|
||||
expect(state.records[0].message).toEqual('Hey, What brings you today');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user