update SurveyList tests, add TemplateSurvey tests

This commit is contained in:
Keith Grant
2020-03-13 10:17:39 -07:00
parent ac9f526cf0
commit 3e616f2770
2 changed files with 119 additions and 71 deletions

View File

@@ -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('<TemplateSurvey />', () => {
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(
<TemplateSurvey template={mockJobTemplateData} />,
{
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(
<TemplateSurvey template={{ ...mockJobTemplateData, id: 'a' }} />
);
});
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(
<TemplateSurvey template={mockJobTemplateData} />,
{
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' },
],
});
});
});

View File

@@ -1,104 +1,69 @@
import React from 'react'; import React from 'react';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import { mountWithContexts, waitForElement } 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 './data.job_template.json';
jest.mock('@api/models/JobTemplates'); jest.mock('@api/models/JobTemplates');
const surveyData = {
name: 'Survey',
description: 'description for survey',
spec: [
{ question_name: 'Foo', type: 'text', default: 'Bar', variable: 'foo' },
],
};
describe('<SurveyList />', () => { describe('<SurveyList />', () => {
beforeEach(() => { // beforeEach(() => {
JobTemplatesAPI.readSurvey.mockResolvedValue({ // JobTemplatesAPI.readSurvey.mockResolvedValue({
data: { // data: {
name: 'Survey', // },
description: 'description for survey', // });
spec: [{ question_name: 'Foo', type: 'text', default: 'Bar' }], // });
},
});
});
test('expect component to mount successfully', async () => { test('expect component to mount successfully', async () => {
let wrapper; let wrapper;
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(<SurveyList survey={surveyData} />);
<SurveyList template={mockJobTemplateData} />
);
}); });
expect(wrapper.length).toBe(1); expect(wrapper.length).toBe(1);
}); });
test('expect api to be called to get survey', async () => {
let wrapper;
await act(async () => {
wrapper = mountWithContexts(
<SurveyList template={mockJobTemplateData} />
);
});
expect(JobTemplatesAPI.readSurvey).toBeCalledWith(7);
wrapper.update(); test('should toggle survey', async () => {
const toggleSurvey = jest.fn();
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(
<SurveyList template={{ ...mockJobTemplateData, id: 'a' }} />
);
});
wrapper.update();
expect(wrapper.find('ContentError').length).toBe(1);
});
test('can toggle survey on and off', async () => {
JobTemplatesAPI.update.mockResolvedValue(); JobTemplatesAPI.update.mockResolvedValue();
let wrapper; let wrapper;
await act(async () => { await act(async () => {
<<<<<<< HEAD
wrapper = mountWithContexts( wrapper = mountWithContexts(
=======
wrapper = await mountWithContexts(
>>>>>>> Adds SurveyList tool bar
<SurveyList <SurveyList
template={{ ...mockJobTemplateData, survey_enabled: false }} survey={surveyData}
surveyEnabled
toggleSurvey={toggleSurvey}
/> />
); );
}); });
expect(wrapper.find('Switch').length).toBe(1); 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 () => { await act(async () => {
<<<<<<< HEAD
wrapper.find('Switch').invoke('onChange')(true); wrapper.find('Switch').invoke('onChange')(true);
=======
await wrapper.find('Switch').invoke('onChange')(true);
>>>>>>> Adds SurveyList tool bar
}); });
wrapper.update(); wrapper.update();
expect(wrapper.find('Switch').prop('isChecked')).toBe(true); expect(toggleSurvey).toHaveBeenCalled();
expect(JobTemplatesAPI.update).toBeCalledWith(7, {
survey_enabled: true,
});
}); });
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; let wrapper;
await act(async () => { await act(async () => {
<<<<<<< HEAD
wrapper = mountWithContexts( wrapper = mountWithContexts(
======= <SurveyList survey={surveyData} deleteSurvey={deleteSurvey} />
wrapper = await mountWithContexts(
>>>>>>> Adds SurveyList tool bar
<SurveyList
template={{ ...mockJobTemplateData, survey_enabled: false }}
/>
); );
}); });
await waitForElement(wrapper, 'SurveyListItem'); wrapper.update();
expect(wrapper.find('Button[variant="danger"]').prop('isDisabled')).toBe( expect(wrapper.find('Button[variant="danger"]').prop('isDisabled')).toBe(
true true
); );
@@ -112,38 +77,32 @@ describe('<SurveyList />', () => {
true true
); );
}); });
wrapper.update(); wrapper.update();
expect( expect(
wrapper.find('Checkbox[aria-label="Select all"]').prop('isChecked') wrapper.find('Checkbox[aria-label="Select all"]').prop('isChecked')
).toBe(true); ).toBe(true);
expect(wrapper.find('Button[variant="danger"]').prop('isDisabled')).toBe( expect(wrapper.find('Button[variant="danger"]').prop('isDisabled')).toBe(
false false
); );
act(() => { act(() => {
wrapper.find('Button[variant="danger"]').invoke('onClick')(); wrapper.find('Button[variant="danger"]').invoke('onClick')();
}); });
wrapper.update(); wrapper.update();
await act(() => await act(() =>
wrapper.find('Button[aria-label="confirm delete"]').invoke('onClick')() wrapper.find('Button[aria-label="confirm delete"]').invoke('onClick')()
); );
expect(JobTemplatesAPI.destroySurvey).toBeCalledWith(7); expect(deleteSurvey).toHaveBeenCalled();
}); });
}); });
describe('Survey with no questions', () => { describe('Survey with no questions', () => {
test('Survey with no questions renders empty state', async () => { test('Survey with no questions renders empty state', async () => {
JobTemplatesAPI.readSurvey.mockResolvedValue({}); JobTemplatesAPI.readSurvey.mockResolvedValue({});
let wrapper; let wrapper;
await act(async () => { await act(async () => {
<<<<<<< HEAD
wrapper = mountWithContexts( wrapper = mountWithContexts(
=======
wrapper = await mountWithContexts(
>>>>>>> Adds SurveyList tool bar
<SurveyList template={mockJobTemplateData} /> <SurveyList template={mockJobTemplateData} />
); );
}); });