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 c76724d commit 4caa9c1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
17 changes: 0 additions & 17 deletions ui/src/app/notifications/component/NotificationItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,4 @@ describe('Notifcation Item', () => {

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();
});
})
23 changes: 23 additions & 0 deletions ui/src/app/notifications/component/NotificationList.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { render, screen } from '@testing-library/react';

import { NotificationList } from './NotificationList';
import { NotificationContext } from '../hoc/Notifications';

jest.mock('../../i18n/hooks', () => ({
useTranslation: (value) => value
}));

describe('Notification List', () => {
it('should render notifications', () => {
const dispatch = jest.fn();
const state = { notifications: [{id: 'foo', body: 'foo', type: 'danger'}] };
render(
<NotificationContext.Provider value={{ state, dispatch }}>
<NotificationList />
</NotificationContext.Provider>
);

expect(screen.getByText('foo')).toBeInTheDocument();
});
})
2 changes: 1 addition & 1 deletion ui/src/app/notifications/hoc/Notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const removeNotificationAction = (id) => {
}
}

function reducer(state, action) {
export function reducer(state, action) {
switch (action.type) {
case NotificationActions.ADD_NOTIFICATION:
return {
Expand Down
24 changes: 24 additions & 0 deletions ui/src/app/notifications/hoc/Notifications.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createNotificationAction, reducer, removeNotificationAction } from './Notifications';

describe('Notifications HOC', () => {
describe('reducer', () => {
it('should add a notification to its state list', () => {
const state = reducer({
notifications: []
}, createNotificationAction('foo'));
expect(state.notifications.length).toBe(1);
})

it('should remove a notification to its state list', () => {
const state = reducer({
notifications: [
{
id: 'foo'
}
]
}, removeNotificationAction('foo'));

expect(state.notifications.length).toBe(0);
})
});
});

0 comments on commit 4caa9c1

Please sign in to comment.