api.js refactor using classes (#250)

Refactor api.js into an api module where endpoint specific models can be imported and used in components.
This commit is contained in:
Michael Abashian
2019-06-07 15:48:09 -04:00
committed by GitHub
parent a8c51670af
commit 2ae93261d1
51 changed files with 839 additions and 727 deletions

View File

@@ -6,6 +6,10 @@ import { asyncFlush } from '../jest.setup';
import App from '../src/App';
import { RootAPI } from '../src/api';
jest.mock('../src/api');
describe('<App />', () => {
test('expected content is rendered', () => {
const appWrapper = mountWithContexts(
@@ -89,15 +93,13 @@ describe('<App />', () => {
});
test('onLogout makes expected call to api client', async (done) => {
const logout = jest.fn(() => Promise.resolve());
const appWrapper = mountWithContexts(<App />, {
context: { network: { api: { logout }, handleHttpError: () => {} } }
context: { network: { handleHttpError: () => {} } }
}).find('App');
appWrapper.instance().onLogout();
await asyncFlush();
expect(logout).toHaveBeenCalledTimes(1);
expect(RootAPI.logout).toHaveBeenCalledTimes(1);
done();
});

View File

@@ -46,17 +46,6 @@ exports[`mountWithContexts injected I18nProvider should mount and render deeply
</Parent>
`;
exports[`mountWithContexts injected Network should mount and render 1`] = `
<Foo
api={"/api/"}
handleHttpError={[Function]}
>
<div>
test
</div>
</Foo>
`;
exports[`mountWithContexts injected Router should mount and render 1`] = `
<div>
<Link

View File

@@ -1,173 +0,0 @@
import APIClient from '../src/api';
const invalidCookie = 'invalid';
const validLoggedOutCookie = 'current_user=%7B%22id%22%3A1%2C%22type%22%3A%22user%22%2C%22url%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2F%22%2C%22related%22%3A%7B%22admin_of_organizations%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fadmin_of_organizations%2F%22%2C%22authorized_tokens%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fauthorized_tokens%2F%22%2C%22roles%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Froles%2F%22%2C%22organizations%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Forganizations%2F%22%2C%22access_list%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Faccess_list%2F%22%2C%22teams%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fteams%2F%22%2C%22tokens%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Ftokens%2F%22%2C%22personal_tokens%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fpersonal_tokens%2F%22%2C%22credentials%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fcredentials%2F%22%2C%22activity_stream%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Factivity_stream%2F%22%2C%22projects%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fprojects%2F%22%7D%2C%22summary_fields%22%3A%7B%7D%2C%22created%22%3A%222018-10-19T16%3A30%3A59.141963Z%22%2C%22username%22%3A%22admin%22%2C%22first_name%22%3A%22%22%2C%22last_name%22%3A%22%22%2C%22email%22%3A%22%22%2C%22is_superuser%22%3Atrue%2C%22is_system_auditor%22%3Afalse%2C%22ldap_dn%22%3A%22%22%2C%22external_account%22%3Anull%2C%22auth%22%3A%5B%5D%7D; userLoggedIn=false; csrftoken=lhOHpLQUFHlIVqx8CCZmEpdEZAz79GIRBIT3asBzTbPE7HS7wizt7WBsgJClz8Ge';
const validLoggedInCookie = 'current_user=%7B%22id%22%3A1%2C%22type%22%3A%22user%22%2C%22url%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2F%22%2C%22related%22%3A%7B%22admin_of_organizations%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fadmin_of_organizations%2F%22%2C%22authorized_tokens%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fauthorized_tokens%2F%22%2C%22roles%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Froles%2F%22%2C%22organizations%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Forganizations%2F%22%2C%22access_list%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Faccess_list%2F%22%2C%22teams%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fteams%2F%22%2C%22tokens%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Ftokens%2F%22%2C%22personal_tokens%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fpersonal_tokens%2F%22%2C%22credentials%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fcredentials%2F%22%2C%22activity_stream%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Factivity_stream%2F%22%2C%22projects%22%3A%22%2Fapi%2Fv2%2Fusers%2F1%2Fprojects%2F%22%7D%2C%22summary_fields%22%3A%7B%7D%2C%22created%22%3A%222018-10-19T16%3A30%3A59.141963Z%22%2C%22username%22%3A%22admin%22%2C%22first_name%22%3A%22%22%2C%22last_name%22%3A%22%22%2C%22email%22%3A%22%22%2C%22is_superuser%22%3Atrue%2C%22is_system_auditor%22%3Afalse%2C%22ldap_dn%22%3A%22%22%2C%22external_account%22%3Anull%2C%22auth%22%3A%5B%5D%7D; userLoggedIn=true; csrftoken=lhOHpLQUFHlIVqx8CCZmEpdEZAz79GIRBIT3asBzTbPE7HS7wizt7WBsgJClz8Ge';
describe('APIClient (api.js)', () => {
test('isAuthenticated returns false when cookie is invalid', () => {
APIClient.getCookie = jest.fn(() => invalidCookie);
const api = new APIClient();
expect(api.isAuthenticated()).toBe(false);
});
test('isAuthenticated returns false when cookie is unauthenticated', () => {
APIClient.getCookie = jest.fn(() => validLoggedOutCookie);
const api = new APIClient();
expect(api.isAuthenticated()).toBe(false);
});
test('isAuthenticated returns true when cookie is valid and authenticated', () => {
APIClient.getCookie = jest.fn(() => validLoggedInCookie);
const api = new APIClient();
expect(api.isAuthenticated()).toBe(true);
});
test('login calls get and post with expected content headers', async (done) => {
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
const createPromise = () => Promise.resolve();
const mockHttp = ({ get: jest.fn(createPromise), post: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
await api.login('username', 'password');
expect(mockHttp.get).toHaveBeenCalledTimes(1);
expect(mockHttp.get.mock.calls[0]).toContainEqual({ headers });
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0]).toContainEqual({ headers });
done();
});
test('login sends expected data', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ get: jest.fn(createPromise), post: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
await api.login('foo', 'bar');
await api.login('foo', 'bar', 'baz');
expect(mockHttp.post).toHaveBeenCalledTimes(2);
expect(mockHttp.post.mock.calls[0]).toContainEqual('username=foo&password=bar&next=%2Fapi%2Fv2%2Fconfig%2F');
expect(mockHttp.post.mock.calls[1]).toContainEqual('username=foo&password=bar&next=baz');
done();
});
test('logout calls expected http method', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ get: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
await api.logout();
expect(mockHttp.get).toHaveBeenCalledTimes(1);
done();
});
test('getConfig calls expected http method', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ get: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
await api.getConfig();
expect(mockHttp.get).toHaveBeenCalledTimes(1);
done();
});
test('getOrganizations calls http method with expected data', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ get: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
const defaultParams = {};
const testParams = { foo: 'bar' };
await api.getOrganizations(testParams);
await api.getOrganizations();
expect(mockHttp.get).toHaveBeenCalledTimes(2);
expect(mockHttp.get.mock.calls[0][1]).toEqual({ params: testParams });
expect(mockHttp.get.mock.calls[1][1]).toEqual({ params: defaultParams });
done();
});
test('createOrganization calls http method with expected data', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ post: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
const data = { name: 'test ' };
await api.createOrganization(data);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0][1]).toEqual(data);
done();
});
test('getOrganizationDetails calls http method with expected data', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ get: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
await api.getOrganizationDetails(99);
expect(mockHttp.get).toHaveBeenCalledTimes(1);
expect(mockHttp.get.mock.calls[0][0]).toContain('99');
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();
});
test('associateInstanceGroup calls expected http method with expected data', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ post: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
const url = 'foo/bar/';
const id = 1;
await api.associateInstanceGroup(url, id);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0][0]).toEqual(url);
expect(mockHttp.post.mock.calls[0][1]).toEqual({ id });
done();
});
test('disassociate calls expected http method with expected data', async (done) => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ post: jest.fn(createPromise) });
const api = new APIClient(mockHttp);
const url = 'foo/bar/';
const id = 1;
await api.disassociate(url, id);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0][0]).toEqual(url);
expect(mockHttp.post.mock.calls[0][1]).toEqual({ id, disassociate: true });
done();
});
});

View File

@@ -0,0 +1,97 @@
import Base from '../../src/api/Base';
describe('Base', () => {
const createPromise = () => Promise.resolve();
const mockBaseURL = '/api/v2/organizations/';
const mockHttp = ({
delete: jest.fn(createPromise),
get: jest.fn(createPromise),
options: jest.fn(createPromise),
patch: jest.fn(createPromise),
post: jest.fn(createPromise),
put: jest.fn(createPromise)
});
const BaseAPI = new Base(mockHttp, mockBaseURL);
afterEach(() => {
jest.clearAllMocks();
});
test('create calls http method with expected data', async (done) => {
const data = { name: 'test ' };
await BaseAPI.create(data);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0][1]).toEqual(data);
done();
});
test('destroy calls http method with expected data', async (done) => {
const resourceId = 1;
await BaseAPI.destroy(resourceId);
expect(mockHttp.delete).toHaveBeenCalledTimes(1);
expect(mockHttp.delete.mock.calls[0][0]).toEqual(`${mockBaseURL}${resourceId}/`);
done();
});
test('read calls http method with expected data', async (done) => {
const defaultParams = {};
const testParams = { foo: 'bar' };
await BaseAPI.read(testParams);
await BaseAPI.read();
expect(mockHttp.get).toHaveBeenCalledTimes(2);
expect(mockHttp.get.mock.calls[0][1]).toEqual({ params: testParams });
expect(mockHttp.get.mock.calls[1][1]).toEqual({ params: defaultParams });
done();
});
test('readDetail calls http method with expected data', async (done) => {
const resourceId = 1;
await BaseAPI.readDetail(resourceId);
expect(mockHttp.get).toHaveBeenCalledTimes(1);
expect(mockHttp.get.mock.calls[0][0]).toEqual(`${mockBaseURL}${resourceId}/`);
done();
});
test('readOptions calls http method with expected data', async (done) => {
await BaseAPI.readOptions();
expect(mockHttp.options).toHaveBeenCalledTimes(1);
expect(mockHttp.options.mock.calls[0][0]).toEqual(`${mockBaseURL}`);
done();
});
test('replace calls http method with expected data', async (done) => {
const resourceId = 1;
const data = { name: 'test ' };
await BaseAPI.replace(resourceId, data);
expect(mockHttp.put).toHaveBeenCalledTimes(1);
expect(mockHttp.put.mock.calls[0][0]).toEqual(`${mockBaseURL}${resourceId}/`);
expect(mockHttp.put.mock.calls[0][1]).toEqual(data);
done();
});
test('update calls http method with expected data', async (done) => {
const resourceId = 1;
const data = { name: 'test ' };
await BaseAPI.update(resourceId, data);
expect(mockHttp.patch).toHaveBeenCalledTimes(1);
expect(mockHttp.patch.mock.calls[0][0]).toEqual(`${mockBaseURL}${resourceId}/`);
expect(mockHttp.patch.mock.calls[0][1]).toEqual(data);
done();
});
});

View File

@@ -0,0 +1,36 @@
import Organizations from '../../src/api/models/Organizations';
describe('OrganizationsAPI', () => {
const orgId = 1;
const searchParams = { foo: 'bar' };
const createPromise = () => Promise.resolve();
const mockHttp = ({ get: jest.fn(createPromise) });
const OrganizationsAPI = new Organizations(mockHttp);
afterEach(() => {
jest.clearAllMocks();
});
test('read access list calls get with expected params', async (done) => {
await OrganizationsAPI.readAccessList(orgId);
await OrganizationsAPI.readAccessList(orgId, searchParams);
expect(mockHttp.get).toHaveBeenCalledTimes(2);
expect(mockHttp.get.mock.calls[0]).toContainEqual(`/api/v2/organizations/${orgId}/access_list/`, { params: {} });
expect(mockHttp.get.mock.calls[1]).toContainEqual(`/api/v2/organizations/${orgId}/access_list/`, { params: searchParams });
done();
});
test('read teams calls get with expected params', async (done) => {
await OrganizationsAPI.readTeams(orgId);
await OrganizationsAPI.readTeams(orgId, searchParams);
expect(mockHttp.get).toHaveBeenCalledTimes(2);
expect(mockHttp.get.mock.calls[0]).toContainEqual(`/api/v2/organizations/${orgId}/teams/`, { params: {} });
expect(mockHttp.get.mock.calls[1]).toContainEqual(`/api/v2/organizations/${orgId}/teams/`, { params: searchParams });
done();
});
});

View File

@@ -0,0 +1,45 @@
import Root from '../../src/api/models/Root';
describe('RootAPI', () => {
const createPromise = () => Promise.resolve();
const mockHttp = ({ get: jest.fn(createPromise), post: jest.fn(createPromise) });
const RootAPI = new Root(mockHttp);
afterEach(() => {
jest.clearAllMocks();
});
test('login calls get and post with expected content headers', async (done) => {
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
await RootAPI.login('username', 'password');
expect(mockHttp.get).toHaveBeenCalledTimes(1);
expect(mockHttp.get.mock.calls[0]).toContainEqual({ headers });
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0]).toContainEqual({ headers });
done();
});
test('login sends expected data', async (done) => {
await RootAPI.login('foo', 'bar');
await RootAPI.login('foo', 'bar', 'baz');
expect(mockHttp.post).toHaveBeenCalledTimes(2);
expect(mockHttp.post.mock.calls[0]).toContainEqual('username=foo&password=bar&next=%2Fapi%2Fv2%2Fconfig%2F');
expect(mockHttp.post.mock.calls[1]).toContainEqual('username=foo&password=bar&next=baz');
done();
});
test('logout calls expected http method', async (done) => {
await RootAPI.logout();
expect(mockHttp.get).toHaveBeenCalledTimes(1);
done();
});
});

View File

@@ -0,0 +1,35 @@
import Teams from '../../src/api/models/Teams';
describe('TeamsAPI', () => {
const teamId = 1;
const roleId = 7;
const createPromise = () => Promise.resolve();
const mockHttp = ({ post: jest.fn(createPromise) });
const TeamsAPI = new Teams(mockHttp);
afterEach(() => {
jest.clearAllMocks();
});
test('associate role calls post with expected params', async (done) => {
await TeamsAPI.associateRole(teamId, roleId);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0]).toContainEqual(`/api/v2/teams/${teamId}/roles/`, { id: roleId });
done();
});
test('read teams calls post with expected params', async (done) => {
await TeamsAPI.disassociateRole(teamId, roleId);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0]).toContainEqual(`/api/v2/teams/${teamId}/roles/`, {
id: roleId,
disassociate: true
});
done();
});
});

View File

@@ -0,0 +1,35 @@
import Users from '../../src/api/models/Users';
describe('UsersAPI', () => {
const userId = 1;
const roleId = 7;
const createPromise = () => Promise.resolve();
const mockHttp = ({ post: jest.fn(createPromise) });
const UsersAPI = new Users(mockHttp);
afterEach(() => {
jest.clearAllMocks();
});
test('associate role calls post with expected params', async (done) => {
await UsersAPI.associateRole(userId, roleId);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0]).toContainEqual(`/api/v2/users/${userId}/roles/`, { id: roleId });
done();
});
test('read users calls post with expected params', async (done) => {
await UsersAPI.disassociateRole(userId, roleId);
expect(mockHttp.post).toHaveBeenCalledTimes(1);
expect(mockHttp.post.mock.calls[0]).toContainEqual(`/api/v2/users/${userId}/roles/`, {
id: roleId,
disassociate: true
});
done();
});
});

View File

@@ -2,9 +2,12 @@ import React from 'react';
import { shallow } from 'enzyme';
import { mountWithContexts } from '../enzymeHelpers';
import AddResourceRole, { _AddResourceRole } from '../../src/components/AddRole/AddResourceRole';
import { TeamsAPI, UsersAPI } from '../../src/api';
jest.mock('../../src/api');
describe('<_AddResourceRole />', () => {
const readUsers = jest.fn().mockResolvedValue({
UsersAPI.read.mockResolvedValue({
data: {
count: 2,
results: [
@@ -13,10 +16,6 @@ describe('<_AddResourceRole />', () => {
]
}
});
const readTeams = jest.fn();
const createUserRole = jest.fn();
const createTeamRole = jest.fn();
const api = { readUsers, readTeams, createUserRole, createTeamRole };
const roles = {
admin_role: {
description: 'Can manage all aspects of the organization',
@@ -32,7 +31,6 @@ describe('<_AddResourceRole />', () => {
test('initially renders without crashing', () => {
shallow(
<_AddResourceRole
api={api}
onClose={() => {}}
onSave={() => {}}
roles={roles}
@@ -43,7 +41,6 @@ describe('<_AddResourceRole />', () => {
test('handleRoleCheckboxClick properly updates state', () => {
const wrapper = shallow(
<_AddResourceRole
api={api}
onClose={() => {}}
onSave={() => {}}
roles={roles}
@@ -79,7 +76,6 @@ describe('<_AddResourceRole />', () => {
test('handleResourceCheckboxClick properly updates state', () => {
const wrapper = shallow(
<_AddResourceRole
api={api}
onClose={() => {}}
onSave={() => {}}
roles={roles}
@@ -115,7 +111,7 @@ describe('<_AddResourceRole />', () => {
onClose={() => {}}
onSave={() => {}}
roles={roles}
/>, { context: { network: { api, handleHttpError: () => {} } } }
/>, { context: { network: { handleHttpError: () => {} } } }
).find('AddResourceRole');
const selectableCardWrapper = wrapper.find('SelectableCard');
expect(selectableCardWrapper.length).toBe(2);
@@ -126,35 +122,9 @@ describe('<_AddResourceRole />', () => {
expect(spy).toHaveBeenCalledWith('teams');
expect(wrapper.state('selectedResource')).toBe('teams');
});
test('readUsers and readTeams call out to corresponding api functions', () => {
const wrapper = shallow(
<_AddResourceRole
api={api}
onClose={() => {}}
onSave={() => {}}
roles={roles}
i18n={{ _: val => val.toString() }}
/>
);
wrapper.instance().readUsers({
foo: 'bar'
});
expect(readUsers).toHaveBeenCalledWith({
foo: 'bar',
is_superuser: false
});
wrapper.instance().readTeams({
foo: 'bar'
});
expect(readTeams).toHaveBeenCalledWith({
foo: 'bar'
});
});
test('handleResourceSelect clears out selected lists and sets selectedResource', () => {
const wrapper = shallow(
<_AddResourceRole
api={api}
onClose={() => {}}
onSave={() => {}}
roles={roles}
@@ -192,7 +162,6 @@ describe('<_AddResourceRole />', () => {
currentStepId: 1
});
});
test('handleWizardSave makes correct api calls, calls onSave when done', async () => {
const handleSave = jest.fn();
const wrapper = mountWithContexts(
@@ -200,7 +169,7 @@ describe('<_AddResourceRole />', () => {
onClose={() => {}}
onSave={handleSave}
roles={roles}
/>, { context: { network: { api, handleHttpError: () => {} } } }
/>, { context: { network: { handleHttpError: () => {} } } }
).find('AddResourceRole');
wrapper.setState({
selectedResource: 'users',
@@ -224,7 +193,7 @@ describe('<_AddResourceRole />', () => {
]
});
await wrapper.instance().handleWizardSave();
expect(createUserRole).toHaveBeenCalledTimes(2);
expect(UsersAPI.associateRole).toHaveBeenCalledTimes(2);
expect(handleSave).toHaveBeenCalled();
wrapper.setState({
selectedResource: 'teams',
@@ -248,7 +217,7 @@ describe('<_AddResourceRole />', () => {
]
});
await wrapper.instance().handleWizardSave();
expect(createTeamRole).toHaveBeenCalledTimes(2);
expect(TeamsAPI.associateRole).toHaveBeenCalledTimes(2);
expect(handleSave).toHaveBeenCalled();
});
});

View File

@@ -70,10 +70,6 @@ const defaultContexts = {
toJSON: () => '/router/',
},
network: {
api: {
getConfig: () => {},
toJSON: () => '/api/',
},
handleHttpError: () => {},
},
dialog: {}
@@ -146,7 +142,6 @@ export function mountWithContexts (node, options = {}) {
history: shape({}).isRequired,
}),
network: shape({
api: shape({}).isRequired,
handleHttpError: func.isRequired,
}),
dialog: shape({

View File

@@ -4,7 +4,6 @@ import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro';
import { mountWithContexts, waitForElement } from './enzymeHelpers';
import { Config } from '../src/contexts/Config';
import { withNetwork } from '../src/contexts/Network';
import { withRootDialog } from '../src/contexts/RootDialog';
describe('mountWithContexts', () => {
@@ -111,32 +110,6 @@ describe('mountWithContexts', () => {
});
});
describe('injected Network', () => {
it('should mount and render', () => {
const Foo = () => (
<div>test</div>
);
const Bar = withNetwork(Foo);
const wrapper = mountWithContexts(<Bar />);
expect(wrapper.find('Foo')).toMatchSnapshot();
});
it('should mount and render with stubbed api', () => {
const network = {
api: {
getFoo: jest.fn().mockReturnValue('foo value'),
},
};
const Foo = ({ api }) => (
<div>{api.getFoo()}</div>
);
const Bar = withNetwork(Foo);
const wrapper = mountWithContexts(<Bar />, { context: { network } });
expect(network.api.getFoo).toHaveBeenCalledTimes(1);
expect(wrapper.find('div').text()).toEqual('foo value');
});
});
describe('injected root dialog', () => {
it('should mount and render', () => {
const Foo = ({ title, setRootDialogMessage }) => (

View File

@@ -2,7 +2,9 @@ import React from 'react';
import { mountWithContexts } from '../enzymeHelpers';
import { asyncFlush } from '../../jest.setup';
import AWXLogin from '../../src/pages/Login';
import APIClient from '../../src/api';
import { RootAPI } from '../../src/api';
jest.mock('../../src/api');
describe('<Login />', () => {
let loginWrapper;
@@ -14,12 +16,8 @@ describe('<Login />', () => {
let submitButton;
let loginHeaderLogo;
const api = new APIClient({});
const mountLogin = () => {
loginWrapper = mountWithContexts(<AWXLogin />, { context: { network: {
api, handleHttpError: () => {}
} } });
loginWrapper = mountWithContexts(<AWXLogin />, { context: { network: {} } });
};
const findChildren = () => {
@@ -33,6 +31,7 @@ describe('<Login />', () => {
};
afterEach(() => {
jest.clearAllMocks();
loginWrapper.unmount();
});
@@ -98,32 +97,31 @@ describe('<Login />', () => {
expect(loginWrapper.find('.pf-c-form__helper-text.pf-m-error').length).toBe(0);
});
test('api.login not called when loading', () => {
api.login = jest.fn().mockImplementation(() => Promise.resolve({}));
test('login API not called when loading', () => {
mountLogin();
findChildren();
expect(awxLogin.state().isLoading).toBe(false);
awxLogin.setState({ isLoading: true });
submitButton.simulate('click');
expect(api.login).toHaveBeenCalledTimes(0);
expect(RootAPI.login).toHaveBeenCalledTimes(0);
});
test('submit calls api.login successfully', async () => {
api.login = jest.fn().mockImplementation(() => Promise.resolve({}));
test('submit calls login API successfully', async () => {
RootAPI.login = jest.fn().mockImplementation(() => Promise.resolve({}));
mountLogin();
findChildren();
expect(awxLogin.state().isLoading).toBe(false);
awxLogin.setState({ username: 'unamee', password: 'pwordd' });
submitButton.simulate('click');
expect(api.login).toHaveBeenCalledTimes(1);
expect(api.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(RootAPI.login).toHaveBeenCalledTimes(1);
expect(RootAPI.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(awxLogin.state().isLoading).toBe(true);
await asyncFlush();
expect(awxLogin.state().isLoading).toBe(false);
});
test('submit calls api.login handles 401 error', async () => {
api.login = jest.fn().mockImplementation(() => {
test('submit calls login API and handles 401 error', async () => {
RootAPI.login = jest.fn().mockImplementation(() => {
const err = new Error('401 error');
err.response = { status: 401, message: 'problem' };
return Promise.reject(err);
@@ -134,16 +132,16 @@ describe('<Login />', () => {
expect(awxLogin.state().isInputValid).toBe(true);
awxLogin.setState({ username: 'unamee', password: 'pwordd' });
submitButton.simulate('click');
expect(api.login).toHaveBeenCalledTimes(1);
expect(api.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(RootAPI.login).toHaveBeenCalledTimes(1);
expect(RootAPI.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(awxLogin.state().isLoading).toBe(true);
await asyncFlush();
expect(awxLogin.state().isInputValid).toBe(false);
expect(awxLogin.state().isLoading).toBe(false);
});
test('submit calls api.login handles non-401 error', async () => {
api.login = jest.fn().mockImplementation(() => {
test('submit calls login API and handles non-401 error', async () => {
RootAPI.login = jest.fn().mockImplementation(() => {
const err = new Error('500 error');
err.response = { status: 500, message: 'problem' };
return Promise.reject(err);
@@ -153,8 +151,8 @@ describe('<Login />', () => {
expect(awxLogin.state().isLoading).toBe(false);
awxLogin.setState({ username: 'unamee', password: 'pwordd' });
submitButton.simulate('click');
expect(api.login).toHaveBeenCalledTimes(1);
expect(api.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(RootAPI.login).toHaveBeenCalledTimes(1);
expect(RootAPI.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(awxLogin.state().isLoading).toBe(true);
await asyncFlush();
expect(awxLogin.state().isLoading).toBe(false);

View File

@@ -2,9 +2,12 @@ import React from 'react';
import { mountWithContexts } from '../../../enzymeHelpers';
import { sleep } from '../../../testUtils';
import OrganizationForm from '../../../../src/pages/Organizations/components/OrganizationForm';
import { OrganizationsAPI } from '../../../../src/api';
jest.mock('../../../../src/api');
describe('<OrganizationForm />', () => {
let network;
const network = {};
const mockData = {
id: 1,
@@ -16,24 +19,11 @@ describe('<OrganizationForm />', () => {
}
};
beforeEach(() => {
network = {};
});
afterEach(() => {
jest.clearAllMocks();
});
test('should request related instance groups from api', () => {
const mockInstanceGroups = [
{ name: 'One', id: 1 },
{ name: 'Two', id: 2 }
];
network.api = {
getOrganizationInstanceGroups: jest.fn(() => (
Promise.resolve({ data: { results: mockInstanceGroups } })
))
};
mountWithContexts(
(
<OrganizationForm
@@ -46,7 +36,7 @@ describe('<OrganizationForm />', () => {
}
);
expect(network.api.getOrganizationInstanceGroups).toHaveBeenCalledTimes(1);
expect(OrganizationsAPI.readInstanceGroups).toHaveBeenCalledTimes(1);
});
test('componentDidMount should set instanceGroups to state', async () => {
@@ -54,11 +44,11 @@ describe('<OrganizationForm />', () => {
{ name: 'One', id: 1 },
{ name: 'Two', id: 2 }
];
network.api = {
getOrganizationInstanceGroups: jest.fn(() => (
Promise.resolve({ data: { results: mockInstanceGroups } })
))
};
OrganizationsAPI.readInstanceGroups.mockReturnValue({
data: {
results: mockInstanceGroups
}
});
const wrapper = mountWithContexts(
(
<OrganizationForm
@@ -72,7 +62,7 @@ describe('<OrganizationForm />', () => {
);
await sleep(0);
expect(network.api.getOrganizationInstanceGroups).toHaveBeenCalled();
expect(OrganizationsAPI.readInstanceGroups).toHaveBeenCalled();
expect(wrapper.find('OrganizationForm').state().instanceGroups).toEqual(mockInstanceGroups);
});
@@ -165,20 +155,20 @@ describe('<OrganizationForm />', () => {
{ name: 'One', id: 1 },
{ name: 'Two', id: 2 }
];
network.api = {
getOrganizationInstanceGroups: jest.fn(() => (
Promise.resolve({ data: { results: mockInstanceGroups } })
))
};
OrganizationsAPI.readInstanceGroups.mockReturnValue({
data: {
results: mockInstanceGroups
}
});
const mockDataForm = {
name: 'Foo',
description: 'Bar',
custom_virtualenv: 'Fizz',
};
const handleSubmit = jest.fn();
network.api.updateOrganizationDetails = jest.fn().mockResolvedValue(1, mockDataForm);
network.api.associateInstanceGroup = jest.fn().mockResolvedValue('done');
network.api.disassociate = jest.fn().mockResolvedValue('done');
OrganizationsAPI.update.mockResolvedValue(1, mockDataForm);
OrganizationsAPI.associateInstanceGroup.mockResolvedValue('done');
OrganizationsAPI.disassociateInstanceGroup.mockResolvedValue('done');
const wrapper = mountWithContexts(
(
<OrganizationForm

View File

@@ -1,6 +1,11 @@
import React from 'react';
import { createMemoryHistory } from 'history';
import { mountWithContexts } from '../../../../enzymeHelpers';
import { sleep } from '../../../../testUtils';
import Organization from '../../../../../src/pages/Organizations/screens/Organization/Organization';
import { OrganizationsAPI } from '../../../../../src/api';
jest.mock('../../../../../src/api');
describe.only('<Organization />', () => {
const me = {
@@ -12,8 +17,41 @@ describe.only('<Organization />', () => {
mountWithContexts(<Organization me={me} />);
});
test('notifications tab shown/hidden based on permissions', () => {
const wrapper = mountWithContexts(<Organization me={me} />);
test('notifications tab shown/hidden based on permissions', async () => {
OrganizationsAPI.readDetail.mockResolvedValue({
data: {
id: 1,
name: 'foo'
}
});
OrganizationsAPI.read.mockResolvedValue({
data: {
results: []
}
});
const history = createMemoryHistory({
initialEntries: ['/organizations/1/details'],
});
const match = { path: '/organizations/:id', url: '/organizations/1' };
const wrapper = mountWithContexts(
<Organization
me={me}
setBreadcrumb={() => {}}
/>,
{
context: {
router: {
history,
route: {
location: history.location,
match
}
}
}
}
);
await sleep(0);
wrapper.update();
expect(wrapper.find('.pf-c-tabs__item').length).toBe(3);
expect(wrapper.find('.pf-c-tabs__button[children="Notifications"]').length).toBe(0);
wrapper.find('Organization').setState({

View File

@@ -3,8 +3,12 @@ import { mountWithContexts } from '../../../../enzymeHelpers';
import OrganizationAccess from '../../../../../src/pages/Organizations/screens/Organization/OrganizationAccess';
import { sleep } from '../../../../testUtils';
import { OrganizationsAPI, TeamsAPI, UsersAPI } from '../../../../../src/api';
jest.mock('../../../../../src/api');
describe('<OrganizationAccess />', () => {
let network;
const network = {};
const organization = {
id: 1,
name: 'Default',
@@ -60,15 +64,11 @@ describe('<OrganizationAccess />', () => {
};
beforeEach(() => {
network = {
api: {
getOrganizationAccessList: jest.fn()
.mockReturnValue(Promise.resolve({ data })),
disassociateTeamRole: jest.fn(),
disassociateUserRole: jest.fn(),
toJSON: () => '/api/',
},
};
OrganizationsAPI.readAccessList.mockReturnValue({ data });
});
afterEach(() => {
jest.clearAllMocks();
});
test('initially renders succesfully', () => {
@@ -86,7 +86,7 @@ describe('<OrganizationAccess />', () => {
);
await sleep(0);
wrapper.update();
expect(network.api.getOrganizationAccessList).toHaveBeenCalled();
expect(OrganizationsAPI.readAccessList).toHaveBeenCalled();
expect(wrapper.find('OrganizationAccess').state('isInitialized')).toBe(true);
expect(wrapper.find('PaginatedDataList').prop('items')).toEqual(data.results);
expect(wrapper.find('OrganizationAccessItem')).toHaveLength(2);
@@ -127,8 +127,8 @@ describe('<OrganizationAccess />', () => {
const component = wrapper.find('OrganizationAccess');
expect(component.state('roleToDelete')).toBeNull();
expect(component.state('roleToDeleteAccessRecord')).toBeNull();
expect(network.api.disassociateTeamRole).not.toHaveBeenCalled();
expect(network.api.disassociateUserRole).not.toHaveBeenCalled();
expect(TeamsAPI.disassociateRole).not.toHaveBeenCalled();
expect(UsersAPI.disassociateRole).not.toHaveBeenCalled();
});
it('should delete user role', async () => {
@@ -149,9 +149,9 @@ describe('<OrganizationAccess />', () => {
const component = wrapper.find('OrganizationAccess');
expect(component.state('roleToDelete')).toBeNull();
expect(component.state('roleToDeleteAccessRecord')).toBeNull();
expect(network.api.disassociateTeamRole).not.toHaveBeenCalled();
expect(network.api.disassociateUserRole).toHaveBeenCalledWith(1, 1);
expect(network.api.getOrganizationAccessList).toHaveBeenCalledTimes(2);
expect(TeamsAPI.disassociateRole).not.toHaveBeenCalled();
expect(UsersAPI.disassociateRole).toHaveBeenCalledWith(1, 1);
expect(OrganizationsAPI.readAccessList).toHaveBeenCalledTimes(2);
});
it('should delete team role', async () => {
@@ -172,8 +172,8 @@ describe('<OrganizationAccess />', () => {
const component = wrapper.find('OrganizationAccess');
expect(component.state('roleToDelete')).toBeNull();
expect(component.state('roleToDeleteAccessRecord')).toBeNull();
expect(network.api.disassociateTeamRole).toHaveBeenCalledWith(5, 3);
expect(network.api.disassociateUserRole).not.toHaveBeenCalled();
expect(network.api.getOrganizationAccessList).toHaveBeenCalledTimes(2);
expect(TeamsAPI.disassociateRole).toHaveBeenCalledWith(5, 3);
expect(UsersAPI.disassociateRole).not.toHaveBeenCalled();
expect(OrganizationsAPI.readAccessList).toHaveBeenCalledTimes(2);
});
});

View File

@@ -1,6 +1,9 @@
import React from 'react';
import { mountWithContexts } from '../../../../enzymeHelpers';
import OrganizationDetail from '../../../../../src/pages/Organizations/screens/Organization/OrganizationDetail';
import { OrganizationsAPI } from '../../../../../src/api';
jest.mock('../../../../../src/api');
describe('<OrganizationDetail />', () => {
const mockDetails = {
@@ -16,6 +19,10 @@ describe('<OrganizationDetail />', () => {
}
};
afterEach(() => {
jest.clearAllMocks();
});
test('initially renders succesfully', () => {
mountWithContexts(
<OrganizationDetail
@@ -25,16 +32,15 @@ describe('<OrganizationDetail />', () => {
});
test('should request instance groups from api', () => {
const getOrganizationInstanceGroups = jest.fn();
mountWithContexts(
<OrganizationDetail
organization={mockDetails}
/>, { context: {
network: { api: { getOrganizationInstanceGroups }, handleHttpError: () => {} }
network: { handleHttpError: () => {} }
} }
).find('OrganizationDetail');
expect(getOrganizationInstanceGroups).toHaveBeenCalledTimes(1);
expect(OrganizationsAPI.readInstanceGroups).toHaveBeenCalledTimes(1);
});
test('should handle setting instance groups to state', async () => {
@@ -42,18 +48,18 @@ describe('<OrganizationDetail />', () => {
{ name: 'One', id: 1 },
{ name: 'Two', id: 2 }
];
const getOrganizationInstanceGroups = jest.fn(() => (
Promise.resolve({ data: { results: mockInstanceGroups } })
));
OrganizationsAPI.readInstanceGroups.mockResolvedValue({
data: { results: mockInstanceGroups }
});
const wrapper = mountWithContexts(
<OrganizationDetail
organization={mockDetails}
/>, { context: {
network: { api: { getOrganizationInstanceGroups }, handleHttpError: () => {} }
network: { handleHttpError: () => {} }
} }
).find('OrganizationDetail');
await getOrganizationInstanceGroups();
await OrganizationsAPI.readInstanceGroups();
expect(wrapper.state().instanceGroups).toEqual(mockInstanceGroups);
});

View File

@@ -3,6 +3,10 @@ import { mountWithContexts } from '../../../../enzymeHelpers';
import OrganizationEdit from '../../../../../src/pages/Organizations/screens/Organization/OrganizationEdit';
import { OrganizationsAPI } from '../../../../../src/api';
jest.mock('../../../../../src/api');
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
describe('<OrganizationEdit />', () => {
@@ -44,10 +48,7 @@ describe('<OrganizationEdit />', () => {
};
wrapper.find('OrganizationForm').prop('handleSubmit')(updatedOrgData, [], []);
expect(api.updateOrganizationDetails).toHaveBeenCalledWith(
1,
updatedOrgData
);
expect(OrganizationsAPI.update).toHaveBeenCalledWith(1, updatedOrgData);
});
test('handleSubmit associates and disassociates instance groups', async () => {
@@ -68,18 +69,9 @@ describe('<OrganizationEdit />', () => {
wrapper.find('OrganizationForm').prop('handleSubmit')(updatedOrgData, [3, 4], [2]);
await sleep(1);
expect(api.associateInstanceGroup).toHaveBeenCalledWith(
'/api/v2/organizations/1/instance_groups',
3
);
expect(api.associateInstanceGroup).toHaveBeenCalledWith(
'/api/v2/organizations/1/instance_groups',
4
);
expect(api.disassociate).toHaveBeenCalledWith(
'/api/v2/organizations/1/instance_groups',
2
);
expect(OrganizationsAPI.associateInstanceGroup).toHaveBeenCalledWith(1, 3);
expect(OrganizationsAPI.associateInstanceGroup).toHaveBeenCalledWith(1, 4);
expect(OrganizationsAPI.disassociateInstanceGroup).toHaveBeenCalledWith(1, 2);
});
test('should navigate to organization detail when cancel is clicked', () => {

View File

@@ -2,10 +2,13 @@ import React from 'react';
import { mountWithContexts } from '../../../../enzymeHelpers';
import OrganizationNotifications from '../../../../../src/pages/Organizations/screens/Organization/OrganizationNotifications';
import { sleep } from '../../../../testUtils';
import { OrganizationsAPI } from '../../../../../src/api';
jest.mock('../../../../../src/api');
describe('<OrganizationNotifications />', () => {
let data;
let network;
const network = {};
beforeEach(() => {
data = {
@@ -22,23 +25,13 @@ describe('<OrganizationNotifications />', () => {
notification_type: 'email',
}]
};
network = {
api: {
getOrganizationNotifications: jest.fn()
.mockReturnValue(Promise.resolve({ data })),
getOrganizationNotificationSuccess: jest.fn()
.mockReturnValue(Promise.resolve({
data: { results: [{ id: 1 }] },
})),
getOrganizationNotificationError: jest.fn()
.mockReturnValue(Promise.resolve({
data: { results: [{ id: 2 }] },
})),
createOrganizationNotificationSuccess: jest.fn(),
createOrganizationNotificationError: jest.fn(),
toJSON: () => '/api/',
}
};
OrganizationsAPI.readNotificationTemplates.mockReturnValue({ data });
OrganizationsAPI.readNotificationTemplatesSuccess.mockReturnValue({
data: { results: [{ id: 1 }] },
});
OrganizationsAPI.readNotificationTemplatesError.mockReturnValue({
data: { results: [{ id: 2 }] },
});
});
afterEach(() => {
@@ -65,7 +58,7 @@ describe('<OrganizationNotifications />', () => {
await sleep(0);
wrapper.update();
expect(network.api.getOrganizationNotifications).toHaveBeenCalled();
expect(OrganizationsAPI.readNotificationTemplates).toHaveBeenCalled();
expect(wrapper.find('OrganizationNotifications').state('notifications'))
.toEqual(data.results);
const items = wrapper.find('NotificationListItem');
@@ -91,7 +84,7 @@ describe('<OrganizationNotifications />', () => {
).toEqual([1]);
const items = wrapper.find('NotificationListItem');
items.at(1).find('Switch').at(0).prop('onChange')();
expect(network.api.createOrganizationNotificationSuccess).toHaveBeenCalled();
expect(OrganizationsAPI.associateNotificationTemplatesSuccess).toHaveBeenCalled();
await sleep(0);
wrapper.update();
expect(
@@ -114,7 +107,7 @@ describe('<OrganizationNotifications />', () => {
).toEqual([2]);
const items = wrapper.find('NotificationListItem');
items.at(0).find('Switch').at(1).prop('onChange')();
expect(network.api.createOrganizationNotificationError).toHaveBeenCalled();
expect(OrganizationsAPI.associateNotificationTemplatesError).toHaveBeenCalled();
await sleep(0);
wrapper.update();
expect(
@@ -137,7 +130,7 @@ describe('<OrganizationNotifications />', () => {
).toEqual([1]);
const items = wrapper.find('NotificationListItem');
items.at(0).find('Switch').at(0).prop('onChange')();
expect(network.api.createOrganizationNotificationSuccess).toHaveBeenCalled();
expect(OrganizationsAPI.disassociateNotificationTemplatesSuccess).toHaveBeenCalled();
await sleep(0);
wrapper.update();
expect(
@@ -160,7 +153,7 @@ describe('<OrganizationNotifications />', () => {
).toEqual([2]);
const items = wrapper.find('NotificationListItem');
items.at(1).find('Switch').at(1).prop('onChange')();
expect(network.api.createOrganizationNotificationError).toHaveBeenCalled();
expect(OrganizationsAPI.disassociateNotificationTemplatesError).toHaveBeenCalled();
await sleep(0);
wrapper.update();
expect(

View File

@@ -3,6 +3,9 @@ import { shallow } from 'enzyme';
import { mountWithContexts } from '../../../../enzymeHelpers';
import { sleep } from '../../../../testUtils';
import OrganizationTeams, { _OrganizationTeams } from '../../../../../src/pages/Organizations/screens/Organization/OrganizationTeams';
import { OrganizationsAPI } from '../../../../../src/api';
jest.mock('../../../../../src/api');
const listData = {
data: {
@@ -17,6 +20,14 @@ const listData = {
}
};
beforeEach(() => {
OrganizationsAPI.readTeams.mockResolvedValue(listData);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('<OrganizationTeams />', () => {
test('renders succesfully', () => {
shallow(
@@ -24,25 +35,21 @@ describe('<OrganizationTeams />', () => {
id={1}
searchString=""
location={{ search: '', pathname: '/organizations/1/teams' }}
api={{
readOrganizationTeamsList: jest.fn(),
}}
handleHttpError={() => {}}
/>
);
});
test('should load teams on mount', () => {
const readOrganizationTeamsList = jest.fn(() => Promise.resolve(listData));
mountWithContexts(
<OrganizationTeams
id={1}
searchString=""
/>, { context: {
network: { api: { readOrganizationTeamsList }, handleHttpError: () => {} } }
network: {} }
}
).find('OrganizationTeams');
expect(readOrganizationTeamsList).toHaveBeenCalledWith(1, {
expect(OrganizationsAPI.readTeams).toHaveBeenCalledWith(1, {
page: 1,
page_size: 5,
order_by: 'name',
@@ -50,13 +57,12 @@ describe('<OrganizationTeams />', () => {
});
test('should pass fetched teams to PaginatedDatalist', async () => {
const readOrganizationTeamsList = jest.fn(() => Promise.resolve(listData));
const wrapper = mountWithContexts(
<OrganizationTeams
id={1}
searchString=""
/>, { context: {
network: { api: { readOrganizationTeamsList }, handleHttpError: () => {} } }
network: { handleHttpError: () => {} } }
}
);

View File

@@ -2,7 +2,6 @@
exports[`<OrganizationAccess /> initially renders succesfully 1`] = `
<OrganizationAccess
api={"/api/"}
handleHttpError={[Function]}
history={"/history/"}
i18n={"/i18n/"}

View File

@@ -8,7 +8,6 @@ exports[`<OrganizationNotifications /> initially renders succesfully 1`] = `
<Provider
value={
Object {
"api": "/api/",
"handleHttpError": [Function],
}
}
@@ -18,7 +17,6 @@ exports[`<OrganizationNotifications /> initially renders succesfully 1`] = `
value={"/config/"}
>
<Provider
api={"/api/"}
handleHttpError={[Function]}
i18n={"/i18n/"}
value={"/config/"}
@@ -28,14 +26,12 @@ exports[`<OrganizationNotifications /> initially renders succesfully 1`] = `
id={1}
>
<withRouter(OrganizationNotifications)
api={"/api/"}
canToggleNotifications={true}
handleHttpError={[Function]}
id={1}
>
<Route>
<OrganizationNotifications
api={"/api/"}
canToggleNotifications={true}
handleHttpError={[Function]}
history={"/history/"}

View File

@@ -3,23 +3,17 @@ import React from 'react';
import { mountWithContexts } from '../../../enzymeHelpers';
import OrganizationAdd from '../../../../src/pages/Organizations/screens/OrganizationAdd';
import { OrganizationsAPI } from '../../../../src/api';
jest.mock('../../../../src/api');
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
describe('<OrganizationAdd />', () => {
let api;
let networkProviderValue;
beforeEach(() => {
api = {
getInstanceGroups: jest.fn(),
createOrganization: jest.fn(),
associateInstanceGroup: jest.fn(),
disassociate: jest.fn(),
};
networkProviderValue = {
api,
handleHttpError: () => {}
};
});
@@ -34,7 +28,7 @@ describe('<OrganizationAdd />', () => {
custom_virtualenv: 'Buzz',
};
wrapper.find('OrganizationForm').prop('handleSubmit')(updatedOrgData, [], []);
expect(api.createOrganization).toHaveBeenCalledWith(updatedOrgData);
expect(OrganizationsAPI.create).toHaveBeenCalledWith(updatedOrgData);
});
test('should navigate to organizations list when cancel is clicked', () => {
@@ -70,7 +64,7 @@ describe('<OrganizationAdd />', () => {
description: 'new description',
custom_virtualenv: 'Buzz',
};
api.createOrganization.mockReturnValueOnce({
OrganizationsAPI.create.mockReturnValueOnce({
data: {
id: 5,
related: {
@@ -96,7 +90,7 @@ describe('<OrganizationAdd />', () => {
description: 'new description',
custom_virtualenv: 'Buzz',
};
api.createOrganization.mockReturnValueOnce({
OrganizationsAPI.create.mockReturnValueOnce({
data: {
id: 5,
related: {
@@ -107,8 +101,8 @@ describe('<OrganizationAdd />', () => {
});
wrapper.find('OrganizationForm').prop('handleSubmit')(orgData, [3], []);
await sleep(0);
expect(api.associateInstanceGroup)
.toHaveBeenCalledWith('/api/v2/organizations/5/instance_groups', 3);
expect(OrganizationsAPI.associateInstanceGroup)
.toHaveBeenCalledWith(5, 3);
});
test('AnsibleSelect component renders if there are virtual environments', () => {

View File

@@ -2,6 +2,9 @@ import React from 'react';
import { createMemoryHistory } from 'history';
import { mountWithContexts } from '../../../enzymeHelpers';
import OrganizationsList, { _OrganizationsList } from '../../../../src/pages/Organizations/screens/OrganizationsList';
import { OrganizationsAPI } from '../../../../src/api';
jest.mock('../../../../src/api');
const mockAPIOrgsList = {
data: {
@@ -124,7 +127,7 @@ describe('<OrganizationsList />', () => {
selected: mockAPIOrgsList.data.results
});
wrapper.find('ToolbarDeleteButton').prop('onDelete')();
expect(api.destroyOrganization).toHaveBeenCalledTimes(component.state('selected').length);
expect(OrganizationsAPI.destroy).toHaveBeenCalledTimes(component.state('selected').length);
});
test('call fetchOrganizations after org(s) have been deleted', () => {