mirror of
https://github.com/ansible/awx.git
synced 2026-05-20 07:17:40 -02:30
add survey form tests
This commit is contained in:
@@ -26,7 +26,10 @@ describe('<HostAdd />', () => {
|
|||||||
let history;
|
let history;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
history = createMemoryHistory();
|
history = createMemoryHistory({
|
||||||
|
initialEntries: ['/templates/job_templates/1/survey/edit/foo'],
|
||||||
|
state: { some: 'state' },
|
||||||
|
});
|
||||||
await act(async () => {
|
await act(async () => {
|
||||||
wrapper = mountWithContexts(<HostAdd />, {
|
wrapper = mountWithContexts(<HostAdd />, {
|
||||||
context: { router: { history } },
|
context: { router: { history } },
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ function SurveyList({
|
|||||||
>
|
>
|
||||||
<div>{i18n._(t`This action will delete the following:`)}</div>
|
<div>{i18n._(t`This action will delete the following:`)}</div>
|
||||||
{selected.map(question => (
|
{selected.map(question => (
|
||||||
<span key={question.id}>
|
<span key={question.variable}>
|
||||||
<strong>{question.question_name}</strong>
|
<strong>{question.question_name}</strong>
|
||||||
<br />
|
<br />
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { act } from 'react-dom/test-utils';
|
|||||||
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
||||||
import SurveyList from './SurveyList';
|
import SurveyList from './SurveyList';
|
||||||
import { JobTemplatesAPI } from '@api';
|
import { JobTemplatesAPI } from '@api';
|
||||||
import mockJobTemplateData from './data.job_template.json';
|
import mockJobTemplateData from '../shared/data.job_template.json';
|
||||||
|
|
||||||
jest.mock('@api/models/JobTemplates');
|
jest.mock('@api/models/JobTemplates');
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,9 @@ export default function SurveyQuestionAdd({ survey, updateSurvey }) {
|
|||||||
const handleSubmit = async question => {
|
const handleSubmit = async question => {
|
||||||
if (survey.spec.find(q => q.variable === question.variable)) {
|
if (survey.spec.find(q => q.variable === question.variable)) {
|
||||||
setFormError(
|
setFormError(
|
||||||
new Error(`Survey already contains a question with variable named
|
new Error(
|
||||||
“${question.variable}”`)
|
`Survey already contains a question with variable named “${question.variable}”`
|
||||||
|
)
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,126 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
||||||
|
import { act } from 'react-dom/test-utils';
|
||||||
|
import SurveyQuestionAdd from './SurveyQuestionAdd';
|
||||||
|
|
||||||
|
const survey = {
|
||||||
|
spec: [
|
||||||
|
{
|
||||||
|
question_name: 'What is the foo?',
|
||||||
|
question_description: 'more about the foo',
|
||||||
|
variable: 'foo',
|
||||||
|
required: true,
|
||||||
|
type: 'text',
|
||||||
|
min: 0,
|
||||||
|
max: 1024,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
question_name: 'Who shot the sheriff?',
|
||||||
|
question_description: 'they did not shoot the deputy',
|
||||||
|
variable: 'bar',
|
||||||
|
required: true,
|
||||||
|
type: 'textarea',
|
||||||
|
min: 0,
|
||||||
|
max: 1024,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('<SurveyQuestionAdd />', () => {
|
||||||
|
let updateSurvey;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
updateSurvey = jest.fn();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should render form', () => {
|
||||||
|
let wrapper;
|
||||||
|
act(() => {
|
||||||
|
wrapper = mountWithContexts(
|
||||||
|
<SurveyQuestionAdd survey={survey} updateSurvey={updateSurvey} />
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(wrapper.find('SurveyQuestionForm')).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should call updateSurvey', () => {
|
||||||
|
let wrapper;
|
||||||
|
act(() => {
|
||||||
|
wrapper = mountWithContexts(
|
||||||
|
<SurveyQuestionAdd survey={survey} updateSurvey={updateSurvey} />
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
wrapper.find('SurveyQuestionForm').invoke('handleSubmit')({
|
||||||
|
question_name: 'new question',
|
||||||
|
variable: 'question',
|
||||||
|
type: 'text',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
expect(updateSurvey).toHaveBeenCalledWith([
|
||||||
|
...survey.spec,
|
||||||
|
{
|
||||||
|
question_name: 'new question',
|
||||||
|
variable: 'question',
|
||||||
|
type: 'text',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should set formError', async () => {
|
||||||
|
const realConsoleError = global.console.error;
|
||||||
|
global.console.error = jest.fn();
|
||||||
|
const err = new Error('oops');
|
||||||
|
updateSurvey.mockImplementation(() => {
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
let wrapper;
|
||||||
|
act(() => {
|
||||||
|
wrapper = mountWithContexts(
|
||||||
|
<SurveyQuestionAdd survey={survey} updateSurvey={updateSurvey} />
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
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 generate error for duplicate variable names', async () => {
|
||||||
|
const realConsoleError = global.console.error;
|
||||||
|
global.console.error = jest.fn();
|
||||||
|
let wrapper;
|
||||||
|
act(() => {
|
||||||
|
wrapper = mountWithContexts(
|
||||||
|
<SurveyQuestionAdd survey={survey} updateSurvey={updateSurvey} />
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
wrapper.find('SurveyQuestionForm').invoke('handleSubmit')({
|
||||||
|
question_name: 'new question',
|
||||||
|
variable: 'foo',
|
||||||
|
type: 'text',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
|
||||||
|
const err = wrapper.find('SurveyQuestionForm').prop('submitError');
|
||||||
|
expect(err.message).toEqual(
|
||||||
|
'Survey already contains a question with variable named “foo”'
|
||||||
|
);
|
||||||
|
global.console.error = realConsoleError;
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -27,8 +27,9 @@ export default function SurveyQuestionEdit({ survey, updateSurvey }) {
|
|||||||
) {
|
) {
|
||||||
debugger;
|
debugger;
|
||||||
setFormError(
|
setFormError(
|
||||||
new Error(`Survey already contains a question with variable named
|
new Error(
|
||||||
“${formData.variable}”`)
|
`Survey already contains a question with variable named “${formData.variable}”`
|
||||||
|
)
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,119 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
||||||
|
import { Switch, Route } from 'react-router-dom';
|
||||||
|
import { act } from 'react-dom/test-utils';
|
||||||
|
import { createMemoryHistory } from 'history';
|
||||||
|
import SurveyQuestionEdit from './SurveyQuestionEdit';
|
||||||
|
|
||||||
|
const survey = {
|
||||||
|
spec: [
|
||||||
|
{
|
||||||
|
question_name: 'What is the foo?',
|
||||||
|
question_description: 'more about the foo',
|
||||||
|
variable: 'foo',
|
||||||
|
required: true,
|
||||||
|
type: 'text',
|
||||||
|
min: 0,
|
||||||
|
max: 1024,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
question_name: 'Who shot the sheriff?',
|
||||||
|
question_description: 'they did not shoot the deputy',
|
||||||
|
variable: 'bar',
|
||||||
|
required: true,
|
||||||
|
type: 'textarea',
|
||||||
|
min: 0,
|
||||||
|
max: 1024,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('<SurveyQuestionEdit />', () => {
|
||||||
|
let updateSurvey;
|
||||||
|
let history;
|
||||||
|
let wrapper;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
history = createMemoryHistory({
|
||||||
|
initialEntries: ['/templates/job_templates/1/survey/edit/foo'],
|
||||||
|
});
|
||||||
|
updateSurvey = jest.fn();
|
||||||
|
act(() => {
|
||||||
|
wrapper = mountWithContexts(
|
||||||
|
<Switch>
|
||||||
|
<Route path="/templates/:templateType/:id/survey/edit/:variable">
|
||||||
|
<SurveyQuestionEdit survey={survey} updateSurvey={updateSurvey} />
|
||||||
|
</Route>
|
||||||
|
</Switch>,
|
||||||
|
{
|
||||||
|
context: { router: { history } },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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([
|
||||||
|
{
|
||||||
|
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 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',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user