22 * Test injectors
33 */
44
5- import * as React from 'react' ;
6-
75import { put } from 'redux-saga/effects' ;
8- import { shallow } from 'enzyme' ;
9-
6+ import renderer from 'react-test-renderer' ;
7+ import { render } from '@testing-library/react' ;
8+ import React from 'react' ;
9+ import { Provider } from 'react-redux' ;
1010
1111import configureStore from '../../configureStore' ;
12+ import injectSaga , { useInjectSaga } from '../injectSaga' ;
13+ import { getInjectors } from '../sagaInjectors' ;
14+
15+
1216import { createMemoryHistory } from 'history' ;
13- import { DAEMON } from '../constants' ;
1417
1518const memoryHistory = createMemoryHistory ( ) ;
1619
@@ -21,19 +24,17 @@ function* testSaga() {
2124 yield put ( { type : 'TEST' , payload : 'yup' } ) ;
2225}
2326
27+ jest . mock ( '../sagaInjectors' ) ;
2428describe ( 'injectSaga decorator' , ( ) => {
2529 let store ;
2630 let injectors ;
2731 let ComponentWithSaga ;
28- let injectSaga ;
2932
3033 beforeAll ( ( ) => {
31- jest . mock ( '../sagaInjectors' , ( ) => ( {
32- __esModule : true ,
33- default : jest . fn ( ) . mockImplementation ( ( ) => injectors ) ,
34- } ) ) ;
35-
36- injectSaga = require ( '../injectSaga' ) . default ;
34+ const mockedGetInjectors = ( getInjectors as unknown ) as jest . Mock <
35+ typeof getInjectors
36+ > ; // compiler doesn't know that it's mocked. So manually cast it.
37+ mockedGetInjectors . mockImplementation ( ( ) => injectors ) ;
3738 } ) ;
3839
3940 beforeEach ( ( ) => {
@@ -45,28 +46,35 @@ describe('injectSaga decorator', () => {
4546 ComponentWithSaga = injectSaga ( {
4647 key : 'test' ,
4748 saga : testSaga ,
48- mode : DAEMON ,
49+ mode : 'testMode' ,
4950 } ) ( Component ) ;
50- jest . unmock ( '../sagaInjectors' ) ;
5151 } ) ;
5252
5353 it ( 'should inject given saga, mode, and props' , ( ) => {
5454 const props = { test : 'test' } ;
55- shallow ( < ComponentWithSaga { ...props } /> , { context : { store : store } } ) ;
55+ renderer . create (
56+ // tslint:disable-next-line: jsx-wrap-multiline
57+ < Provider store = { store } >
58+ < ComponentWithSaga { ...props } />
59+ </ Provider > ,
60+ ) ;
5661
5762 expect ( injectors . injectSaga ) . toHaveBeenCalledTimes ( 1 ) ;
5863 expect ( injectors . injectSaga ) . toHaveBeenCalledWith (
5964 'test' ,
60- { saga : testSaga , mode : DAEMON } ,
65+ { saga : testSaga , mode : 'testMode' } ,
6166 props ,
6267 ) ;
6368 } ) ;
6469
6570 it ( 'should eject on unmount with a correct saga key' , ( ) => {
6671 const props = { test : 'test' } ;
67- const renderedComponent = shallow ( < ComponentWithSaga { ...props } /> , {
68- context : { store : store } ,
69- } ) ;
72+ const renderedComponent = renderer . create (
73+ // tslint:disable-next-line: jsx-wrap-multiline
74+ < Provider store = { store } >
75+ < ComponentWithSaga { ...props } />
76+ </ Provider > ,
77+ ) ;
7078 renderedComponent . unmount ( ) ;
7179
7280 expect ( injectors . ejectSaga ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -82,10 +90,73 @@ describe('injectSaga decorator', () => {
8290
8391 it ( 'should propagate props' , ( ) => {
8492 const props = { testProp : 'test' } ;
85- const renderedComponent = shallow ( < ComponentWithSaga { ...props } /> , {
86- context : { store : store } ,
93+ const renderedComponent = renderer . create (
94+ // tslint:disable-next-line: jsx-wrap-multiline
95+ < Provider store = { store } >
96+ < ComponentWithSaga { ...props } />
97+ </ Provider > ,
98+ ) ;
99+ const {
100+ props : { children } ,
101+ } = renderedComponent . getInstance ( ) ;
102+ expect ( children . props ) . toEqual ( props ) ;
103+ } ) ;
104+ } ) ;
105+
106+ describe ( 'useInjectSaga hook' , ( ) => {
107+ let store ;
108+ let injectors ;
109+ let ComponentWithSaga ;
110+
111+ beforeAll ( ( ) => {
112+ const mockedGetInjectors = ( getInjectors as unknown ) as jest . Mock <
113+ typeof getInjectors
114+ > ; // compiler doesn't know that it's mocked. So manually cast it.
115+ mockedGetInjectors . mockImplementation ( ( ) => injectors ) ; } ) ;
116+
117+ beforeEach ( ( ) => {
118+ store = configureStore ( { } , memoryHistory ) ;
119+ injectors = {
120+ injectSaga : jest . fn ( ) ,
121+ ejectSaga : jest . fn ( ) ,
122+ } ;
123+ ComponentWithSaga = ( ) => {
124+ useInjectSaga ( {
125+ key : 'test' ,
126+ saga : testSaga ,
127+ mode : 'testMode' ,
128+ } ) ;
129+ return null ;
130+ } ;
131+ } ) ;
132+
133+ it ( 'should inject given saga and mode' , ( ) => {
134+ const props = { test : 'test' } ;
135+ render (
136+ // tslint:disable-next-line: jsx-wrap-multiline
137+ < Provider store = { store } >
138+ < ComponentWithSaga { ...props } />
139+ </ Provider > ,
140+ ) ;
141+
142+ expect ( injectors . injectSaga ) . toHaveBeenCalledTimes ( 1 ) ;
143+ expect ( injectors . injectSaga ) . toHaveBeenCalledWith ( 'test' , {
144+ saga : testSaga ,
145+ mode : 'testMode' ,
87146 } ) ;
147+ } ) ;
88148
89- expect ( renderedComponent . prop ( 'testProp' ) ) . toBe ( 'test' ) ;
149+ it ( 'should eject on unmount with a correct saga key' , ( ) => {
150+ const props = { test : 'test' } ;
151+ const { unmount } = render (
152+ // tslint:disable-next-line: jsx-wrap-multiline
153+ < Provider store = { store } >
154+ < ComponentWithSaga { ...props } />
155+ </ Provider > ,
156+ ) ;
157+ unmount ( ) ;
158+
159+ expect ( injectors . ejectSaga ) . toHaveBeenCalledTimes ( 1 ) ;
160+ expect ( injectors . ejectSaga ) . toHaveBeenCalledWith ( 'test' ) ;
90161 } ) ;
91162} ) ;
0 commit comments