mirror of
https://github.com/ansible/awx.git
synced 2026-05-23 08:37:48 -02:30
Fix NotificationList tests
This commit is contained in:
@@ -1,139 +1,107 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { mount } from 'enzyme';
|
import { mountWithContexts } from '../enzymeHelpers';
|
||||||
import { MemoryRouter } from 'react-router-dom';
|
|
||||||
import { I18nProvider } from '@lingui/react';
|
|
||||||
import Notifications, { _Notifications } from '../../src/components/NotificationsList/Notifications.list';
|
import Notifications, { _Notifications } from '../../src/components/NotificationsList/Notifications.list';
|
||||||
|
|
||||||
describe('<Notifications />', () => {
|
describe('<Notifications />', () => {
|
||||||
test('initially renders succesfully', () => {
|
test('initially renders succesfully', () => {
|
||||||
mount(
|
mountWithContexts(
|
||||||
<MemoryRouter>
|
<Notifications
|
||||||
<I18nProvider>
|
onReadError={() => {}}
|
||||||
<Notifications
|
onReadNotifications={() => {}}
|
||||||
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
|
onReadSuccess={() => {}}
|
||||||
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
onCreateError={() => {}}
|
||||||
onReadError={jest.fn()}
|
onCreateSuccess={() => {}}
|
||||||
onReadNotifications={jest.fn()}
|
/>
|
||||||
onReadSuccess={jest.fn()}
|
|
||||||
onCreateError={jest.fn()}
|
|
||||||
onCreateSuccess={jest.fn()}
|
|
||||||
handleHttpError={() => {}}
|
|
||||||
/>
|
|
||||||
</I18nProvider>
|
|
||||||
</MemoryRouter>
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('fetches notifications on mount', () => {
|
test('fetches notifications on mount', () => {
|
||||||
const spy = jest.spyOn(_Notifications.prototype, 'readNotifications');
|
const spy = jest.spyOn(_Notifications.prototype, 'readNotifications');
|
||||||
mount(
|
mountWithContexts(
|
||||||
<I18nProvider>
|
<Notifications
|
||||||
<_Notifications
|
onReadError={() => {}}
|
||||||
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
|
onReadNotifications={() => {}}
|
||||||
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
onReadSuccess={() => {}}
|
||||||
onReadError={jest.fn()}
|
onCreateError={() => {}}
|
||||||
onReadNotifications={jest.fn()}
|
onCreateSuccess={() => {}}
|
||||||
onReadSuccess={jest.fn()}
|
/>
|
||||||
onCreateError={jest.fn()}
|
|
||||||
onCreateSuccess={jest.fn()}
|
|
||||||
handleHttpError={() => {}}
|
|
||||||
/>
|
|
||||||
</I18nProvider>
|
|
||||||
);
|
);
|
||||||
expect(spy).toHaveBeenCalled();
|
expect(spy).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('toggle success calls post', () => {
|
test('toggle success calls post', () => {
|
||||||
const spy = jest.spyOn(_Notifications.prototype, 'createSuccess');
|
const spy = jest.spyOn(_Notifications.prototype, 'createSuccess');
|
||||||
const wrapper = mount(
|
const wrapper = mountWithContexts(
|
||||||
<I18nProvider>
|
<Notifications
|
||||||
<_Notifications
|
onReadError={() => {}}
|
||||||
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
|
onReadNotifications={() => {}}
|
||||||
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
onReadSuccess={() => {}}
|
||||||
onReadError={jest.fn()}
|
onCreateError={() => {}}
|
||||||
onReadNotifications={jest.fn()}
|
onCreateSuccess={() => {}}
|
||||||
onReadSuccess={jest.fn()}
|
/>
|
||||||
onCreateError={jest.fn()}
|
|
||||||
onCreateSuccess={jest.fn()}
|
|
||||||
handleHttpError={() => {}}
|
|
||||||
/>
|
|
||||||
</I18nProvider>
|
|
||||||
).find('Notifications');
|
).find('Notifications');
|
||||||
wrapper.instance().toggleNotification(1, true, 'success');
|
wrapper.instance().toggleNotification(1, true, 'success');
|
||||||
expect(spy).toHaveBeenCalledWith(1, true);
|
expect(spy).toHaveBeenCalledWith(1, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('post success makes request and updates state properly', async () => {
|
test('post success makes request and updates state properly', async () => {
|
||||||
const createSuccess = jest.fn();
|
const onCreateSuccess = jest.fn();
|
||||||
const wrapper = mount(
|
const wrapper = mountWithContexts(
|
||||||
<MemoryRouter>
|
<_Notifications
|
||||||
<I18nProvider>
|
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications', params: { id: 1 } }}
|
||||||
<Notifications
|
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
||||||
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications', params: { id: 1 } }}
|
handleHttpError={() => {}}
|
||||||
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
onReadError={() => {}}
|
||||||
onReadError={jest.fn()}
|
onReadNotifications={() => {}}
|
||||||
onReadNotifications={jest.fn()}
|
onReadSuccess={() => {}}
|
||||||
onReadSuccess={jest.fn()}
|
onCreateError={() => {}}
|
||||||
onCreateError={jest.fn()}
|
onCreateSuccess={onCreateSuccess}
|
||||||
onCreateSuccess={createSuccess}
|
/>
|
||||||
handleHttpError={() => {}}
|
|
||||||
/>
|
|
||||||
</I18nProvider>
|
|
||||||
</MemoryRouter>
|
|
||||||
).find('Notifications');
|
).find('Notifications');
|
||||||
wrapper.setState({ successTemplateIds: [44] });
|
wrapper.setState({ successTemplateIds: [44] });
|
||||||
await wrapper.instance().createSuccess(44, true);
|
await wrapper.instance().createSuccess(44, true);
|
||||||
expect(createSuccess).toHaveBeenCalledWith(1, { id: 44, disassociate: true });
|
expect(onCreateSuccess).toHaveBeenCalledWith(1, { id: 44, disassociate: true });
|
||||||
expect(wrapper.state('successTemplateIds')).not.toContain(44);
|
expect(wrapper.state('successTemplateIds')).not.toContain(44);
|
||||||
await wrapper.instance().createSuccess(44, false);
|
await wrapper.instance().createSuccess(44, false);
|
||||||
expect(createSuccess).toHaveBeenCalledWith(1, { id: 44 });
|
expect(onCreateSuccess).toHaveBeenCalledWith(1, { id: 44 });
|
||||||
expect(wrapper.state('successTemplateIds')).toContain(44);
|
expect(wrapper.state('successTemplateIds')).toContain(44);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('toggle error calls post', () => {
|
test('toggle error calls post', () => {
|
||||||
const spy = jest.spyOn(_Notifications.prototype, 'createError');
|
const spy = jest.spyOn(_Notifications.prototype, 'createError');
|
||||||
const wrapper = mount(
|
const wrapper = mountWithContexts(
|
||||||
<I18nProvider>
|
<Notifications
|
||||||
<_Notifications
|
onReadError={() => {}}
|
||||||
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications' }}
|
onReadNotifications={() => {}}
|
||||||
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
onReadSuccess={() => {}}
|
||||||
onReadError={jest.fn()}
|
onCreateError={() => {}}
|
||||||
onReadNotifications={jest.fn()}
|
onCreateSuccess={() => {}}
|
||||||
onReadSuccess={jest.fn()}
|
/>
|
||||||
onCreateError={jest.fn()}
|
|
||||||
onCreateSuccess={jest.fn()}
|
|
||||||
handleHttpError={() => {}}
|
|
||||||
/>
|
|
||||||
</I18nProvider>
|
|
||||||
).find('Notifications');
|
).find('Notifications');
|
||||||
wrapper.instance().toggleNotification(1, true, 'error');
|
wrapper.instance().toggleNotification(1, true, 'error');
|
||||||
expect(spy).toHaveBeenCalledWith(1, true);
|
expect(spy).toHaveBeenCalledWith(1, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('post error makes request and updates state properly', async () => {
|
test('post error makes request and updates state properly', async () => {
|
||||||
const createError = jest.fn();
|
const onCreateError = jest.fn();
|
||||||
const wrapper = mount(
|
const wrapper = mountWithContexts(
|
||||||
<MemoryRouter>
|
<_Notifications
|
||||||
<I18nProvider>
|
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications', params: { id: 1 } }}
|
||||||
<Notifications
|
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
||||||
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications', params: { id: 1 } }}
|
handleHttpError={() => {}}
|
||||||
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
onReadError={() => {}}
|
||||||
onReadError={jest.fn()}
|
onReadNotifications={() => {}}
|
||||||
onReadNotifications={jest.fn()}
|
onReadSuccess={() => {}}
|
||||||
onReadSuccess={jest.fn()}
|
onCreateError={onCreateError}
|
||||||
onCreateError={createError}
|
onCreateSuccess={() => {}}
|
||||||
onCreateSuccess={jest.fn()}
|
/>
|
||||||
handleHttpError={() => {}}
|
|
||||||
/>
|
|
||||||
</I18nProvider>
|
|
||||||
</MemoryRouter>
|
|
||||||
).find('Notifications');
|
).find('Notifications');
|
||||||
wrapper.setState({ errorTemplateIds: [44] });
|
wrapper.setState({ errorTemplateIds: [44] });
|
||||||
await wrapper.instance().createError(44, true);
|
await wrapper.instance().createError(44, true);
|
||||||
expect(createError).toHaveBeenCalledWith(1, { id: 44, disassociate: true });
|
expect(onCreateError).toHaveBeenCalledWith(1, { id: 44, disassociate: true });
|
||||||
expect(wrapper.state('errorTemplateIds')).not.toContain(44);
|
expect(wrapper.state('errorTemplateIds')).not.toContain(44);
|
||||||
await wrapper.instance().createError(44, false);
|
await wrapper.instance().createError(44, false);
|
||||||
expect(createError).toHaveBeenCalledWith(1, { id: 44 });
|
expect(onCreateError).toHaveBeenCalledWith(1, { id: 44 });
|
||||||
expect(wrapper.state('errorTemplateIds')).toContain(44);
|
expect(wrapper.state('errorTemplateIds')).toContain(44);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -159,28 +127,24 @@ describe('<Notifications />', () => {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const readError = jest.fn().mockResolvedValue({
|
const onReadError = jest.fn().mockResolvedValue({
|
||||||
data: {
|
data: {
|
||||||
results: [
|
results: [
|
||||||
{ id: 2 }
|
{ id: 2 }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const wrapper = mount(
|
const wrapper = mountWithContexts(
|
||||||
<MemoryRouter>
|
<_Notifications
|
||||||
<I18nProvider>
|
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications', params: { id: 1 } }}
|
||||||
<Notifications
|
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
||||||
match={{ path: '/organizations/:id/?tab=notifications', url: '/organizations/:id/?tab=notifications', params: { id: 1 } }}
|
handleHttpError={() => {}}
|
||||||
location={{ search: '', pathname: '/organizations/:id/?tab=notifications' }}
|
onReadError={onReadError}
|
||||||
onReadNotifications={onReadNotifications}
|
onReadNotifications={onReadNotifications}
|
||||||
onReadSuccess={onReadSuccess}
|
onReadSuccess={onReadSuccess}
|
||||||
onReadError={readError}
|
onCreateError={() => {}}
|
||||||
onCreateError={jest.fn()}
|
onCreateSuccess={() => {}}
|
||||||
onCreateSuccess={jest.fn()}
|
/>
|
||||||
handleHttpError={() => {}}
|
|
||||||
/>
|
|
||||||
</I18nProvider>
|
|
||||||
</MemoryRouter>
|
|
||||||
).find('Notifications');
|
).find('Notifications');
|
||||||
wrapper.instance().updateUrl = jest.fn();
|
wrapper.instance().updateUrl = jest.fn();
|
||||||
await wrapper.instance().readNotifications(mockQueryParams);
|
await wrapper.instance().readNotifications(mockQueryParams);
|
||||||
@@ -188,7 +152,7 @@ describe('<Notifications />', () => {
|
|||||||
expect(onReadSuccess).toHaveBeenCalledWith(1, {
|
expect(onReadSuccess).toHaveBeenCalledWith(1, {
|
||||||
id__in: '1,2,3'
|
id__in: '1,2,3'
|
||||||
});
|
});
|
||||||
expect(readError).toHaveBeenCalledWith(1, {
|
expect(onReadError).toHaveBeenCalledWith(1, {
|
||||||
id__in: '1,2,3'
|
id__in: '1,2,3'
|
||||||
});
|
});
|
||||||
expect(wrapper.state('successTemplateIds')).toContain(1);
|
expect(wrapper.state('successTemplateIds')).toContain(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user