mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 17:37:37 -02:30
Add Project Edit test coverage
This commit is contained in:
@@ -108,7 +108,11 @@ describe('<ProjectAdd />', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
await changeState;
|
await changeState;
|
||||||
wrapper.find('form').simulate('submit');
|
await act(async () => {
|
||||||
|
wrapper.find('form').simulate('submit');
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
expect(ProjectsAPI.create).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('handleSubmit should throw an error', async () => {
|
test('handleSubmit should throw an error', async () => {
|
||||||
|
|||||||
153
awx/ui_next/src/screens/Project/ProjectEdit/ProjectEdit.test.jsx
Normal file
153
awx/ui_next/src/screens/Project/ProjectEdit/ProjectEdit.test.jsx
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { act } from 'react-dom/test-utils';
|
||||||
|
import { createMemoryHistory } from 'history';
|
||||||
|
import { mountWithContexts, waitForElement } from '@testUtils/enzymeHelpers';
|
||||||
|
import ProjectEdit from './ProjectEdit';
|
||||||
|
import { ProjectsAPI, CredentialTypesAPI } from '@api';
|
||||||
|
|
||||||
|
jest.mock('@api');
|
||||||
|
|
||||||
|
describe('<ProjectEdit />', () => {
|
||||||
|
let wrapper;
|
||||||
|
const projectData = {
|
||||||
|
id: 123,
|
||||||
|
name: 'foo',
|
||||||
|
description: 'bar',
|
||||||
|
scm_type: 'git',
|
||||||
|
scm_url: 'https://foo.bar',
|
||||||
|
scm_clean: true,
|
||||||
|
credential: 100,
|
||||||
|
organization: 2,
|
||||||
|
scm_update_on_launch: true,
|
||||||
|
scm_update_cache_timeout: 3,
|
||||||
|
allow_override: false,
|
||||||
|
custom_virtualenv: '/venv/custom-env',
|
||||||
|
summary_fields: {
|
||||||
|
credential: {
|
||||||
|
id: 100,
|
||||||
|
credential_type_id: 5,
|
||||||
|
kind: 'insights',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const projectOptionsResolve = {
|
||||||
|
data: {
|
||||||
|
actions: {
|
||||||
|
GET: {
|
||||||
|
scm_type: {
|
||||||
|
choices: [
|
||||||
|
['', 'Manual'],
|
||||||
|
['git', 'Git'],
|
||||||
|
['hg', 'Mercurial'],
|
||||||
|
['svn', 'Subversion'],
|
||||||
|
['insights', 'Red Hat Insights'],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const scmCredentialResolve = {
|
||||||
|
data: {
|
||||||
|
results: [
|
||||||
|
{
|
||||||
|
id: 4,
|
||||||
|
name: 'Source Control',
|
||||||
|
kind: 'scm',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const insightsCredentialResolve = {
|
||||||
|
data: {
|
||||||
|
results: [
|
||||||
|
{
|
||||||
|
id: 5,
|
||||||
|
name: 'Insights',
|
||||||
|
kind: 'insights',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await ProjectsAPI.readOptions.mockImplementation(
|
||||||
|
() => projectOptionsResolve
|
||||||
|
);
|
||||||
|
await CredentialTypesAPI.read.mockImplementationOnce(
|
||||||
|
() => scmCredentialResolve
|
||||||
|
);
|
||||||
|
await CredentialTypesAPI.read.mockImplementationOnce(
|
||||||
|
() => insightsCredentialResolve
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('initially renders successfully', async () => {
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(<ProjectEdit project={projectData} />);
|
||||||
|
});
|
||||||
|
expect(wrapper.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handleSubmit should post to the api', async () => {
|
||||||
|
const history = createMemoryHistory();
|
||||||
|
ProjectsAPI.update.mockResolvedValueOnce({
|
||||||
|
data: { ...projectData },
|
||||||
|
});
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(<ProjectEdit project={projectData} />, {
|
||||||
|
context: { router: { history } },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
await waitForElement(wrapper, 'ContentLoading', el => el.length === 0);
|
||||||
|
await act(async () => {
|
||||||
|
wrapper.find('form').simulate('submit');
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
expect(ProjectsAPI.update).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handleSubmit should throw an error', async () => {
|
||||||
|
ProjectsAPI.update.mockImplementation(() => Promise.reject(new Error()));
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(<ProjectEdit project={projectData} />);
|
||||||
|
});
|
||||||
|
await waitForElement(wrapper, 'ContentLoading', el => el.length === 0);
|
||||||
|
await act(async () => {
|
||||||
|
wrapper.find('form').simulate('submit');
|
||||||
|
});
|
||||||
|
wrapper.update();
|
||||||
|
expect(ProjectsAPI.update).toHaveBeenCalledTimes(1);
|
||||||
|
expect(wrapper.find('ProjectEdit .formSubmitError').length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('CardHeader close button should navigate to project details', async () => {
|
||||||
|
const history = createMemoryHistory();
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(<ProjectEdit project={projectData} />, {
|
||||||
|
context: { router: { history } },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
wrapper.find('CardCloseButton').simulate('click');
|
||||||
|
expect(history.location.pathname).toEqual('/projects/123/details');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('CardBody cancel button should navigate to project details', async () => {
|
||||||
|
const history = createMemoryHistory();
|
||||||
|
await act(async () => {
|
||||||
|
wrapper = mountWithContexts(<ProjectEdit project={projectData} />, {
|
||||||
|
context: { router: { history } },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
await waitForElement(wrapper, 'EmptyStateBody', el => el.length === 0);
|
||||||
|
wrapper.find('ProjectEdit button[aria-label="Cancel"]').simulate('click');
|
||||||
|
expect(history.location.pathname).toEqual('/projects/123/details');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -122,16 +122,23 @@ function ProjectForm({ project, ...props }) {
|
|||||||
scm_update_cache_timeout: 0,
|
scm_update_cache_timeout: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Save current scm subform field values to state */
|
||||||
const saveSubFormState = form => {
|
const saveSubFormState = form => {
|
||||||
const updatedScmFormFields = { ...scmFormFields };
|
const currentScmFormFields = { ...scmFormFields };
|
||||||
|
|
||||||
Object.keys(updatedScmFormFields).forEach(label => {
|
Object.keys(currentScmFormFields).forEach(label => {
|
||||||
updatedScmFormFields[label] = form.values[label];
|
currentScmFormFields[label] = form.values[label];
|
||||||
});
|
});
|
||||||
|
|
||||||
setScmSubFormState(updatedScmFormFields);
|
setScmSubFormState(currentScmFormFields);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If scm type is !== the initial scm type value,
|
||||||
|
* reset scm subform field values to defaults.
|
||||||
|
* If scm type is === the initial scm type value,
|
||||||
|
* reset scm subform field values to scmSubFormState.
|
||||||
|
*/
|
||||||
const resetScmTypeFields = (value, form) => {
|
const resetScmTypeFields = (value, form) => {
|
||||||
if (form.values.scm_type === form.initialValues.scm_type) {
|
if (form.values.scm_type === form.initialValues.scm_type) {
|
||||||
saveSubFormState(form);
|
saveSubFormState(form);
|
||||||
|
|||||||
@@ -21,6 +21,13 @@ describe('<ProjectAdd />', () => {
|
|||||||
scm_update_cache_timeout: 3,
|
scm_update_cache_timeout: 3,
|
||||||
allow_override: false,
|
allow_override: false,
|
||||||
custom_virtualenv: '/venv/custom-env',
|
custom_virtualenv: '/venv/custom-env',
|
||||||
|
summary_fields: {
|
||||||
|
credential: {
|
||||||
|
id: 100,
|
||||||
|
credential_type_id: 4,
|
||||||
|
kind: 'scm',
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const projectOptionsResolve = {
|
const projectOptionsResolve = {
|
||||||
|
|||||||
Reference in New Issue
Block a user