UNPKG

2.94 kBJavaScriptView Raw
1/* @flow */
2import React from 'react';
3import { createStore, combineReducers, applyMiddleware } from 'redux';
4import thunk from 'redux-thunk';
5import { Provider } from 'react-redux';
6import Enzyme, { mount } from 'enzyme';
7import Adapter from 'enzyme-adapter-react-16';
8import ModalContainer from '../components/modalContainer';
9import { openConfirmationModal } from '../actions/modal';
10import modalReducer from '../reducers/modalReducer';
11
12Enzyme.configure({ adapter: new Adapter() });
13
14describe('Confirmation Modal', () => {
15 let store;
16 let dispatch;
17
18 beforeEach(() => {
19 // $FlowFixMe redux types suck.
20 store = createStore(
21 combineReducers({ modal: modalReducer }),
22 { modal: null },
23 applyMiddleware(thunk)
24 );
25 dispatch = store.dispatch;
26 });
27
28 function renderModal() {
29 return mount(
30 <Provider store={store}>
31 <ModalContainer />
32 </Provider>
33 );
34 }
35
36 it('should render a modal when openConfirmationModal is called', () => {
37 const modalWrapper = renderModal();
38 expect(modalWrapper.find('main')).toHaveLength(0);
39 dispatch(openConfirmationModal({ modalContent: <div />, element: null }));
40 modalWrapper.update();
41 expect(modalWrapper.find('main')).toHaveLength(1);
42 });
43
44 it('should close the modal and the promise should resolve when onConfirm is called', done => {
45 dispatch(openConfirmationModal({ modalContent: <div />, element: null })).then(done);
46 const modalWrapper = renderModal();
47 modalWrapper
48 .find('button')
49 .last()
50 .simulate('click');
51 modalWrapper.update();
52 expect(modalWrapper.find('main')).toHaveLength(0);
53 });
54
55 it('should return focus to the calling element when the modal is closed', done => {
56 const element = document.createElement('button');
57 const focusListener = jest.fn();
58 element.addEventListener('focus', focusListener);
59 dispatch(openConfirmationModal({ modalContent: <div />, element })).then(() => {
60 expect(focusListener).toHaveBeenCalled();
61 done();
62 });
63 const modalWrapper = renderModal();
64 expect(modalWrapper.find('main')).toHaveLength(1);
65 modalWrapper
66 .find('button')
67 .last()
68 .simulate('click');
69 modalWrapper.update();
70 });
71
72 it('should close the modal and the promise should reject when onCancel is called', done => {
73 dispatch(
74 openConfirmationModal({
75 modalContent: <div />,
76 element: null,
77 })
78 ).then(() => {}, done);
79 const modalWrapper = renderModal();
80 expect(modalWrapper.find('main')).toHaveLength(1);
81 modalWrapper
82 .find('button')
83 .first()
84 .simulate('click');
85 });
86});
87
\No newline at end of file