Skip to content

Commit

Permalink
notification tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Jun 18, 2021
1 parent d5971ec commit c76724d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
11 changes: 4 additions & 7 deletions ui/src/app/notifications/component/NotificationItem.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import React from 'react';
import Alert from 'react-bootstrap/Alert';
import { NotificationContext, removeNotificationAction } from '../hoc/Notifications';

export function NotificationItem ({ type, body, timeout, id }) {

const { dispatch } = React.useContext(NotificationContext);
export function NotificationItem ({ type, body, timeout, id, onRemove }) {

React.useEffect(() => {
if (timeout) {
setTimeout(() => {
dispatch(removeNotificationAction(id));
onRemove(id)
}, timeout);
}
}, [timeout, id, dispatch]);
}, [timeout, id, onRemove]);

return (
<Alert variant={type} onClose={() => dispatch(removeNotificationAction(id))}>
<Alert variant={type} onClose={() => onRemove(id)}>
{body}
</Alert>
)
Expand Down
31 changes: 22 additions & 9 deletions ui/src/app/notifications/component/NotificationItem.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';

import { NotificationItem } from './NotificationItem';
import { NotificationContext, Notifications } from '../hoc/Notifications';

jest.mock('../../i18n/hooks', () => ({
useTranslation: (value) => value
Expand All @@ -12,26 +11,40 @@ describe('Notifcation Item', () => {
let context;
beforeEach(() => {
jest.useFakeTimers();

context = React.useContext(NotificationContext);
})

it('should change color based on type', () => {
render(<NotificationItem type="danger" body="foo" />, {wrapper: Notifications });
render(<NotificationItem type="danger" body="foo" />);
const el = screen.getByText('foo');
expect(el).toBeInTheDocument();
expect(el).toHaveClass('alert-danger')
});

it('should dispatch an event if provided a timeout', () => {

jest.spyOn(context, 'dispatch');
const mockOnRemove = jest.fn();

render(<NotificationItem type="danger" body="foo" timeout={5000} />, { wrapper: Notifications });
const el = screen.getByText('foo');
render(<NotificationItem type="danger" body="foo" timeout={5000} onRemove={mockOnRemove} />);

jest.runAllTimers();

expect(context.dispatch).toHaveBeenCalled();
expect(mockOnRemove).toHaveBeenCalled();
});

it('should be removed when clicked', () => {

const mockOnRemove = jest.fn();

render(<NotificationItem type="danger" body="foo" timeout={5000} onRemove={mockOnRemove} />);

const el = screen.getByText('Close alert');

fireEvent(el,
new MouseEvent('click', {
bubbles: true,
cancelable: true,
}));

expect(mockOnRemove).toHaveBeenCalled();
});
})
8 changes: 5 additions & 3 deletions ui/src/app/notifications/component/NotificationList.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import React from 'react';
import { NotificationContext } from '../hoc/Notifications';
import { NotificationContext, removeNotificationAction } from '../hoc/Notifications';
import { NotificationItem } from './NotificationItem';

export function NotificationList () {

const { state } = React.useContext(NotificationContext);
const { state, dispatch } = React.useContext(NotificationContext);

const onRemove = (id) => dispatch(removeNotificationAction(id));

return (
<ul className="notification-list list-unstyled position-fixed p-4 w-25">
{state.notifications.map((n) => (
<li key={n.id}>
<NotificationItem id={n.id} type={n.type} body={n.body} timeout={n.timeout} />
<NotificationItem id={n.id} type={n.type} body={n.body} timeout={n.timeout} onRemove={onRemove} />
</li>
))}
</ul>
Expand Down

0 comments on commit c76724d

Please sign in to comment.