From 3e616f2770f67a5fd197f2cefdd911363c406347 Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Fri, 13 Mar 2020 10:17:39 -0700 Subject: [PATCH] update SurveyList tests, add TemplateSurvey tests --- .../screens/Template/TemplateSurvey.test.jsx | 89 +++++++++++++++ .../Template/shared/SurveyList.test.jsx | 101 ++++++------------ 2 files changed, 119 insertions(+), 71 deletions(-) create mode 100644 awx/ui_next/src/screens/Template/TemplateSurvey.test.jsx diff --git a/awx/ui_next/src/screens/Template/TemplateSurvey.test.jsx b/awx/ui_next/src/screens/Template/TemplateSurvey.test.jsx new file mode 100644 index 0000000000..e1846923ec --- /dev/null +++ b/awx/ui_next/src/screens/Template/TemplateSurvey.test.jsx @@ -0,0 +1,89 @@ +import React from 'react'; +import { act } from 'react-dom/test-utils'; +import { createMemoryHistory } from 'history'; +import { mountWithContexts } from '@testUtils/enzymeHelpers'; +import TemplateSurvey from './TemplateSurvey'; +import { JobTemplatesAPI } from '@api'; +import mockJobTemplateData from './shared/data.job_template.json'; + +jest.mock('@api/models/JobTemplates'); + +const surveyData = { + name: 'Survey', + description: 'description for survey', + spec: [ + { question_name: 'Foo', type: 'text', default: 'Bar', variable: 'foo' }, + ], +}; + +describe('', () => { + beforeEach(() => { + JobTemplatesAPI.readSurvey.mockResolvedValue({ + data: surveyData, + }); + }); + + test('should fetch survey from API', async () => { + const history = createMemoryHistory({ + initialEntries: ['/templates/job_template/1/survey'], + }); + let wrapper; + await act(async () => { + wrapper = mountWithContexts( + , + { + context: { router: { history } }, + } + ); + }); + wrapper.update(); + expect(JobTemplatesAPI.readSurvey).toBeCalledWith(7); + + expect(wrapper.find('SurveyList').prop('survey')).toEqual(surveyData); + }); + + test('should display error in retrieving survey', async () => { + JobTemplatesAPI.readSurvey.mockRejectedValue(new Error()); + let wrapper; + await act(async () => { + wrapper = mountWithContexts( + + ); + }); + + wrapper.update(); + + expect(wrapper.find('ContentError').length).toBe(1); + }); + + test('should update API with survey changes', async () => { + const history = createMemoryHistory({ + initialEntries: ['/templates/job_template/1/survey'], + }); + let wrapper; + await act(async () => { + wrapper = mountWithContexts( + , + { + context: { router: { history } }, + } + ); + }); + wrapper.update(); + + await act(async () => { + await wrapper.find('SurveyList').invoke('updateSurvey')([ + { question_name: 'Foo', type: 'text', default: 'One', variable: 'foo' }, + { question_name: 'Bar', type: 'text', default: 'Two', variable: 'bar' }, + ]); + }); + expect(JobTemplatesAPI.updateSurvey).toHaveBeenCalledWith(7, { + name: 'Survey', + description: 'description for survey', + spec: [ + { question_name: 'Foo', type: 'text', default: 'One', variable: 'foo' }, + { question_name: 'Bar', type: 'text', default: 'Two', variable: 'bar' }, + ], + }); + }); +}); diff --git a/awx/ui_next/src/screens/Template/shared/SurveyList.test.jsx b/awx/ui_next/src/screens/Template/shared/SurveyList.test.jsx index bfd74023b3..9998e6569f 100644 --- a/awx/ui_next/src/screens/Template/shared/SurveyList.test.jsx +++ b/awx/ui_next/src/screens/Template/shared/SurveyList.test.jsx @@ -1,104 +1,69 @@ import React from 'react'; import { act } from 'react-dom/test-utils'; -import { mountWithContexts, waitForElement } from '@testUtils/enzymeHelpers'; +import { mountWithContexts } from '@testUtils/enzymeHelpers'; import SurveyList from './SurveyList'; import { JobTemplatesAPI } from '@api'; import mockJobTemplateData from './data.job_template.json'; jest.mock('@api/models/JobTemplates'); +const surveyData = { + name: 'Survey', + description: 'description for survey', + spec: [ + { question_name: 'Foo', type: 'text', default: 'Bar', variable: 'foo' }, + ], +}; + describe('', () => { - beforeEach(() => { - JobTemplatesAPI.readSurvey.mockResolvedValue({ - data: { - name: 'Survey', - description: 'description for survey', - spec: [{ question_name: 'Foo', type: 'text', default: 'Bar' }], - }, - }); - }); + // beforeEach(() => { + // JobTemplatesAPI.readSurvey.mockResolvedValue({ + // data: { + // }, + // }); + // }); + test('expect component to mount successfully', async () => { let wrapper; await act(async () => { - wrapper = mountWithContexts( - - ); + wrapper = mountWithContexts(); }); expect(wrapper.length).toBe(1); }); - test('expect api to be called to get survey', async () => { - let wrapper; - await act(async () => { - wrapper = mountWithContexts( - - ); - }); - expect(JobTemplatesAPI.readSurvey).toBeCalledWith(7); - wrapper.update(); - - expect(wrapper.find('SurveyListItem').length).toBe(1); - }); - test('error in retrieving the survey throws an error', async () => { - JobTemplatesAPI.readSurvey.mockRejectedValue(new Error()); - let wrapper; - await act(async () => { - wrapper = mountWithContexts( - - ); - }); - - wrapper.update(); - - expect(wrapper.find('ContentError').length).toBe(1); - }); - test('can toggle survey on and off', async () => { + test('should toggle survey', async () => { + const toggleSurvey = jest.fn(); JobTemplatesAPI.update.mockResolvedValue(); let wrapper; await act(async () => { -<<<<<<< HEAD wrapper = mountWithContexts( -======= - wrapper = await mountWithContexts( ->>>>>>> Adds SurveyList tool bar ); }); expect(wrapper.find('Switch').length).toBe(1); - expect(wrapper.find('Switch').prop('isChecked')).toBe(false); + expect(wrapper.find('Switch').prop('isChecked')).toBe(true); await act(async () => { -<<<<<<< HEAD wrapper.find('Switch').invoke('onChange')(true); -======= - await wrapper.find('Switch').invoke('onChange')(true); ->>>>>>> Adds SurveyList tool bar }); - wrapper.update(); - expect(wrapper.find('Switch').prop('isChecked')).toBe(true); - expect(JobTemplatesAPI.update).toBeCalledWith(7, { - survey_enabled: true, - }); + expect(toggleSurvey).toHaveBeenCalled(); }); - test('selectAll enables delete button and calls the api to delete properly', async () => { + test('should select all and delete', async () => { + const deleteSurvey = jest.fn(); let wrapper; await act(async () => { -<<<<<<< HEAD wrapper = mountWithContexts( -======= - wrapper = await mountWithContexts( ->>>>>>> Adds SurveyList tool bar - + ); }); - await waitForElement(wrapper, 'SurveyListItem'); + wrapper.update(); expect(wrapper.find('Button[variant="danger"]').prop('isDisabled')).toBe( true ); @@ -112,38 +77,32 @@ describe('', () => { true ); }); - wrapper.update(); expect( wrapper.find('Checkbox[aria-label="Select all"]').prop('isChecked') ).toBe(true); - expect(wrapper.find('Button[variant="danger"]').prop('isDisabled')).toBe( false ); act(() => { wrapper.find('Button[variant="danger"]').invoke('onClick')(); }); - wrapper.update(); await act(() => wrapper.find('Button[aria-label="confirm delete"]').invoke('onClick')() ); - expect(JobTemplatesAPI.destroySurvey).toBeCalledWith(7); + expect(deleteSurvey).toHaveBeenCalled(); }); }); + describe('Survey with no questions', () => { test('Survey with no questions renders empty state', async () => { JobTemplatesAPI.readSurvey.mockResolvedValue({}); let wrapper; await act(async () => { -<<<<<<< HEAD wrapper = mountWithContexts( -======= - wrapper = await mountWithContexts( ->>>>>>> Adds SurveyList tool bar ); });