From cfe0607b6a351366699fbcb69f8168e1181a8428 Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Thu, 19 Mar 2020 16:22:12 -0700 Subject: [PATCH] add survey form tests --- .../src/screens/Host/HostAdd/HostAdd.test.jsx | 5 +- .../screens/Template/Survey/SurveyList.jsx | 2 +- .../Template/Survey/SurveyList.test.jsx | 2 +- .../Template/Survey/SurveyQuestionAdd.jsx | 5 +- .../Survey/SurveyQuestionAdd.test.jsx | 126 ++++++++++++++++++ .../Template/Survey/SurveyQuestionEdit.jsx | 5 +- .../Survey/SurveyQuestionEdit.test.jsx | 119 +++++++++++++++++ .../{Survey => shared}/data.job_template.json | 0 8 files changed, 257 insertions(+), 7 deletions(-) create mode 100644 awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.test.jsx create mode 100644 awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.test.jsx rename awx/ui_next/src/screens/Template/{Survey => shared}/data.job_template.json (100%) diff --git a/awx/ui_next/src/screens/Host/HostAdd/HostAdd.test.jsx b/awx/ui_next/src/screens/Host/HostAdd/HostAdd.test.jsx index 1096b83ca6..f6a9362344 100644 --- a/awx/ui_next/src/screens/Host/HostAdd/HostAdd.test.jsx +++ b/awx/ui_next/src/screens/Host/HostAdd/HostAdd.test.jsx @@ -26,7 +26,10 @@ describe('', () => { let history; beforeEach(async () => { - history = createMemoryHistory(); + history = createMemoryHistory({ + initialEntries: ['/templates/job_templates/1/survey/edit/foo'], + state: { some: 'state' }, + }); await act(async () => { wrapper = mountWithContexts(, { context: { router: { history } }, diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyList.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyList.jsx index e020876bfc..5c8a53f0bd 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyList.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyList.jsx @@ -131,7 +131,7 @@ function SurveyList({ >
{i18n._(t`This action will delete the following:`)}
{selected.map(question => ( - + {question.question_name}
diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyList.test.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyList.test.jsx index d0e7e2df15..acce909fd7 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyList.test.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyList.test.jsx @@ -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'); diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx index 057f74e25a..29304f3069 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx @@ -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; } diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.test.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.test.jsx new file mode 100644 index 0000000000..0d0e3bed5e --- /dev/null +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.test.jsx @@ -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('', () => { + let updateSurvey; + + beforeEach(() => { + updateSurvey = jest.fn(); + }); + + test('should render form', () => { + let wrapper; + act(() => { + wrapper = mountWithContexts( + + ); + }); + + expect(wrapper.find('SurveyQuestionForm')).toHaveLength(1); + }); + + test('should call updateSurvey', () => { + let wrapper; + act(() => { + wrapper = mountWithContexts( + + ); + }); + + 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( + + ); + }); + + 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( + + ); + }); + + 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; + }); +}); diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.jsx index f6f391fb13..cb9dac0a58 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.jsx @@ -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; } diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.test.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.test.jsx new file mode 100644 index 0000000000..25665b339d --- /dev/null +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.test.jsx @@ -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('', () => { + let updateSurvey; + let history; + let wrapper; + + beforeEach(() => { + history = createMemoryHistory({ + initialEntries: ['/templates/job_templates/1/survey/edit/foo'], + }); + updateSurvey = jest.fn(); + act(() => { + wrapper = mountWithContexts( + + + + + , + { + 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; + }); +}); diff --git a/awx/ui_next/src/screens/Template/Survey/data.job_template.json b/awx/ui_next/src/screens/Template/shared/data.job_template.json similarity index 100% rename from awx/ui_next/src/screens/Template/Survey/data.job_template.json rename to awx/ui_next/src/screens/Template/shared/data.job_template.json