Testing Redux combined reducers
Asked Answered
J

1

9

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

Judenberg answered 4/3, 2016 at 15:59 Comment(2)
does it matter more if it contains "reducer2" (whatever that would even mean) or does it work the same as reducer2? i think your test might be asking the wrong questions... also, looking at github.com/reactjs/redux/blob/master/src/combineReducers.js#L93, i don't see any ref back to the un-combined reducers (ether finalReducers or reducers), so "no" i think is your direct answer. all it returns is a function that loops and calls all the individuals...Loeb
Thanks. Your comment made me think about what the reducer actually does, and made me realise that it will produce a state with keys named after the reducers that were passed into the combineReducers function, so I could test the returned state and check that it contains the keys I expect it to. That way I'll know if I have passed in all the required reducers.Judenberg
R
7

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.

Resor answered 6/3, 2016 at 17:36 Comment(4)
Thanks. I think that's a similar approach to how I've ended up doing it, although I'm currently testing the state that the combined reducer generates. Your idea sounds like a better idea though, so I'll try that instead as my test could fail if Redux changed how it handled the state (unlikely, but you never know).Judenberg
Testing the resulting state will make the tests brittle as it will need to be changed every time any of the combined reducers or anything "downstream" changes.Venom
I'm starting learning now and I would like to know what to test on the combineReducers() function. I have my reducers splited in 2 parts. The first being the definition of the reducer and the other containing all the actions for a given reducers. Both already have tests. It seems redundant for me to just test the combineReducers, am i right?Portillo
IMHO yes, it's redundant.Venom

© 2022 - 2024 — McMap. All rights reserved.