UNPKG

2.45 kBJavaScriptView Raw
1/* @flow */
2import Enzyme, { mount } from 'enzyme';
3import Adapter from 'enzyme-adapter-react-16';
4import React from 'react';
5import NotificationContainer from '../components/notificationContainer';
6import { createStore, combineReducers, applyMiddleware } from 'redux';
7import thunk from 'redux-thunk';
8import { Provider } from 'react-redux';
9import { showNotification, removeNotification } from '../actions/notificationActions';
10
11import notification from '../reducers/notificationReducer';
12
13Enzyme.configure({ adapter: new Adapter() });
14
15const cancelNotification = {
16 dismissable: true,
17 icon: 'CANCEL',
18 text: 'Cancel Notification',
19 timeout: 0,
20 callToAction: 'Do something',
21 onCallToAction: jest.fn(),
22};
23
24describe('Notifications', () => {
25 jest.useFakeTimers();
26 let store;
27 let dispatch;
28
29 function TestContainer(props) {
30 return (
31 <Provider store={store}>
32 <NotificationContainer />
33 </Provider>
34 );
35 }
36
37 beforeEach(() => {
38 store = createStore(
39 combineReducers({ notification }),
40 { notification: {} },
41 applyMiddleware(thunk)
42 );
43 dispatch = store.dispatch;
44 });
45
46 it('should render a wrapper', () => {
47 const wrapper = mount(<TestContainer />);
48 expect(wrapper.find('div')).toHaveLength(1);
49 });
50
51 it('should render a notification', () => {
52 const wrapper = mount(<TestContainer />);
53 expect(
54 wrapper
55 .find('div')
56 .first()
57 .children()
58 ).toHaveLength(0);
59 dispatch(showNotification(cancelNotification));
60 wrapper.update();
61 expect(
62 wrapper
63 .find('div')
64 .first()
65 .children()
66 ).toHaveLength(1);
67 });
68
69 it('should remove a notification', async () => {
70 const wrapper = mount(<TestContainer />);
71 const id = await dispatch(showNotification(cancelNotification));
72 wrapper.update();
73 expect(
74 wrapper
75 .find('div')
76 .first()
77 .children()
78 ).toHaveLength(1);
79 dispatch(removeNotification(id));
80 jest.runAllTimers();
81 wrapper.update();
82 expect(
83 wrapper
84 .find('div')
85 .first()
86 .children()
87 ).toHaveLength(0);
88 });
89});
90
\No newline at end of file