Merge pull request #9995 from mabashian/7670-delete-workflow-survey

Fixes bug deleting the last workflow survey question

SUMMARY
link #7670

ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME

UI

Reviewed-by: Alex Corey <Alex.swansboro@gmail.com>
Reviewed-by: Tiago Góes <tiago.goes2009@gmail.com>
This commit is contained in:
softwarefactory-project-zuul[bot] 2021-04-23 16:54:28 +00:00 committed by GitHub
commit fa02fd8563
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 96 additions and 2 deletions

View File

@ -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(

View File

@ -27,6 +27,10 @@ describe('<TemplateSurvey />', () => {
});
});
afterEach(() => {
jest.clearAllMocks();
});
test('should fetch survey from API', async () => {
const history = createMemoryHistory({
initialEntries: ['/templates/job_template/7/survey'],
@ -209,4 +213,90 @@ describe('<TemplateSurvey />', () => {
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(
<Route path="/templates/:templateType/:id/survey">
<TemplateSurvey template={mockJobTemplateData} canEdit />
</Route>,
{
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(
<Route path="/templates/:templateType/:id/survey">
<TemplateSurvey template={mockWorkflowJobTemplateData} canEdit />
</Route>,
{
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);
});
});