Merge pull request #8520 from AlexSCorey/7636-WFJTSurveyNotEnabled

Toggles WFJT survey on and off

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot] 2020-11-10 12:25:57 +00:00 committed by GitHub
commit d16055806b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 5 deletions

View File

@ -68,11 +68,17 @@ function TemplateSurvey({ template, canEdit, i18n }) {
const { request: toggleSurvey, error: toggleError } = useRequest(
useCallback(async () => {
await JobTemplatesAPI.update(template.id, {
survey_enabled: !surveyEnabled,
});
if (templateType === 'workflow_job_template') {
await WorkflowJobTemplatesAPI.update(template.id, {
survey_enabled: !surveyEnabled,
});
} else {
await JobTemplatesAPI.update(template.id, {
survey_enabled: !surveyEnabled,
});
}
setSurveyEnabled(!surveyEnabled);
}, [template.id, surveyEnabled])
}, [template.id, templateType, surveyEnabled])
);
const { error, dismissError } = useDismissableError(

View File

@ -1,12 +1,14 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import { Route } from 'react-router-dom';
import { createMemoryHistory } from 'history';
import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
import TemplateSurvey from './TemplateSurvey';
import { JobTemplatesAPI } from '../../api';
import { JobTemplatesAPI, WorkflowJobTemplatesAPI } from '../../api';
import mockJobTemplateData from './shared/data.job_template.json';
jest.mock('../../api/models/JobTemplates');
jest.mock('../../api/models/WorkflowJobTemplates');
const surveyData = {
name: 'Survey',
@ -86,4 +88,64 @@ describe('<TemplateSurvey />', () => {
],
});
});
test('should toggle jt survery on', async () => {
const history = createMemoryHistory({
initialEntries: ['/templates/job_template/1/survey'],
});
let wrapper;
await act(async () => {
wrapper = mountWithContexts(
<TemplateSurvey template={mockJobTemplateData} canEdit />,
{
context: { router: { history } },
}
);
});
wrapper.update();
await act(() =>
wrapper.find('Switch[aria-label="Survey Toggle"]').prop('onChange')()
);
wrapper.update();
expect(JobTemplatesAPI.update).toBeCalledWith(7, { survey_enabled: false });
});
test('should toggle wfjt survey on', async () => {
const history = createMemoryHistory({
initialEntries: ['/templates/workflow_job_template/1/survey'],
});
WorkflowJobTemplatesAPI.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: 'workflow_job_template' } },
},
},
},
}
);
});
wrapper.update();
await act(() =>
wrapper.find('Switch[aria-label="Survey Toggle"]').prop('onChange')()
);
wrapper.update();
expect(WorkflowJobTemplatesAPI.update).toBeCalledWith(7, {
survey_enabled: false,
});
});
});