diff --git a/app/javascript/dashboard/api/automation.js b/app/javascript/dashboard/api/automation.js index 3c354307d..e83ece3d1 100644 --- a/app/javascript/dashboard/api/automation.js +++ b/app/javascript/dashboard/api/automation.js @@ -1,9 +1,14 @@ +/* global axios */ import ApiClient from './ApiClient'; class AutomationsAPI extends ApiClient { constructor() { super('automation_rules', { accountScoped: true }); } + + clone(automationId) { + return axios.post(`${this.url}/${automationId}/clone`); + } } export default new AutomationsAPI(); diff --git a/app/javascript/dashboard/api/specs/automation.spec.js b/app/javascript/dashboard/api/specs/automation.spec.js index c236b5ce3..2ab4cae82 100644 --- a/app/javascript/dashboard/api/specs/automation.spec.js +++ b/app/javascript/dashboard/api/specs/automation.spec.js @@ -9,6 +9,7 @@ describe('#AutomationsAPI', () => { expect(automations).toHaveProperty('create'); expect(automations).toHaveProperty('update'); expect(automations).toHaveProperty('delete'); + expect(automations).toHaveProperty('clone'); expect(automations.url).toBe('/api/v1/automation_rules'); }); }); diff --git a/app/javascript/dashboard/i18n/locale/en/automation.json b/app/javascript/dashboard/i18n/locale/en/automation.json index fa0dda7fc..ee78de7c8 100644 --- a/app/javascript/dashboard/i18n/locale/en/automation.json +++ b/app/javascript/dashboard/i18n/locale/en/automation.json @@ -71,6 +71,13 @@ "ERROR_MESSAGE": "Could not update automation rule, Please try again later" } }, + "CLONE": { + "TOOLTIP": "Clone", + "API": { + "SUCCESS_MESSAGE": "Automation cloned successfully", + "ERROR_MESSAGE": "Could not clone automation rule, Please try again later" + } + }, "FORM": { "EDIT": "Edit", "CREATE": "Create", diff --git a/app/javascript/dashboard/routes/dashboard/settings/automation/Index.vue b/app/javascript/dashboard/routes/dashboard/settings/automation/Index.vue index 6c1892a28..39fe9c71f 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/automation/Index.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/automation/Index.vue @@ -54,17 +54,17 @@ @click="openEditPopup(automation)" > - + { + commit(types.SET_AUTOMATION_UI_FLAG, { isCloning: true }); + try { + await AutomationAPI.clone(id); + } catch (error) { + throw new Error(error); + } finally { + commit(types.SET_AUTOMATION_UI_FLAG, { isCloning: false }); + } + }, }; export const mutations = { diff --git a/app/javascript/dashboard/store/modules/specs/automations/actions.spec.js b/app/javascript/dashboard/store/modules/specs/automations/actions.spec.js index 8dbc08f00..9ac38a5bc 100644 --- a/app/javascript/dashboard/store/modules/specs/automations/actions.spec.js +++ b/app/javascript/dashboard/store/modules/specs/automations/actions.spec.js @@ -47,28 +47,28 @@ describe('#actions', () => { ]); }); }); - // API Work in progress - // describe('#update', () => { - // it('sends correct actions if API is success', async () => { - // axios.patch.mockResolvedValue({ data: automationsList[0] }); - // await actions.update({ commit }, automationsList[0]); - // expect(commit.mock.calls).toEqual([ - // [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: true }], - // [types.default.EDIT_AUTOMATION, automationsList[0]], - // [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: false }], - // ]); - // }); - // it('sends correct actions if API is error', async () => { - // axios.patch.mockRejectedValue({ message: 'Incorrect header' }); - // await expect( - // actions.update({ commit }, automationsList[0]) - // ).rejects.toThrow(Error); - // expect(commit.mock.calls).toEqual([ - // [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: true }], - // [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: false }], - // ]); - // }); - // }); + + describe('#update', () => { + it('sends correct actions if API is success', async () => { + axios.patch.mockResolvedValue({ data: automationsList[0] }); + await actions.update({ commit }, automationsList[0]); + expect(commit.mock.calls).toEqual([ + [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: true }], + [types.default.EDIT_AUTOMATION, automationsList[0]], + [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: false }], + ]); + }); + it('sends correct actions if API is error', async () => { + axios.patch.mockRejectedValue({ message: 'Incorrect header' }); + await expect( + actions.update({ commit }, automationsList[0]) + ).rejects.toThrow(Error); + expect(commit.mock.calls).toEqual([ + [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: true }], + [types.default.SET_AUTOMATION_UI_FLAG, { isUpdating: false }], + ]); + }); + }); describe('#delete', () => { it('sends correct actions if API is success', async () => { @@ -91,4 +91,15 @@ describe('#actions', () => { ]); }); }); + + describe('#clone', () => { + it('clones the automation', async () => { + axios.post.mockResolvedValue({ data: automationsList[0] }); + await actions.clone({ commit }, automationsList[0]); + expect(commit.mock.calls).toEqual([ + [types.default.SET_AUTOMATION_UI_FLAG, { isCloning: true }], + [types.default.SET_AUTOMATION_UI_FLAG, { isCloning: false }], + ]); + }); + }); });