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

Dylan Parry
  • 3,373
  • 6
  • 26
  • 38
  • 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 https://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... – dandavis Mar 04 '16 at 16:28
  • 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. – Dylan Parry Mar 04 '16 at 16:45

1 Answers1

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.

Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82
  • 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). – Dylan Parry Mar 06 '16 at 19:25
  • 1
    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. – Tomáš Fejfar Mar 06 '16 at 19:33
  • 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? – celsomtrindade Apr 11 '17 at 20:52
  • IMHO yes, it's redundant. – Tomáš Fejfar Apr 25 '17 at 13:36