11/*eslint-disable react/prop-types*/
22
3- import React from 'react'
3+ import React , { useReducer } from 'react'
44import { createStore } from 'redux'
55import * as rtl from 'react-testing-library'
66import { Provider as ProviderMock , useSelector } from '../../src/index.js'
@@ -165,15 +165,40 @@ describe('React', () => {
165165
166166 expect ( renderedItems . length ) . toBe ( 1 )
167167 } )
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+ } )
168196 } )
169197
170198 describe ( 'edge cases' , ( ) => {
171199 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-
176200 const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
201+
177202 const Parent = ( ) => {
178203 const count = useSelector ( s => s . count )
179204 return < Child parentCount = { count } />
@@ -197,9 +222,7 @@ describe('React', () => {
197222 </ ProviderMock >
198223 )
199224
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 ( )
203226
204227 spy . mockRestore ( )
205228 } )
0 commit comments