1. 9
    Unit Testing a Memoized Redux Selector Built with Reselect/RTK
    2m 42s

Unit Testing a Memoized Redux Selector Built with Reselect/RTK

InstructorJamund Ferguson

Share this video with your friends

Send Tweet

The reselect module popularized a pattern of memoized redux selectors. Now that pattern is baked into Redux Toolkit with its createSelector method, which is used by the getMemoizedNumItems selector we're testing here.

Any selectors created with createSelector come with these two additional methods recomputations() and resetRecomputations(). These methods keep track of how many teams your data was recomputed and are essential for testing these selectors.

Some additional examples on testing memoized selectors can be found here: https://github.com/reduxjs/reselect#q-how-do-i-test-a-selector


The createSelector method first takes any number of "input selectors" which gather data from your redux store and then for its final arguments takes an additional selector which has for its inputs the result of those input selectors.

The memoization is broken if either the initial state is unchanged OR if the any of the results of the input selectors are unchanged. It uses === equality and not any sort of nested equality checking so state and { ...state } would not be considered the same and always would recompute. On the flip side passing in state and then running state.count++ would not trigger a recompute automatically, because the state object reference has not changed and === would pass. It definitely tripped me up a few times, so keep an eye out for issues with this.

YUNUS
~ 2 years ago

This is the best testing course I ever watched Thanks