admin管理员组

文章数量:1335132

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

For example, if I have this:

import { bineReducers } from 'redux'

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

const rootReducer = bineReducers({
    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 bineReducers(...) function is used to bine 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 bined reducer to make sure that it contains all the other reducers that it should (in case I forget to add one for example).

Thanks

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

For example, if I have this:

import { bineReducers } from 'redux'

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

const rootReducer = bineReducers({
    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 bineReducers(...) function is used to bine 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 bined reducer to make sure that it contains all the other reducers that it should (in case I forget to add one for example).

Thanks

Share Improve this question asked Mar 4, 2016 at 15:59 Dylan ParryDylan Parry 3,4536 gold badges27 silver badges38 bronze badges 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./reactjs/redux/blob/master/src/bineReducers.js#L93, i don't see any ref back to the un-bined 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 Commented Mar 4, 2016 at 16:28
  • Thanks. Your ment 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 bineReducers 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 Commented Mar 4, 2016 at 16:45
Add a ment  | 

1 Answer 1

Reset to default 7

You are testing the wrong thing IMO. You should trust that the bineReducers() 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 bine to pass as the parameter to bineReducers(). That method can and should be tested.

本文标签: javascriptTesting Redux combined reducersStack Overflow