From 9bfbf8d556b7ca518f0f2af58c722d84918724ad Mon Sep 17 00:00:00 2001 From: mabashian Date: Thu, 22 Apr 2021 16:30:47 -0400 Subject: [PATCH] Fixes bug deleting the last workflow survey question --- .../src/screens/Template/TemplateSurvey.jsx | 8 +- .../screens/Template/TemplateSurvey.test.jsx | 90 +++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/awx/ui_next/src/screens/Template/TemplateSurvey.jsx b/awx/ui_next/src/screens/Template/TemplateSurvey.jsx index 37b24cea67..c3701baa33 100644 --- a/awx/ui_next/src/screens/Template/TemplateSurvey.jsx +++ b/awx/ui_next/src/screens/Template/TemplateSurvey.jsx @@ -61,9 +61,13 @@ function TemplateSurvey({ template, canEdit, i18n }) { const { request: deleteSurvey, error: deleteError } = useRequest( useCallback(async () => { - await JobTemplatesAPI.destroySurvey(templateId); + if (templateType === 'workflow_job_template') { + await WorkflowJobTemplatesAPI.destroySurvey(templateId); + } else { + await JobTemplatesAPI.destroySurvey(templateId); + } setSurvey(null); - }, [templateId, setSurvey]) + }, [templateId, setSurvey, templateType]) ); const { request: toggleSurvey, error: toggleError } = useRequest( diff --git a/awx/ui_next/src/screens/Template/TemplateSurvey.test.jsx b/awx/ui_next/src/screens/Template/TemplateSurvey.test.jsx index ae0a5206b2..59fb33a5cb 100644 --- a/awx/ui_next/src/screens/Template/TemplateSurvey.test.jsx +++ b/awx/ui_next/src/screens/Template/TemplateSurvey.test.jsx @@ -27,6 +27,10 @@ describe('', () => { }); }); + afterEach(() => { + jest.clearAllMocks(); + }); + test('should fetch survey from API', async () => { const history = createMemoryHistory({ initialEntries: ['/templates/job_template/7/survey'], @@ -209,4 +213,90 @@ describe('', () => { survey_enabled: false, }); }); + + test('should successfully delete jt survey', async () => { + const history = createMemoryHistory({ + initialEntries: ['/templates/job_template/15/survey'], + }); + + JobTemplatesAPI.readSurvey.mockResolvedValueOnce({ + data: surveyData, + }); + + let wrapper; + await act(async () => { + wrapper = mountWithContexts( + + + , + { + context: { + router: { + history, + route: { + location: history.location, + match: { + params: { templateType: 'job_template', id: 15 }, + }, + }, + }, + }, + } + ); + }); + wrapper.update(); + act(() => wrapper.find('Checkbox#select-all').invoke('onChange')(true)); + wrapper.update(); + wrapper.find('Button[ouiaId="survey-delete-button"]').simulate('click'); + wrapper.update(); + await act(async () => + wrapper.find('Button[ouiaId="delete-confirm-button"]').simulate('click') + ); + wrapper.update(); + expect(JobTemplatesAPI.destroySurvey).toBeCalledWith('15'); + expect(WorkflowJobTemplatesAPI.destroySurvey).toHaveBeenCalledTimes(0); + }); + + test('should successfully delete wfjt survey', async () => { + const history = createMemoryHistory({ + initialEntries: ['/templates/workflow_job_template/15/survey'], + }); + + WorkflowJobTemplatesAPI.readSurvey.mockResolvedValueOnce({ + data: surveyData, + }); + + let wrapper; + await act(async () => { + wrapper = mountWithContexts( + + + , + { + context: { + router: { + history, + route: { + location: history.location, + match: { + params: { templateType: 'workflow_job_template', id: 15 }, + }, + }, + }, + }, + } + ); + }); + wrapper.update(); + act(() => wrapper.find('Checkbox#select-all').invoke('onChange')(true)); + wrapper.update(); + wrapper.find('Button[ouiaId="survey-delete-button"]').simulate('click'); + wrapper.update(); + await act(async () => + wrapper.find('Button[ouiaId="delete-confirm-button"]').simulate('click') + ); + wrapper.update(); + expect(WorkflowJobTemplatesAPI.destroySurvey).toBeCalledWith('15'); + expect(JobTemplatesAPI.destroySurvey).toHaveBeenCalledTimes(0); + }); });