mirror of
https://github.com/ansible/awx.git
synced 2026-03-26 21:35:01 -02:30
Fix & Add unit tests.
This commit is contained in:
@@ -1,28 +1,7 @@
|
||||
import React from 'react';
|
||||
import { mount } from 'enzyme';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
|
||||
let OrganizationAdd;
|
||||
const getAppWithConfigContext = (context = {
|
||||
custom_virtualenvs: ['foo', 'bar']
|
||||
}) => {
|
||||
|
||||
// Mock the ConfigContext module being used in our OrganizationAdd component
|
||||
jest.doMock('../../../../src/context', () => {
|
||||
return {
|
||||
ConfigContext: {
|
||||
Consumer: (props) => props.children(context)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Return the updated OrganizationAdd module with mocked context
|
||||
return require('../../../../src/pages/Organizations/screens/OrganizationAdd').default;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
OrganizationAdd = getAppWithConfigContext();
|
||||
})
|
||||
import OrganizationAdd from '../../../../src/pages/Organizations/screens/OrganizationAdd'
|
||||
|
||||
describe('<OrganizationAdd />', () => {
|
||||
test('initially renders succesfully', () => {
|
||||
@@ -78,4 +57,31 @@ describe('<OrganizationAdd />', () => {
|
||||
wrapper.find('button.at-C-CancelButton').prop('onClick')();
|
||||
expect(spy).toBeCalled();
|
||||
});
|
||||
test('API response data is formatted properly', () => {
|
||||
const mockData = { data: { results: [{ name: 'test instance', id: 1 }] } };
|
||||
const promise = Promise.resolve(mockData);
|
||||
|
||||
return promise.then(({ data }) => {
|
||||
const expected = [{ id: 1, name: 'test instance', isChecked: false }];
|
||||
const results = OrganizationAdd.WrappedComponent.prototype.format(data);
|
||||
expect(results).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
test('Successful form submission triggers redirect', (done) => {
|
||||
const spy = jest.spyOn(OrganizationAdd.WrappedComponent.prototype, 'onSuccess');
|
||||
const mockedResp = {data: {id: 1, related: {instance_groups: '/bar'}}};
|
||||
const api = { createOrganization: jest.fn().mockResolvedValue(mockedResp), createInstanceGroups: jest.fn().mockResolvedValue('done') };
|
||||
const wrapper = mount(
|
||||
<MemoryRouter>
|
||||
<OrganizationAdd api={api} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
wrapper.find('input#add-org-form-name').simulate('change', { target: { value: 'foo' } });
|
||||
wrapper.find('button.at-C-SubmitButton').prop('onClick')();
|
||||
setImmediate(() => {
|
||||
expect(spy).toHaveBeenCalled();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user