1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-30 13:55:31 +03:00

add survey form tests

This commit is contained in:
Keith Grant 2020-03-19 16:22:12 -07:00
parent 7f24d0c0c2
commit cfe0607b6a
8 changed files with 257 additions and 7 deletions

View File

@ -26,7 +26,10 @@ describe('<HostAdd />', () => {
let history;
beforeEach(async () => {
history = createMemoryHistory();
history = createMemoryHistory({
initialEntries: ['/templates/job_templates/1/survey/edit/foo'],
state: { some: 'state' },
});
await act(async () => {
wrapper = mountWithContexts(<HostAdd />, {
context: { router: { history } },

View File

@ -131,7 +131,7 @@ function SurveyList({
>
<div>{i18n._(t`This action will delete the following:`)}</div>
{selected.map(question => (
<span key={question.id}>
<span key={question.variable}>
<strong>{question.question_name}</strong>
<br />
</span>

View File

@ -3,7 +3,7 @@ import { act } from 'react-dom/test-utils';
import { mountWithContexts } from '@testUtils/enzymeHelpers';
import SurveyList from './SurveyList';
import { JobTemplatesAPI } from '@api';
import mockJobTemplateData from './data.job_template.json';
import mockJobTemplateData from '../shared/data.job_template.json';
jest.mock('@api/models/JobTemplates');

View File

@ -11,8 +11,9 @@ export default function SurveyQuestionAdd({ survey, updateSurvey }) {
const handleSubmit = async question => {
if (survey.spec.find(q => q.variable === question.variable)) {
setFormError(
new Error(`Survey already contains a question with variable named
${question.variable}`)
new Error(
`Survey already contains a question with variable named “${question.variable}`
)
);
return;
}

View File

@ -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;
});
});

View File

@ -27,8 +27,9 @@ export default function SurveyQuestionEdit({ survey, updateSurvey }) {
) {
debugger;
setFormError(
new Error(`Survey already contains a question with variable named
${formData.variable}`)
new Error(
`Survey already contains a question with variable named “${formData.variable}`
)
);
return;
}

View File

@ -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;
});
});