mirror of
https://github.com/ansible/awx.git
synced 2024-10-28 19:25:40 +03:00
Fix issue with broken survey question edit breadcrumb by altering the url scheme
This commit is contained in:
parent
e08e88d940
commit
f44faf4e61
@ -94,7 +94,7 @@ function SurveyListItem({
|
||||
dataListCells={[
|
||||
<DataListCell key="name">
|
||||
<>
|
||||
<Link to={`survey/edit/${question.variable}`}>
|
||||
<Link to={`survey/edit?question_variable=${question.variable}`}>
|
||||
{question.question_name}
|
||||
</Link>
|
||||
{question.required && (
|
||||
|
@ -1,5 +1,10 @@
|
||||
import React, { useState } from 'react';
|
||||
import { useHistory, useRouteMatch } from 'react-router-dom';
|
||||
import {
|
||||
Redirect,
|
||||
useHistory,
|
||||
useLocation,
|
||||
useRouteMatch,
|
||||
} from 'react-router-dom';
|
||||
import ContentLoading from '../../../components/ContentLoading';
|
||||
import { CardBody } from '../../../components/Card';
|
||||
import SurveyQuestionForm from './SurveyQuestionForm';
|
||||
@ -8,12 +13,23 @@ export default function SurveyQuestionEdit({ survey, updateSurvey }) {
|
||||
const [formError, setFormError] = useState(null);
|
||||
const history = useHistory();
|
||||
const match = useRouteMatch();
|
||||
const { search } = useLocation();
|
||||
const queryParams = new URLSearchParams(search);
|
||||
const questionVariable = queryParams.get('question_variable');
|
||||
|
||||
if (!survey) {
|
||||
return <ContentLoading />;
|
||||
}
|
||||
|
||||
const question = survey.spec.find(q => q.variable === match.params.variable);
|
||||
const question = survey.spec.find(q => q.variable === questionVariable);
|
||||
|
||||
if (!question) {
|
||||
return (
|
||||
<Redirect
|
||||
to={`/templates/${match.params.templateType}/${match.params.id}/survey`}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const navigateToList = () => {
|
||||
const index = match.url.indexOf('/edit');
|
||||
@ -34,7 +50,7 @@ export default function SurveyQuestionEdit({ survey, updateSurvey }) {
|
||||
return;
|
||||
}
|
||||
const questionIndex = survey.spec.findIndex(
|
||||
q => q.variable === match.params.variable
|
||||
q => q.variable === questionVariable
|
||||
);
|
||||
if (questionIndex === -1) {
|
||||
throw new Error('Question not found in spec');
|
||||
|
@ -33,87 +33,124 @@ describe('<SurveyQuestionEdit />', () => {
|
||||
let history;
|
||||
let wrapper;
|
||||
|
||||
beforeEach(() => {
|
||||
history = createMemoryHistory({
|
||||
initialEntries: ['/templates/job_templates/1/survey/edit/foo'],
|
||||
describe('with question_variable present', () => {
|
||||
beforeEach(() => {
|
||||
history = createMemoryHistory({
|
||||
initialEntries: [
|
||||
'/templates/job_templates/1/survey/edit?question_variable=foo',
|
||||
],
|
||||
});
|
||||
updateSurvey = jest.fn();
|
||||
act(() => {
|
||||
wrapper = mountWithContexts(
|
||||
<Switch>
|
||||
<Route path="/templates/:templateType/:id/survey/edit">
|
||||
<SurveyQuestionEdit survey={survey} updateSurvey={updateSurvey} />
|
||||
</Route>
|
||||
</Switch>,
|
||||
{
|
||||
context: { router: { history } },
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
updateSurvey = jest.fn();
|
||||
act(() => {
|
||||
wrapper = mountWithContexts(
|
||||
<Switch>
|
||||
<Route path="/templates/:templateType/:id/survey/edit/:variable">
|
||||
<SurveyQuestionEdit survey={survey} updateSurvey={updateSurvey} />
|
||||
</Route>
|
||||
</Switch>,
|
||||
|
||||
afterEach(() => {
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
test('should render form', () => {
|
||||
expect(wrapper.find('SurveyQuestionForm')).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('should call updateSurvey', () => {
|
||||
act(() => {
|
||||
wrapper.find('SurveyQuestionForm').invoke('handleSubmit')({
|
||||
question_name: 'new question',
|
||||
variable: 'question',
|
||||
type: 'text',
|
||||
});
|
||||
});
|
||||
wrapper.update();
|
||||
|
||||
expect(updateSurvey).toHaveBeenCalledWith([
|
||||
{
|
||||
context: { router: { history } },
|
||||
}
|
||||
question_name: 'new question',
|
||||
variable: 'question',
|
||||
type: 'text',
|
||||
},
|
||||
survey.spec[1],
|
||||
]);
|
||||
});
|
||||
|
||||
test('should set formError', async () => {
|
||||
const realConsoleError = global.console.error;
|
||||
global.console.error = jest.fn();
|
||||
const err = new Error('oops');
|
||||
updateSurvey.mockImplementation(() => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
act(() => {
|
||||
wrapper.find('SurveyQuestionForm').invoke('handleSubmit')({
|
||||
question_name: 'new question',
|
||||
variable: 'question',
|
||||
type: 'text',
|
||||
});
|
||||
});
|
||||
wrapper.update();
|
||||
|
||||
expect(wrapper.find('SurveyQuestionForm').prop('submitError')).toEqual(
|
||||
err
|
||||
);
|
||||
global.console.error = realConsoleError;
|
||||
});
|
||||
});
|
||||
|
||||
test('should render form', () => {
|
||||
expect(wrapper.find('SurveyQuestionForm')).toHaveLength(1);
|
||||
});
|
||||
test('should generate error for duplicate variable names', async () => {
|
||||
const realConsoleError = global.console.error;
|
||||
global.console.error = jest.fn();
|
||||
|
||||
test('should call updateSurvey', () => {
|
||||
act(() => {
|
||||
wrapper.find('SurveyQuestionForm').invoke('handleSubmit')({
|
||||
question_name: 'new question',
|
||||
variable: 'question',
|
||||
type: 'text',
|
||||
act(() => {
|
||||
wrapper.find('SurveyQuestionForm').invoke('handleSubmit')({
|
||||
question_name: 'new question',
|
||||
variable: 'bar',
|
||||
type: 'text',
|
||||
});
|
||||
});
|
||||
});
|
||||
wrapper.update();
|
||||
wrapper.update();
|
||||
|
||||
expect(updateSurvey).toHaveBeenCalledWith([
|
||||
{
|
||||
question_name: 'new question',
|
||||
variable: 'question',
|
||||
type: 'text',
|
||||
},
|
||||
survey.spec[1],
|
||||
]);
|
||||
const err = wrapper.find('SurveyQuestionForm').prop('submitError');
|
||||
expect(err.message).toEqual(
|
||||
'Survey already contains a question with variable named “bar”'
|
||||
);
|
||||
global.console.error = realConsoleError;
|
||||
});
|
||||
});
|
||||
|
||||
test('should set formError', async () => {
|
||||
const realConsoleError = global.console.error;
|
||||
global.console.error = jest.fn();
|
||||
const err = new Error('oops');
|
||||
updateSurvey.mockImplementation(() => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
act(() => {
|
||||
wrapper.find('SurveyQuestionForm').invoke('handleSubmit')({
|
||||
question_name: 'new question',
|
||||
variable: 'question',
|
||||
type: 'text',
|
||||
describe('without question_variable present', () => {
|
||||
test('should redirect back to the survey list', () => {
|
||||
history = createMemoryHistory({
|
||||
initialEntries: ['/templates/job_templates/1/survey/edit'],
|
||||
});
|
||||
});
|
||||
wrapper.update();
|
||||
|
||||
expect(wrapper.find('SurveyQuestionForm').prop('submitError')).toEqual(err);
|
||||
global.console.error = realConsoleError;
|
||||
});
|
||||
|
||||
test('should generate error for duplicate variable names', async () => {
|
||||
const realConsoleError = global.console.error;
|
||||
global.console.error = jest.fn();
|
||||
|
||||
act(() => {
|
||||
wrapper.find('SurveyQuestionForm').invoke('handleSubmit')({
|
||||
question_name: 'new question',
|
||||
variable: 'bar',
|
||||
type: 'text',
|
||||
updateSurvey = jest.fn();
|
||||
act(() => {
|
||||
wrapper = mountWithContexts(
|
||||
<Switch>
|
||||
<Route path="/templates/:templateType/:id/survey/edit">
|
||||
<SurveyQuestionEdit survey={survey} updateSurvey={updateSurvey} />
|
||||
</Route>
|
||||
</Switch>,
|
||||
{
|
||||
context: { router: { history } },
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
wrapper.update();
|
||||
|
||||
const err = wrapper.find('SurveyQuestionForm').prop('submitError');
|
||||
expect(err.message).toEqual(
|
||||
'Survey already contains a question with variable named “bar”'
|
||||
);
|
||||
global.console.error = realConsoleError;
|
||||
expect(history.location.pathname).toEqual(
|
||||
'/templates/job_templates/1/survey'
|
||||
);
|
||||
|
||||
wrapper.unmount();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -100,7 +100,7 @@ function TemplateSurvey({ template, canEdit, i18n }) {
|
||||
</Route>
|
||||
)}
|
||||
{canEdit && (
|
||||
<Route path="/templates/:templateType/:id/survey/edit/:variable">
|
||||
<Route path="/templates/:templateType/:id/survey/edit">
|
||||
<SurveyQuestionEdit
|
||||
survey={survey}
|
||||
updateSurvey={updateSurveySpec}
|
||||
|
Loading…
Reference in New Issue
Block a user