mirror of
https://github.com/ansible/awx.git
synced 2026-02-27 07:56:06 -03:30
Address PR feedback
This commit is contained in:
@@ -14,13 +14,13 @@ import JobTemplateForm from '../shared/JobTemplateForm';
|
|||||||
import { JobTemplatesAPI } from '@api';
|
import { JobTemplatesAPI } from '@api';
|
||||||
|
|
||||||
function JobTemplateAdd({ history, i18n }) {
|
function JobTemplateAdd({ history, i18n }) {
|
||||||
const [error, setError] = useState('');
|
const [error, setError] = useState(null);
|
||||||
|
|
||||||
const handleSubmit = async values => {
|
const handleSubmit = async values => {
|
||||||
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const data = await JobTemplatesAPI.create(values);
|
const { data } = await JobTemplatesAPI.create(values);
|
||||||
const { response } = data;
|
history.push(`/templates/${data.type}/${data.id}/details`);
|
||||||
history.push(`/templates/${response.type}/${response.id}/details`);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err);
|
setError(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
||||||
import JobTemplateAdd from './JobTemplateAdd';
|
import JobTemplateAdd from './JobTemplateAdd';
|
||||||
|
import { JobTemplatesAPI } from '../../../api';
|
||||||
|
|
||||||
jest.mock('@api');
|
jest.mock('@api');
|
||||||
|
|
||||||
describe('<JobTemplateAdd />', () => {
|
describe('<JobTemplateAdd />', () => {
|
||||||
const defaultProps = {
|
const defaultProps = {
|
||||||
description: '',
|
description: '',
|
||||||
inventory: 0,
|
inventory: '',
|
||||||
job_type: 'run',
|
job_type: 'run',
|
||||||
name: '',
|
name: '',
|
||||||
playbook: '',
|
playbook: '',
|
||||||
project: 0,
|
project: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -25,26 +26,84 @@ describe('<JobTemplateAdd />', () => {
|
|||||||
|
|
||||||
test('should render Job Template Form with default values', () => {
|
test('should render Job Template Form with default values', () => {
|
||||||
const wrapper = mountWithContexts(<JobTemplateAdd />);
|
const wrapper = mountWithContexts(<JobTemplateAdd />);
|
||||||
expect(wrapper.find('input#template-description').props().value).toBe(
|
expect(wrapper.find('input#template-description').text()).toBe(
|
||||||
defaultProps.description
|
defaultProps.description
|
||||||
);
|
);
|
||||||
expect(wrapper.find('input#template-inventory').props().value).toBe(
|
expect(wrapper.find('input#template-inventory').text()).toBe(
|
||||||
defaultProps.inventory
|
defaultProps.inventory
|
||||||
);
|
);
|
||||||
expect(wrapper.find('AnsibleSelect[name="job_type"]').props().value).toBe(
|
expect(wrapper.find('AnsibleSelect[name="job_type"]').props().value).toBe(
|
||||||
defaultProps.job_type
|
defaultProps.job_type
|
||||||
);
|
);
|
||||||
expect(wrapper.find('input#template-name').props().value).toBe(
|
expect(
|
||||||
defaultProps.name
|
wrapper
|
||||||
);
|
.find('AnsibleSelect[name="job_type"]')
|
||||||
expect(wrapper.find('input#template-playbook').props().value).toBe(
|
.containsAllMatchingElements([
|
||||||
|
<option>Choose a job type</option>,
|
||||||
|
<option>Run</option>,
|
||||||
|
<option>Check</option>,
|
||||||
|
])
|
||||||
|
).toEqual(true);
|
||||||
|
expect(wrapper.find('input#template-name').text()).toBe(defaultProps.name);
|
||||||
|
expect(wrapper.find('input#template-playbook').text()).toBe(
|
||||||
defaultProps.playbook
|
defaultProps.playbook
|
||||||
);
|
);
|
||||||
expect(wrapper.find('input#template-project').props().value).toBe(
|
expect(wrapper.find('input#template-project').text()).toBe(
|
||||||
defaultProps.project
|
defaultProps.project
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('handleSubmit should post to api', async done => {
|
||||||
|
const jobTemplateData = {
|
||||||
|
description: 'Baz',
|
||||||
|
inventory: 1,
|
||||||
|
job_type: 'run',
|
||||||
|
name: 'Foo',
|
||||||
|
playbook: 'Bar',
|
||||||
|
project: 2,
|
||||||
|
};
|
||||||
|
JobTemplatesAPI.create.mockResolvedValueOnce({
|
||||||
|
data: {
|
||||||
|
id: 1,
|
||||||
|
...jobTemplateData,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const wrapper = mountWithContexts(<JobTemplateAdd />);
|
||||||
|
await wrapper.find('JobTemplateForm').prop('handleSubmit')(jobTemplateData);
|
||||||
|
expect(JobTemplatesAPI.create).toHaveBeenCalledWith(jobTemplateData);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should navigate to job template detail after form submission', async done => {
|
||||||
|
const history = {
|
||||||
|
push: jest.fn(),
|
||||||
|
};
|
||||||
|
const jobTemplateData = {
|
||||||
|
description: 'Baz',
|
||||||
|
inventory: 1,
|
||||||
|
job_type: 'run',
|
||||||
|
name: 'Foo',
|
||||||
|
playbook: 'Bar',
|
||||||
|
project: 2,
|
||||||
|
};
|
||||||
|
JobTemplatesAPI.create.mockResolvedValueOnce({
|
||||||
|
data: {
|
||||||
|
id: 1,
|
||||||
|
type: 'job_template',
|
||||||
|
...jobTemplateData,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const wrapper = mountWithContexts(<JobTemplateAdd />, {
|
||||||
|
context: { router: { history } },
|
||||||
|
});
|
||||||
|
|
||||||
|
await wrapper.find('JobTemplateForm').prop('handleSubmit')(jobTemplateData);
|
||||||
|
expect(history.push).toHaveBeenCalledWith(
|
||||||
|
'/templates/job_template/1/details'
|
||||||
|
);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
test('should navigate to templates list when cancel is clicked', () => {
|
test('should navigate to templates list when cancel is clicked', () => {
|
||||||
const history = {
|
const history = {
|
||||||
push: jest.fn(),
|
push: jest.fn(),
|
||||||
|
|||||||
@@ -29,9 +29,9 @@ class JobTemplateForm extends Component {
|
|||||||
template: {
|
template: {
|
||||||
name: '',
|
name: '',
|
||||||
description: '',
|
description: '',
|
||||||
inventory: 0,
|
inventory: '',
|
||||||
job_type: 'run',
|
job_type: 'run',
|
||||||
project: 0,
|
project: '',
|
||||||
playbook: '',
|
playbook: '',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
import { shape, arrayOf, number, string, bool, oneOf } from 'prop-types';
|
import {
|
||||||
|
shape,
|
||||||
|
arrayOf,
|
||||||
|
number,
|
||||||
|
string,
|
||||||
|
bool,
|
||||||
|
oneOf,
|
||||||
|
oneOfType,
|
||||||
|
} from 'prop-types';
|
||||||
|
|
||||||
export const Role = shape({
|
export const Role = shape({
|
||||||
descendent_roles: arrayOf(string),
|
descendent_roles: arrayOf(string),
|
||||||
@@ -57,8 +65,8 @@ export const QSConfig = shape({
|
|||||||
export const JobTemplate = shape({
|
export const JobTemplate = shape({
|
||||||
name: string.isRequired,
|
name: string.isRequired,
|
||||||
description: string,
|
description: string,
|
||||||
inventory: number.isRequired,
|
inventory: oneOfType([number, string]).isRequired,
|
||||||
job_type: oneOf(['run', 'check']),
|
job_type: oneOf(['run', 'check']),
|
||||||
playbook: string.isRequired,
|
playbook: string.isRequired,
|
||||||
project: number.isRequired,
|
project: oneOfType([number, string]).isRequired,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user