Bump up unit test coverage for OrganizationAdd component.

This commit is contained in:
kialam 2019-01-15 15:51:37 -05:00
parent fc32cf026f
commit 7230b4bf8d
No known key found for this signature in database
GPG Key ID: 2D0E60E4B8C7EA0F
2 changed files with 21 additions and 3 deletions

View File

@ -67,9 +67,25 @@ describe('<OrganizationAdd />', () => {
expect(results).toEqual(expected);
});
});
test('API response is formatted properly', (done) => {
const spy = jest.spyOn(OrganizationAdd.WrappedComponent.prototype, 'format');
const mockedResp = {data: {id: 1, name: 'foo bar'} };
const api = { getInstanceGroups: jest.fn().mockResolvedValue(mockedResp) };
mount(
<MemoryRouter>
<OrganizationAdd api={api} />
</MemoryRouter>
);
setImmediate(() => {
expect(spy).toHaveBeenCalled();
done();
});
});
test('Successful form submission triggers redirect', (done) => {
const spy = jest.spyOn(OrganizationAdd.WrappedComponent.prototype, 'onSuccess');
const onSuccess = jest.spyOn(OrganizationAdd.WrappedComponent.prototype, 'onSuccess');
const resetForm = jest.spyOn(OrganizationAdd.WrappedComponent.prototype, 'resetForm');
const mockedResp = {data: {id: 1, related: {instance_groups: '/bar'}}};
const api = { createOrganization: jest.fn().mockResolvedValue(mockedResp), createInstanceGroups: jest.fn().mockResolvedValue('done') };
const wrapper = mount(
@ -80,7 +96,8 @@ describe('<OrganizationAdd />', () => {
wrapper.find('input#add-org-form-name').simulate('change', { target: { value: 'foo' } });
wrapper.find('button.at-C-SubmitButton').prop('onClick')();
setImmediate(() => {
expect(spy).toHaveBeenCalled();
expect(onSuccess).toHaveBeenCalled();
expect(resetForm).toHaveBeenCalled();
done();
});
});

View File

@ -30,6 +30,7 @@ class OrganizationAdd extends React.Component {
this.onSelectChange = this.onSelectChange.bind(this);
this.onLookupChange = this.onLookupChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.resetForm = this.resetForm.bind(this);
this.onSuccess = this.onSuccess.bind(this);
this.onCancel = this.onCancel.bind(this);
this.format = this.format.bind(this);
@ -83,11 +84,11 @@ class OrganizationAdd extends React.Component {
selected.forEach( async (select) => {
await api.createInstanceGroups(url, select.id);
});
this.resetForm();
}
} catch (err) {
this.setState({ createInstanceGroupsError: err })
} finally {
this.resetForm();
this.onSuccess(response.id);
}
}