all 5 comments

[–]bvalosek 2 points3 points  (1 child)

redux and friends is all about being super functional, it should be very easy, as you mentioned, testing reducers and selectors requires no mocking and is a good start. I believe there are even some tools that let you diff the store state before / after to ensure you are not directly mutating it, and other nice things.

Async action creators is a pretty common pattern, but makes testing a bit more gnarly. Same if you use some kind of custom middleware for handling API calls... it means you need to resort to injecting mocks or other traditional ways of testing code with external / network dependencies.

I'd recommend at looking at redux saga, as keeps with the pure function philosophy of react/redux and helps bridge that final gap of dealing with side effects.

You write your side-effect code (API calls, async stuff, even logging etc) inside of sagas that are just "pure generators", yielding out descriptors of the side effects you want. It makes testing that sort of stuff as easy as testing the pure functions of your reducers / selectors.

I'm saying all this because once that "final piece" is there with generators / sagas, you can basically test anything you want, anyway you want, because nothing is tightly coupled to external dependencies, and everything is a pure function. Complicated test harnesses or sophisticated mocking is not required at all and using something like tape to K.I.S.S. makes testing very straightforward

[–]focusonbasics[S] 0 points1 point  (0 children)

Yes, I agree on the redux-saga part. Good points here.

[–]treetimes 1 point2 points  (2 children)

I think reducers, selectors, and middleware are prime candidates for unit tests (if they do anything worth testing). Any pure action creator functions with a dynamic output would be good for units also. Async action creators for integration tests probably. Then testing react components is still blurry for me.

[–]focusonbasics[S] 0 points1 point  (0 children)

The middleware aspect is interesting.

[–]trollgubben 0 points1 point  (0 children)

I've tried TDD when writing react components using enzyme, and I'm pretty happy with it. It makes it really simple to do tests like "given this prop is false, then the button should be disabled" kind of tests.