Fix & Add unit tests.

This commit is contained in:
kialam 2019-01-15 14:58:23 -05:00
parent 215c23609c
commit e8fe6fe33c
No known key found for this signature in database
GPG Key ID: 2D0E60E4B8C7EA0F
5 changed files with 85 additions and 28 deletions

View File

@ -126,4 +126,16 @@ describe('APIClient (api.js)', () => {
done();
});
test('getInstanceGroups calls expected http method', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ get: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
await api.getInstanceGroups();
expect(mockHttp.get).toHaveBeenCalledTimes(1);
done();
});
});

View File

@ -0,0 +1,33 @@
import React from 'react';
import { mount } from 'enzyme';
import Lookup from '../../src/components/Lookup';
import { I18nProvider } from '@lingui/react';
const mockData = [{ name: 'foo', id: 0, isChecked: false }];
describe('<Lookup />', () => {
test('initially renders succesfully', () => {
mount(
<Lookup
lookup_header="Foo Bar"
lookupChange={() => { }}
data={mockData}
/>
);
});
test('calls "onLookup" when search icon is clicked', () => {
const spy = jest.spyOn(Lookup.prototype, 'onLookup');
const wrapper = mount(
<I18nProvider>
<Lookup
lookup_header="Foo Bar"
lookupChange={() => { }}
data={mockData}
/>
</I18nProvider>
);
expect(spy).not.toHaveBeenCalled();
wrapper.find('#search').simulate('click');
expect(spy).toHaveBeenCalled();
});
});

View File

@ -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();
});
});
});

View File

@ -9,7 +9,7 @@ import {
ToolbarGroup,
} from '@patternfly/react-core';
import CheckboxListItem from '../CheckboxListItem'
import CheckboxListItem from '../ListItem'
class Lookup extends React.Component {
constructor(props) {

View File

@ -30,7 +30,9 @@ class OrganizationAdd extends React.Component {
this.onSelectChange = this.onSelectChange.bind(this);
this.onLookupChange = this.onLookupChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.onSuccess = this.onSuccess.bind(this);
this.onCancel = this.onCancel.bind(this);
this.format = this.format.bind(this);
}
state = {
@ -98,19 +100,23 @@ class OrganizationAdd extends React.Component {
this.props.history.push(`/organizations/${id}`);
}
format(data) {
let results = [];
data.results.map((result) => {
results.push({ id: result.id, name: result.name, isChecked: false });
});
return results;
};
async componentDidMount() {
const { api } = this.props;
try {
const { data } = await api.getInstanceGroups();
let results = [];
data.results.map((result) => {
results.push({ id: result.id, name: result.name, isChecked: false });
})
this.format(data);
this.setState({ results });
} catch (error) {
this.setState({ getInstanceGroupsError: error })
}
}
render() {