You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write code that allows to sepearate business logic frow UI.
Write code that allows to inject any hard-to-test, too-verbose, want-this-mocked stuff logic.
Is redux a good tool for impelmenting these ideas?
It has special place for handling business logic, called middleware.
Popular middleware have a mechanism for context injection. If you are writing your own custom middleware, then you probably should think about adding this too.
Have everything mocked by default. If spec or storybook wants an a store instance, give them one. And it should be safe to play with. No accidental graphql calls, analytics calls, you get the point.
Make mocking code extensible and flexible, in other words allow providing different behavior for mocked services.
it("displays an error on unsuccessful submit",()=>{constgraphqlMutationService={submitAgentRequest: ()=>Promise.reject("something went wrong")};conststore=configureStore({ graphqlMutationService });constcomponent=mountRender(store);//...expect(errorMessage.exists()).toBeTrue();})
it("updates error message when mutation was not successful",()=>{constexpectedError=newError("something went wrong");graphqlMutation.mutate.mockImplementation(()=>Promise.reject(expectedError));returnexpectSaga(mutationSaga,{ callback, formValues }).provide([[getContext("graphqlMutation"),graphqlMutation],[select(submitDataSelector),submitData]]).put(handleMutationFailure(expectedError)).run();});
Bonus: redux mental model for those who likes OOP/OOD
Store is an object, actions are messages (methods)
Selectors are getters
Message handling is spread across reducers (setters) and middleware (business logic)
Message can have no handlers (ignored by reducers and middleware)
Existing message handlers can be extended by adding middleware or another "switch" condition in any of the reducers