fix unit tests for network handling

This commit is contained in:
John Mitchell
2019-04-10 11:16:20 -04:00
parent ad0e409448
commit 344713f938
26 changed files with 499 additions and 344 deletions

View File

@@ -2,6 +2,7 @@ import React from 'react';
import { mount } from 'enzyme';
import { I18nProvider } from '@lingui/react';
import Lookup from '../../src/components/Lookup';
import { _Lookup } from '../../src/components/Lookup/Lookup';
let mockData = [{ name: 'foo', id: 1, isChecked: false }];
const mockColumns = [
@@ -11,7 +12,7 @@ describe('<Lookup />', () => {
test('initially renders succesfully', () => {
mount(
<I18nProvider>
<Lookup
<_Lookup
lookup_header="Foo Bar"
name="fooBar"
value={mockData}
@@ -19,14 +20,16 @@ describe('<Lookup />', () => {
getItems={() => { }}
columns={mockColumns}
sortedColumnKey="name"
handleHttpError={() => {}}
/>
</I18nProvider>
);
});
test('API response is formatted properly', (done) => {
const wrapper = mount(
<I18nProvider>
<Lookup
<_Lookup
lookup_header="Foo Bar"
name="fooBar"
value={mockData}
@@ -34,6 +37,7 @@ describe('<Lookup />', () => {
getItems={() => ({ data: { results: [{ name: 'test instance', id: 1 }] } })}
columns={mockColumns}
sortedColumnKey="name"
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Lookup');
@@ -43,12 +47,13 @@ describe('<Lookup />', () => {
done();
});
});
test('Opens modal when search icon is clicked', () => {
const spy = jest.spyOn(Lookup.prototype, 'handleModalToggle');
const spy = jest.spyOn(_Lookup.prototype, 'handleModalToggle');
const mockSelected = [{ name: 'foo', id: 1 }];
const wrapper = mount(
<I18nProvider>
<Lookup
<_Lookup
id="search"
lookup_header="Foo Bar"
name="fooBar"
@@ -57,6 +62,7 @@ describe('<Lookup />', () => {
getItems={() => { }}
columns={mockColumns}
sortedColumnKey="name"
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Lookup');
@@ -71,12 +77,13 @@ describe('<Lookup />', () => {
}]);
expect(wrapper.state('isModalOpen')).toEqual(true);
});
test('calls "toggleSelected" when a user changes a checkbox', (done) => {
const spy = jest.spyOn(Lookup.prototype, 'toggleSelected');
const spy = jest.spyOn(_Lookup.prototype, 'toggleSelected');
const mockSelected = [{ name: 'foo', id: 1 }];
const wrapper = mount(
<I18nProvider>
<Lookup
<_Lookup
id="search"
lookup_header="Foo Bar"
name="fooBar"
@@ -85,6 +92,7 @@ describe('<Lookup />', () => {
getItems={() => ({ data: { results: [{ name: 'test instance', id: 1 }] } })}
columns={mockColumns}
sortedColumnKey="name"
handleHttpError={() => {}}
/>
</I18nProvider>
);
@@ -96,12 +104,13 @@ describe('<Lookup />', () => {
done();
});
});
test('calls "toggleSelected" when remove icon is clicked', () => {
const spy = jest.spyOn(Lookup.prototype, 'toggleSelected');
const spy = jest.spyOn(_Lookup.prototype, 'toggleSelected');
mockData = [{ name: 'foo', id: 1 }, { name: 'bar', id: 2 }];
const wrapper = mount(
<I18nProvider>
<Lookup
<_Lookup
id="search"
lookup_header="Foo Bar"
name="fooBar"
@@ -110,6 +119,7 @@ describe('<Lookup />', () => {
getItems={() => { }}
columns={mockColumns}
sortedColumnKey="name"
handleHttpError={() => {}}
/>
</I18nProvider>
);
@@ -117,6 +127,7 @@ describe('<Lookup />', () => {
removeIcon.simulate('click');
expect(spy).toHaveBeenCalled();
});
test('renders chips from prop value', () => {
mockData = [{ name: 'foo', id: 0 }, { name: 'bar', id: 1 }];
const wrapper = mount(
@@ -129,12 +140,14 @@ describe('<Lookup />', () => {
getItems={() => { }}
columns={mockColumns}
sortedColumnKey="name"
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Lookup');
const chip = wrapper.find('li.pf-c-chip');
expect(chip).toHaveLength(2);
});
test('toggleSelected successfully adds/removes row from lookupSelectedItems state', () => {
mockData = [];
const wrapper = mount(
@@ -146,6 +159,7 @@ describe('<Lookup />', () => {
getItems={() => { }}
columns={mockColumns}
sortedColumnKey="name"
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Lookup');
@@ -163,6 +177,7 @@ describe('<Lookup />', () => {
});
expect(wrapper.state('lookupSelectedItems')).toEqual([]);
});
test('saveModal calls callback with selected items', () => {
mockData = [];
const onLookupSaveFn = jest.fn();
@@ -174,6 +189,7 @@ describe('<Lookup />', () => {
value={mockData}
onLookupSave={onLookupSaveFn}
getItems={() => { }}
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Lookup');
@@ -191,11 +207,12 @@ describe('<Lookup />', () => {
name: 'foo'
}], 'fooBar');
});
test('onSort sets state and calls getData ', () => {
const spy = jest.spyOn(Lookup.prototype, 'getData');
const spy = jest.spyOn(_Lookup.prototype, 'getData');
const wrapper = mount(
<I18nProvider>
<Lookup
<_Lookup
lookup_header="Foo Bar"
onLookupSave={() => { }}
value={mockData}
@@ -203,6 +220,7 @@ describe('<Lookup />', () => {
columns={mockColumns}
sortedColumnKey="name"
getItems={() => { }}
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Lookup');
@@ -211,11 +229,12 @@ describe('<Lookup />', () => {
expect(wrapper.state('sortOrder')).toEqual('descending');
expect(spy).toHaveBeenCalled();
});
test('onSetPage sets state and calls getData ', () => {
const spy = jest.spyOn(Lookup.prototype, 'getData');
test('onSearch calls getData (through calling onSort)', () => {
const spy = jest.spyOn(_Lookup.prototype, 'getData');
const wrapper = mount(
<I18nProvider>
<Lookup
<_Lookup
lookup_header="Foo Bar"
onLookupSave={() => { }}
value={mockData}
@@ -223,6 +242,27 @@ describe('<Lookup />', () => {
columns={mockColumns}
sortedColumnKey="name"
getItems={() => { }}
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Lookup');
wrapper.instance().onSearch();
expect(spy).toHaveBeenCalled();
});
test('onSetPage sets state and calls getData ', () => {
const spy = jest.spyOn(_Lookup.prototype, 'getData');
const wrapper = mount(
<I18nProvider>
<_Lookup
lookup_header="Foo Bar"
onLookupSave={() => { }}
value={mockData}
selected={[]}
columns={mockColumns}
sortedColumnKey="name"
getItems={() => { }}
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Lookup');

View File

@@ -2,7 +2,7 @@ import React from 'react';
import { mount } from 'enzyme';
import { MemoryRouter } from 'react-router-dom';
import { I18nProvider } from '@lingui/react';
import Notifications from '../../src/components/NotificationsList/Notifications.list';
import Notifications, { _Notifications } from '../../src/components/NotificationsList/Notifications.list';
describe('<Notifications />', () => {
test('initially renders succesfully', () => {
@@ -17,50 +17,52 @@ describe('<Notifications />', () => {
onReadSuccess={jest.fn()}
onCreateError={jest.fn()}
onCreateSuccess={jest.fn()}
handleHttpError={() => {}}
/>
</I18nProvider>
</MemoryRouter>
);
});
test('fetches notifications on mount', () => {
const spy = jest.spyOn(Notifications.prototype, 'readNotifications');
const spy = jest.spyOn(_Notifications.prototype, 'readNotifications');
mount(
<MemoryRouter>
<I18nProvider>
<Notifications
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
onReadError={jest.fn()}
onReadNotifications={jest.fn()}
onReadSuccess={jest.fn()}
onCreateError={jest.fn()}
onCreateSuccess={jest.fn()}
/>
</I18nProvider>
</MemoryRouter>
<I18nProvider>
<_Notifications
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
onReadError={jest.fn()}
onReadNotifications={jest.fn()}
onReadSuccess={jest.fn()}
onCreateError={jest.fn()}
onCreateSuccess={jest.fn()}
handleHttpError={() => {}}
/>
</I18nProvider>
);
expect(spy).toHaveBeenCalled();
});
test('toggle success calls post', () => {
const spy = jest.spyOn(Notifications.prototype, 'createSuccess');
const spy = jest.spyOn(_Notifications.prototype, 'createSuccess');
const wrapper = mount(
<MemoryRouter>
<I18nProvider>
<Notifications
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
onReadError={jest.fn()}
onReadNotifications={jest.fn()}
onReadSuccess={jest.fn()}
onCreateError={jest.fn()}
onCreateSuccess={jest.fn()}
/>
</I18nProvider>
</MemoryRouter>
<I18nProvider>
<_Notifications
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
onReadError={jest.fn()}
onReadNotifications={jest.fn()}
onReadSuccess={jest.fn()}
onCreateError={jest.fn()}
onCreateSuccess={jest.fn()}
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Notifications');
wrapper.instance().toggleNotification(1, true, 'success');
expect(spy).toHaveBeenCalledWith(1, true);
});
test('post success makes request and updates state properly', async () => {
const createSuccess = jest.fn();
const wrapper = mount(
@@ -74,6 +76,7 @@ describe('<Notifications />', () => {
onReadSuccess={jest.fn()}
onCreateError={jest.fn()}
onCreateSuccess={createSuccess}
handleHttpError={() => {}}
/>
</I18nProvider>
</MemoryRouter>
@@ -86,26 +89,27 @@ describe('<Notifications />', () => {
expect(createSuccess).toHaveBeenCalledWith(1, { id: 44 });
expect(wrapper.state('successTemplateIds')).toContain(44);
});
test('toggle error calls post', () => {
const spy = jest.spyOn(Notifications.prototype, 'createError');
const spy = jest.spyOn(_Notifications.prototype, 'createError');
const wrapper = mount(
<MemoryRouter>
<I18nProvider>
<Notifications
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
onReadError={jest.fn()}
onReadNotifications={jest.fn()}
onReadSuccess={jest.fn()}
onCreateError={jest.fn()}
onCreateSuccess={jest.fn()}
/>
</I18nProvider>
</MemoryRouter>
<I18nProvider>
<_Notifications
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
onReadError={jest.fn()}
onReadNotifications={jest.fn()}
onReadSuccess={jest.fn()}
onCreateError={jest.fn()}
onCreateSuccess={jest.fn()}
handleHttpError={() => {}}
/>
</I18nProvider>
).find('Notifications');
wrapper.instance().toggleNotification(1, true, 'error');
expect(spy).toHaveBeenCalledWith(1, true);
});
test('post error makes request and updates state properly', async () => {
const createError = jest.fn();
const wrapper = mount(
@@ -119,6 +123,7 @@ describe('<Notifications />', () => {
onReadSuccess={jest.fn()}
onCreateError={createError}
onCreateSuccess={jest.fn()}
handleHttpError={() => {}}
/>
</I18nProvider>
</MemoryRouter>
@@ -131,6 +136,7 @@ describe('<Notifications />', () => {
expect(createError).toHaveBeenCalledWith(1, { id: 44 });
expect(wrapper.state('errorTemplateIds')).toContain(44);
});
test('fetchNotifications', async () => {
const mockQueryParams = {
page: 44,
@@ -171,6 +177,7 @@ describe('<Notifications />', () => {
onReadError={readError}
onCreateError={jest.fn()}
onCreateSuccess={jest.fn()}
handleHttpError={() => {}}
/>
</I18nProvider>
</MemoryRouter>

View File

@@ -0,0 +1,25 @@
import React from 'react';
import { mount } from 'enzyme';
import { MemoryRouter } from 'react-router-dom';
import { I18nProvider } from '@lingui/react';
import { _NotifyAndRedirect } from '../../src/components/NotifyAndRedirect';
describe('<NotifyAndRedirect />', () => {
test('initially renders succesfully and calls setRootDialogMessage', () => {
const setRootDialogMessage = jest.fn();
mount(
<MemoryRouter>
<I18nProvider>
<_NotifyAndRedirect
to="foo"
setRootDialogMessage={setRootDialogMessage}
location={{ pathname: 'foo' }}
/>
</I18nProvider>
</MemoryRouter>
);
expect(setRootDialogMessage).toHaveBeenCalled();
});
});