|
1 | | -import React from 'react'; |
2 | | -import { shallow, ShallowWrapper } from 'enzyme'; |
| 1 | +import * as React from 'react'; |
| 2 | +import * as ReactRedux from 'react-redux'; |
| 3 | +import { mount, ReactWrapper } from 'enzyme'; |
| 4 | +import PopupBase from '../PopupBase'; |
3 | 5 | import PopupReply, { Props } from '../PopupReply'; |
4 | 6 | import ReplyForm from '../../ReplyForm'; |
5 | 7 |
|
6 | | -jest.mock('../PopupBase', () => ({ |
7 | | - __esModule: true, |
8 | | - default: ({ children }: { children: React.ReactNode }): JSX.Element => <div>{children}</div>, |
| 8 | +jest.mock('react-redux', () => ({ |
| 9 | + useSelector: jest.fn(), |
9 | 10 | })); |
10 | 11 |
|
11 | | -type PropsIndex = { |
12 | | - [key: string]: Function | HTMLElement | boolean; |
13 | | -}; |
| 12 | +jest.mock('../PopupBase'); |
| 13 | +jest.mock('../../ReplyForm'); |
14 | 14 |
|
15 | 15 | describe('components/Popups/PopupReply', () => { |
16 | | - const defaults: Props & PropsIndex = { |
| 16 | + const defaults: Props = { |
17 | 17 | isPending: false, |
18 | 18 | onCancel: jest.fn(), |
19 | 19 | onChange: jest.fn(), |
20 | 20 | onSubmit: jest.fn(), |
21 | 21 | reference: document.createElement('div'), |
22 | 22 | }; |
23 | 23 |
|
24 | | - const getWrapper = (props = {}): ShallowWrapper => shallow(<PopupReply {...defaults} {...props} />); |
| 24 | + const getWrapper = (props = {}): ReactWrapper => mount(<PopupReply {...defaults} {...props} />); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + jest.spyOn(React, 'useEffect').mockImplementation(f => f()); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('state changes', () => { |
| 31 | + test('should call update on the underlying popper instance when the store changes', () => { |
| 32 | + const popupMock = { popper: { update: jest.fn() } }; |
| 33 | + const reduxSpy = jest.spyOn(ReactRedux, 'useSelector').mockImplementation(() => false); |
| 34 | + const refSpy = jest.spyOn(React, 'useRef').mockImplementation(() => ({ current: popupMock })); |
| 35 | + const wrapper = getWrapper(); |
| 36 | + |
| 37 | + reduxSpy.mockReturnValueOnce(true); |
| 38 | + wrapper.setProps({ value: '1' }); |
| 39 | + |
| 40 | + reduxSpy.mockReturnValueOnce(false); |
| 41 | + wrapper.setProps({ value: '2' }); |
| 42 | + |
| 43 | + expect(refSpy).toHaveBeenCalled(); |
| 44 | + expect(popupMock.popper.update).toHaveBeenCalledTimes(3); |
| 45 | + }); |
| 46 | + }); |
25 | 47 |
|
26 | 48 | describe('render()', () => { |
27 | 49 | test('should render the ReplyForm', () => { |
28 | 50 | const wrapper = getWrapper(); |
29 | 51 |
|
| 52 | + expect(wrapper.exists(PopupBase)).toBe(true); |
30 | 53 | expect(wrapper.exists(ReplyForm)).toBe(true); |
31 | 54 | }); |
32 | 55 | }); |
|
0 commit comments