mirror of
https://github.com/ansible/awx.git
synced 2026-07-06 13:58:05 -02:30
fix unit tests for network handling
This commit is contained in:
@@ -3,7 +3,7 @@ import { mount } from 'enzyme';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { I18nProvider } from '@lingui/react';
|
||||
|
||||
import OrganizationAccessList from '../../../../src/pages/Organizations/components/OrganizationAccessList';
|
||||
import OrganizationAccessList, { _OrganizationAccessList } from '../../../../src/pages/Organizations/components/OrganizationAccessList';
|
||||
|
||||
const mockData = [
|
||||
{
|
||||
@@ -66,16 +66,17 @@ describe('<OrganizationAccessList />', () => {
|
||||
});
|
||||
|
||||
test('onExpand and onCompact methods called when user clicks on Expand and Compact icons respectively', async (done) => {
|
||||
const onExpand = jest.spyOn(OrganizationAccessList.prototype, 'onExpand');
|
||||
const onCompact = jest.spyOn(OrganizationAccessList.prototype, 'onCompact');
|
||||
const onExpand = jest.spyOn(_OrganizationAccessList.prototype, 'onExpand');
|
||||
const onCompact = jest.spyOn(_OrganizationAccessList.prototype, 'onCompact');
|
||||
const wrapper = mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter>
|
||||
<OrganizationAccessList
|
||||
<_OrganizationAccessList
|
||||
match={{ path: '/organizations/:id', url: '/organizations/1', params: { id: '0' } }}
|
||||
location={{ search: '', pathname: '/organizations/1/access' }}
|
||||
getAccessList={() => ({ data: { count: 1, results: mockData } })}
|
||||
removeRole={() => {}}
|
||||
handleHttpError={() => {}}
|
||||
/>
|
||||
</MemoryRouter>
|
||||
</I18nProvider>
|
||||
@@ -94,15 +95,16 @@ describe('<OrganizationAccessList />', () => {
|
||||
});
|
||||
|
||||
test('onSort being passed properly to DataListToolbar component', async (done) => {
|
||||
const onSort = jest.spyOn(OrganizationAccessList.prototype, 'onSort');
|
||||
const onSort = jest.spyOn(_OrganizationAccessList.prototype, 'onSort');
|
||||
const wrapper = mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter>
|
||||
<OrganizationAccessList
|
||||
<_OrganizationAccessList
|
||||
match={{ path: '/organizations/:id', url: '/organizations/1', params: { id: '0' } }}
|
||||
location={{ search: '', pathname: '/organizations/1/access' }}
|
||||
getAccessList={() => ({ data: { count: 1, results: mockData } })}
|
||||
removeRole={() => {}}
|
||||
handleHttpError={() => {}}
|
||||
/>
|
||||
</MemoryRouter>
|
||||
</I18nProvider>
|
||||
@@ -141,17 +143,18 @@ describe('<OrganizationAccessList />', () => {
|
||||
});
|
||||
|
||||
test('test handleWarning, confirmDelete, and removeRole methods for Alert component', (done) => {
|
||||
const handleWarning = jest.spyOn(OrganizationAccessList.prototype, 'handleWarning');
|
||||
const confirmDelete = jest.spyOn(OrganizationAccessList.prototype, 'confirmDelete');
|
||||
const removeRole = jest.spyOn(OrganizationAccessList.prototype, 'removeAccessRole');
|
||||
const handleWarning = jest.spyOn(_OrganizationAccessList.prototype, 'handleWarning');
|
||||
const confirmDelete = jest.spyOn(_OrganizationAccessList.prototype, 'confirmDelete');
|
||||
const removeRole = jest.spyOn(_OrganizationAccessList.prototype, 'removeAccessRole');
|
||||
const wrapper = mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter>
|
||||
<OrganizationAccessList
|
||||
<_OrganizationAccessList
|
||||
match={{ path: '/organizations/:id', url: '/organizations/1', params: { id: '0' } }}
|
||||
location={{ search: '', pathname: '/organizations/1/access' }}
|
||||
getAccessList={() => ({ data: { count: 1, results: mockData } })}
|
||||
removeRole={() => {}}
|
||||
handleHttpError={() => {}}
|
||||
/>
|
||||
</MemoryRouter>
|
||||
</I18nProvider>
|
||||
@@ -164,19 +167,19 @@ describe('<OrganizationAccessList />', () => {
|
||||
const rendered = wrapper.update().find('ChipButton');
|
||||
rendered.find('button[aria-label="close"]').simulate('click');
|
||||
expect(handleWarning).toHaveBeenCalled();
|
||||
const alert = wrapper.update().find('Alert');
|
||||
alert.find('button[aria-label="confirm-delete"]').simulate('click');
|
||||
const alertModal = wrapper.update().find('Modal');
|
||||
alertModal.find('button[aria-label="Confirm delete"]').simulate('click');
|
||||
expect(confirmDelete).toHaveBeenCalled();
|
||||
expect(removeRole).toHaveBeenCalled();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
test('state is set appropriately when a user tries deleting a role', (done) => {
|
||||
test.only('state is set appropriately when a user tries deleting a role', (done) => {
|
||||
const wrapper = mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter>
|
||||
<OrganizationAccessList
|
||||
<_OrganizationAccessList
|
||||
match={{ path: '/organizations/:id', url: '/organizations/1', params: { id: '0' } }}
|
||||
location={{ search: '', pathname: '/organizations/1/access' }}
|
||||
getAccessList={() => ({ data: { count: 1, results: mockData } })}
|
||||
@@ -200,8 +203,8 @@ describe('<OrganizationAccessList />', () => {
|
||||
];
|
||||
const rendered = wrapper.update().find('ChipButton');
|
||||
rendered.find('button[aria-label="close"]').simulate('click');
|
||||
const alert = wrapper.update().find('Alert');
|
||||
alert.find('button[aria-label="confirm-delete"]').simulate('click');
|
||||
const alertModal = wrapper.update().find('Modal');
|
||||
alertModal.find('button[aria-label="Confirm delete"]').simulate('click');
|
||||
expect(wrapper.state().warningTitle).not.toBe(null);
|
||||
expect(wrapper.state().warningMsg).not.toBe(null);
|
||||
expected.forEach(criteria => {
|
||||
|
||||
@@ -2,12 +2,21 @@ import React from 'react';
|
||||
import { mount } from 'enzyme';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { I18nProvider } from '@lingui/react';
|
||||
<<<<<<< HEAD
|
||||
import { ConfigContext } from '../../../../src/context';
|
||||
import OrganizationForm from '../../../../src/pages/Organizations/components/OrganizationForm';
|
||||
import { sleep } from '../../../testUtils';
|
||||
=======
|
||||
import { ConfigProvider } from '../../../../src/contexts/Config';
|
||||
import { NetworkProvider } from '../../../../src/contexts/Network';
|
||||
import OrganizationForm, { _OrganizationForm } from '../../../../src/pages/Organizations/components/OrganizationForm';
|
||||
|
||||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
>>>>>>> fix unit tests for network handling
|
||||
|
||||
describe('<OrganizationForm />', () => {
|
||||
let api;
|
||||
let networkProviderValue;
|
||||
|
||||
const mockData = {
|
||||
id: 1,
|
||||
@@ -23,6 +32,11 @@ describe('<OrganizationForm />', () => {
|
||||
api = {
|
||||
getInstanceGroups: jest.fn(),
|
||||
};
|
||||
|
||||
networkProviderValue = {
|
||||
api,
|
||||
handleHttpError: () => {}
|
||||
};
|
||||
});
|
||||
|
||||
test('should request related instance groups from api', () => {
|
||||
@@ -34,16 +48,18 @@ describe('<OrganizationForm />', () => {
|
||||
Promise.resolve({ data: { results: mockInstanceGroups } })
|
||||
));
|
||||
mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
|
||||
<OrganizationForm
|
||||
api={api}
|
||||
organization={mockData}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</MemoryRouter>
|
||||
</I18nProvider>
|
||||
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
|
||||
<I18nProvider>
|
||||
<NetworkProvider value={networkProviderValue}>
|
||||
<_OrganizationForm
|
||||
api={api}
|
||||
organization={mockData}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</NetworkProvider>
|
||||
</I18nProvider>
|
||||
</MemoryRouter>
|
||||
).find('OrganizationForm');
|
||||
|
||||
expect(api.getOrganizationInstanceGroups).toHaveBeenCalledTimes(1);
|
||||
@@ -58,16 +74,18 @@ describe('<OrganizationForm />', () => {
|
||||
Promise.resolve({ data: { results: mockInstanceGroups } })
|
||||
));
|
||||
const wrapper = mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</MemoryRouter>
|
||||
</I18nProvider>
|
||||
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
|
||||
<I18nProvider>
|
||||
<NetworkProvider value={networkProviderValue}>
|
||||
<_OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</NetworkProvider>
|
||||
</I18nProvider>
|
||||
</MemoryRouter>
|
||||
).find('OrganizationForm');
|
||||
|
||||
await wrapper.instance().componentDidMount();
|
||||
@@ -78,12 +96,13 @@ describe('<OrganizationForm />', () => {
|
||||
const wrapper = mount(
|
||||
<MemoryRouter>
|
||||
<I18nProvider>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
<NetworkProvider value={networkProviderValue}>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</NetworkProvider>
|
||||
</I18nProvider>
|
||||
</MemoryRouter>
|
||||
).find('OrganizationForm');
|
||||
@@ -109,12 +128,13 @@ describe('<OrganizationForm />', () => {
|
||||
const wrapper = mount(
|
||||
<MemoryRouter>
|
||||
<I18nProvider>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
<NetworkProvider value={networkProviderValue}>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</NetworkProvider>
|
||||
</I18nProvider>
|
||||
</MemoryRouter>
|
||||
).find('OrganizationForm');
|
||||
@@ -137,14 +157,15 @@ describe('<OrganizationForm />', () => {
|
||||
const wrapper = mount(
|
||||
<MemoryRouter>
|
||||
<I18nProvider>
|
||||
<ConfigContext.Provider value={config}>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</ConfigContext.Provider>
|
||||
<NetworkProvider value={networkProviderValue}>
|
||||
<ConfigProvider value={config}>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</ConfigProvider>
|
||||
</NetworkProvider>
|
||||
</I18nProvider>
|
||||
</MemoryRouter>
|
||||
);
|
||||
@@ -157,16 +178,18 @@ describe('<OrganizationForm />', () => {
|
||||
const wrapper = mount(
|
||||
<MemoryRouter>
|
||||
<I18nProvider>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={handleSubmit}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
<NetworkProvider value={networkProviderValue}>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={handleSubmit}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</NetworkProvider>
|
||||
</I18nProvider>
|
||||
</MemoryRouter>
|
||||
).find('OrganizationForm');
|
||||
expect(wrapper.prop('handleSubmit')).not.toHaveBeenCalled();
|
||||
expect(handleSubmit).not.toHaveBeenCalled();
|
||||
wrapper.find('button[aria-label="Save"]').simulate('click');
|
||||
await sleep(1);
|
||||
expect(handleSubmit).toHaveBeenCalledWith({
|
||||
@@ -196,12 +219,14 @@ describe('<OrganizationForm />', () => {
|
||||
const wrapper = mount(
|
||||
<I18nProvider>
|
||||
<MemoryRouter initialEntries={['/organizations/1']} initialIndex={0}>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={handleSubmit}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
<NetworkProvider value={networkProviderValue}>
|
||||
<_OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={handleSubmit}
|
||||
handleCancel={jest.fn()}
|
||||
/>
|
||||
</NetworkProvider>
|
||||
</MemoryRouter>
|
||||
</I18nProvider>
|
||||
).find('OrganizationForm');
|
||||
@@ -223,12 +248,13 @@ describe('<OrganizationForm />', () => {
|
||||
const wrapper = mount(
|
||||
<MemoryRouter>
|
||||
<I18nProvider>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
api={api}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={handleCancel}
|
||||
/>
|
||||
<NetworkProvider value={networkProviderValue}>
|
||||
<OrganizationForm
|
||||
organization={mockData}
|
||||
handleSubmit={jest.fn()}
|
||||
handleCancel={handleCancel}
|
||||
/>
|
||||
</NetworkProvider>
|
||||
</I18nProvider>
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user