1
1
/*eslint-disable react/prop-types*/
2
2
3
- import React from 'react'
3
+ import React , { useReducer } from 'react'
4
4
import { createStore } from 'redux'
5
5
import * as rtl from 'react-testing-library'
6
6
import { Provider as ProviderMock , useSelector } from '../../src/index.js'
@@ -165,15 +165,40 @@ describe('React', () => {
165
165
166
166
expect ( renderedItems . length ) . toBe ( 1 )
167
167
} )
168
+
169
+ it ( 're-uses the selector if deps do not change' , ( ) => {
170
+ let selectorId = 0
171
+ let forceRender
172
+
173
+ const Comp = ( ) => {
174
+ const [ , f ] = useReducer ( c => c + 1 , 0 )
175
+ forceRender = f
176
+ const renderedSelectorId = selectorId ++
177
+ const value = useSelector ( ( ) => renderedSelectorId , [ ] )
178
+ renderedItems . push ( value )
179
+ return < div />
180
+ }
181
+
182
+ rtl . render (
183
+ < ProviderMock store = { store } >
184
+ < Comp />
185
+ </ ProviderMock >
186
+ )
187
+
188
+ rtl . act ( forceRender )
189
+
190
+ // this line verifies the susbcription callback uses the same memoized selector and therefore
191
+ // does not cause a re-render
192
+ store . dispatch ( { type : '' } )
193
+
194
+ expect ( renderedItems ) . toEqual ( [ 0 , 0 ] )
195
+ } )
168
196
} )
169
197
170
198
describe ( 'edge cases' , ( ) => {
171
199
it ( 'ignores transient errors in selector (e.g. due to stale props)' , ( ) => {
172
- // TODO Not sure this test is really testing what we want.
173
- // TODO The parent re-renders, which causes the child to re-run the selector anyway and throw the error.
174
- // TODO Had to flip the assertion for now. Probably needs to be rethought.
175
-
176
200
const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
201
+
177
202
const Parent = ( ) => {
178
203
const count = useSelector ( s => s . count )
179
204
return < Child parentCount = { count } />
@@ -197,9 +222,7 @@ describe('React', () => {
197
222
</ ProviderMock >
198
223
)
199
224
200
- expect ( ( ) => store . dispatch ( { type : '' } ) ) . toThrowError (
201
- / w h i l e s e l e c t i n g t h e s t o r e s t a t e /
202
- )
225
+ expect ( ( ) => store . dispatch ( { type : '' } ) ) . not . toThrowError ( )
203
226
204
227
spy . mockRestore ( )
205
228
} )
0 commit comments