From e3d6ee6f9e9b363f898112dbccd85850a4c8ba5c Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Tue, 13 Aug 2019 09:23:14 -0400 Subject: [PATCH] move label requests to function --- .../JobTemplateEdit/JobTemplateEdit.jsx | 40 ++++++++++++------- .../JobTemplateEdit/JobTemplateEdit.test.jsx | 5 ++- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.jsx b/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.jsx index 655ececfe5..6dcad602f4 100644 --- a/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.jsx +++ b/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.jsx @@ -19,6 +19,7 @@ class JobTemplateEdit extends Component { this.handleCancel = this.handleCancel.bind(this); this.handleSubmit = this.handleSubmit.bind(this); + this.submitLabels = this.submitLabels.bind(this); } async handleSubmit(values, newLabels = [], removedLabels = []) { @@ -27,28 +28,37 @@ class JobTemplateEdit extends Component { history, } = this.props; - const disassociatedLabels = removedLabels.forEach(removedLabel => - JobTemplatesAPI.disassociateLabel(id, removedLabel) - ); - const associatedLabels = newLabels - .filter(newLabel => !newLabel.organization) - .forEach(newLabel => JobTemplatesAPI.associateLabel(id, newLabel)); - const generatedLabels = newLabels - .filter(newLabel => newLabel.organization) - .forEach(newLabel => JobTemplatesAPI.generateLabel(id, newLabel)); try { - await Promise.all([ - JobTemplatesAPI.update(id, { ...values }), - disassociatedLabels, - associatedLabels, - generatedLabels, - ]); + await JobTemplatesAPI.update(id, { ...values }); + await Promise.all([this.submitLabels(newLabels, removedLabels)]); history.push(`/templates/${type}/${id}/details`); } catch (error) { this.setState({ error }); } } + async submitLabels(newLabels, removedLabels) { + const { + template: { id }, + } = this.props; + const disassociationPromises = removedLabels.map(label => + JobTemplatesAPI.disassociateLabel(id, label) + ); + const associationPromises = newLabels + .filter(label => !label.organization) + .map(label => JobTemplatesAPI.associateLabel(id, label)); + const creationPromises = newLabels + .filter(label => label.organization) + .map(label => JobTemplatesAPI.generateLabel(id, label)); + + const results = await Promise.all([ + ...disassociationPromises, + ...associationPromises, + ...creationPromises, + ]); + return results; + } + handleCancel() { const { template: { id, type }, diff --git a/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx b/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx index 1448887e95..0f8a49feb7 100644 --- a/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx +++ b/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx @@ -29,7 +29,7 @@ describe('', () => { mountWithContexts(); }); - test('handleSubmit should call api update', () => { + test('handleSubmit should call api update', async (done) => { const wrapper = mountWithContexts(); const updatedTemplateData = { name: 'new name', @@ -47,7 +47,7 @@ describe('', () => { { disassociate: true, id: 2 }, ]; - wrapper.find('JobTemplateForm').prop('handleSubmit')( + await wrapper.find('JobTemplateForm').prop('handleSubmit')( updatedTemplateData, newLabels, removedLabels @@ -56,6 +56,7 @@ describe('', () => { expect(JobTemplatesAPI.disassociateLabel).toHaveBeenCalledTimes(2); expect(JobTemplatesAPI.associateLabel).toHaveBeenCalledTimes(2); expect(JobTemplatesAPI.generateLabel).toHaveBeenCalledTimes(2); + done(); }); test('should navigate to job template detail when cancel is clicked', () => {