update organization add test to use mountWithContexts

This commit is contained in:
John Mitchell
2019-04-18 13:54:41 -04:00
parent ce49cb9ba4
commit a6f79c646d

View File

@@ -1,12 +1,8 @@
import React from 'react'; import React from 'react';
import { mount } from 'enzyme';
import { MemoryRouter } from 'react-router-dom';
import { I18nProvider } from '@lingui/react';
import { ConfigProvider } from '../../../../src/contexts/Config'; import { mountWithContexts } from '../../../enzymeHelpers';
import { NetworkProvider } from '../../../../src/contexts/Network';
import { _OrganizationAdd } from '../../../../src/pages/Organizations/screens/OrganizationAdd'; import OrganizationAdd from '../../../../src/pages/Organizations/screens/OrganizationAdd';
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
@@ -29,25 +25,15 @@ describe('<OrganizationAdd />', () => {
}); });
test('handleSubmit should post to api', () => { test('handleSubmit should post to api', () => {
const wrapper = mount( const wrapper = mountWithContexts(<OrganizationAdd />, {
<MemoryRouter> context: { network: networkProviderValue }
<I18nProvider> });
<NetworkProvider value={networkProviderValue}>
<ConfigProvider>
<_OrganizationAdd api={api} />
</ConfigProvider>
</NetworkProvider>
</I18nProvider>
</MemoryRouter>
);
const updatedOrgData = { const updatedOrgData = {
name: 'new name', name: 'new name',
description: 'new description', description: 'new description',
custom_virtualenv: 'Buzz', custom_virtualenv: 'Buzz',
}; };
wrapper.find('OrganizationForm').prop('handleSubmit')(updatedOrgData, [], []); wrapper.find('OrganizationForm').prop('handleSubmit')(updatedOrgData, [], []);
expect(api.createOrganization).toHaveBeenCalledWith(updatedOrgData); expect(api.createOrganization).toHaveBeenCalledWith(updatedOrgData);
}); });
@@ -55,21 +41,11 @@ describe('<OrganizationAdd />', () => {
const history = { const history = {
push: jest.fn(), push: jest.fn(),
}; };
const wrapper = mount( const wrapper = mountWithContexts(<OrganizationAdd />, {
<MemoryRouter> context: { router: { history } }
<I18nProvider> });
<NetworkProvider value={networkProviderValue}>
<ConfigProvider>
<_OrganizationAdd api={api} history={history} />
</ConfigProvider>
</NetworkProvider>
</I18nProvider>
</MemoryRouter>
);
expect(history.push).not.toHaveBeenCalled(); expect(history.push).not.toHaveBeenCalled();
wrapper.find('button[aria-label="Cancel"]').prop('onClick')(); wrapper.find('button[aria-label="Cancel"]').prop('onClick')();
expect(history.push).toHaveBeenCalledWith('/organizations'); expect(history.push).toHaveBeenCalledWith('/organizations');
}); });
@@ -77,21 +53,11 @@ describe('<OrganizationAdd />', () => {
const history = { const history = {
push: jest.fn(), push: jest.fn(),
}; };
const wrapper = mount( const wrapper = mountWithContexts(<OrganizationAdd />, {
<MemoryRouter> context: { router: { history } }
<I18nProvider> });
<NetworkProvider value={networkProviderValue}>
<ConfigProvider>
<_OrganizationAdd api={api} history={history} />
</ConfigProvider>
</NetworkProvider>
</I18nProvider>
</MemoryRouter>
);
expect(history.push).not.toHaveBeenCalled(); expect(history.push).not.toHaveBeenCalled();
wrapper.find('button[aria-label="Close"]').prop('onClick')(); wrapper.find('button[aria-label="Close"]').prop('onClick')();
expect(history.push).toHaveBeenCalledWith('/organizations'); expect(history.push).toHaveBeenCalledWith('/organizations');
}); });
@@ -113,37 +79,18 @@ describe('<OrganizationAdd />', () => {
...orgData, ...orgData,
} }
}); });
const wrapper = mount( const wrapper = mountWithContexts(<OrganizationAdd />, {
<MemoryRouter> context: { router: { history }, network: networkProviderValue }
<I18nProvider> });
<NetworkProvider value={networkProviderValue}>
<ConfigProvider>
<_OrganizationAdd api={api} history={history} />
</ConfigProvider>
</NetworkProvider>
</I18nProvider>
</MemoryRouter>
);
wrapper.find('OrganizationForm').prop('handleSubmit')(orgData, [], []); wrapper.find('OrganizationForm').prop('handleSubmit')(orgData, [], []);
await sleep(0); await sleep(0);
expect(history.push).toHaveBeenCalledWith('/organizations/5'); expect(history.push).toHaveBeenCalledWith('/organizations/5');
}); });
test('handleSubmit should post instance groups', async () => { test('handleSubmit should post instance groups', async () => {
const wrapper = mount( const wrapper = mountWithContexts(<OrganizationAdd />, {
<MemoryRouter> context: { network: networkProviderValue }
<I18nProvider> });
<NetworkProvider value={networkProviderValue}>
<ConfigProvider>
<_OrganizationAdd api={api} />
</ConfigProvider>
</NetworkProvider>
</I18nProvider>
</MemoryRouter>
);
const orgData = { const orgData = {
name: 'new name', name: 'new name',
description: 'new description', description: 'new description',
@@ -160,7 +107,6 @@ describe('<OrganizationAdd />', () => {
}); });
wrapper.find('OrganizationForm').prop('handleSubmit')(orgData, [3], []); wrapper.find('OrganizationForm').prop('handleSubmit')(orgData, [3], []);
await sleep(0); await sleep(0);
expect(api.associateInstanceGroup) expect(api.associateInstanceGroup)
.toHaveBeenCalledWith('/api/v2/organizations/5/instance_groups', 3); .toHaveBeenCalledWith('/api/v2/organizations/5/instance_groups', 3);
}); });
@@ -169,17 +115,9 @@ describe('<OrganizationAdd />', () => {
const config = { const config = {
custom_virtualenvs: ['foo', 'bar'], custom_virtualenvs: ['foo', 'bar'],
}; };
const wrapper = mount( const wrapper = mountWithContexts(<OrganizationAdd />, {
<MemoryRouter> context: { network: networkProviderValue, config }
<I18nProvider> }).find('AnsibleSelect');
<NetworkProvider value={networkProviderValue}>
<ConfigProvider value={config}>
<_OrganizationAdd api={api} />
</ConfigProvider>
</NetworkProvider>
</I18nProvider>
</MemoryRouter>
).find('OrganizationAdd').find('AnsibleSelect');
expect(wrapper.find('FormSelect')).toHaveLength(1); expect(wrapper.find('FormSelect')).toHaveLength(1);
expect(wrapper.find('FormSelectOption')).toHaveLength(2); expect(wrapper.find('FormSelectOption')).toHaveLength(2);
}); });
@@ -188,17 +126,9 @@ describe('<OrganizationAdd />', () => {
const config = { const config = {
custom_virtualenvs: [], custom_virtualenvs: [],
}; };
const wrapper = mount( const wrapper = mountWithContexts(<OrganizationAdd />, {
<MemoryRouter> context: { network: networkProviderValue, config }
<I18nProvider> }).find('AnsibleSelect');
<NetworkProvider value={networkProviderValue}>
<ConfigProvider value={config}>
<_OrganizationAdd api={api} />
</ConfigProvider>
</NetworkProvider>
</I18nProvider>
</MemoryRouter>
).find('OrganizationAdd').find('AnsibleSelect');
expect(wrapper.find('FormSelect')).toHaveLength(0); expect(wrapper.find('FormSelect')).toHaveLength(0);
}); });
}); });