Testing Redux combined reducers

4.4k Views Asked by At

Say I have several reducer functions and I combine them all into one reducer using combineReducers(...), is there a way of testing what reducers the combined reducer actually contains?

For example, if I have this:

import { combineReducers } from 'redux'

const reducer1 = (state, action) => {...}
... (more reducers, etc)

const rootReducer = combineReducers({
    reducer1,
    reducer2,
    reducer3
})

export default rootReducer

Can I write a test with Mocha and Expect.js that will enable me to check if the rootReducer contains say reducer2? Is this even possible?

The way I currently have my project set up is that each reducer is in a separate file and is then imported into the file where the combineReducers(...) function is used to combine them all. I am testing all the individual reducers to check that they do what they should, but I also thought it would be a good idea to test the combined reducer to make sure that it contains all the other reducers that it should (in case I forget to add one for example).

Thanks

1

There are 1 best solutions below

4
On BEST ANSWER

You are testing the wrong thing IMO. You should trust that the combineReducers() function does what it should (it should be tested in Redux distrubution tests). But you can create a method that will return the object with reducers to combine to pass as the parameter to combineReducers(). That method can and should be tested.