Coding task · JavaScript and React course · Module 13: Testing React
Practice: useReducer Hook
A reducer is a pure function: for the same state and action it returns the same result and never changes the old state, so it can be tested with a plain expect without rendering a component. A frozen state (Object.freeze) makes every mutation attempt end with an error in the test. The test runs the hook in a small test component and calls the actions in act(): this is how renderHook works, which you will meet in the lesson about testing hooks.
Task
Finish the missionReducer reducer and the useMissionTracker hook of the mission log. ___BLANK1___: the START action changes the status of a planned mission to 'active' by returning a new object with the other mission fields (do not change the object that is in the state). ___BLANK2___: the REMOVE action returns a new array without the mission whose id is in the action (action.id). ___BLANK3___: the hook passes the missionReducer reducer to useReducer. The test checks the reducer as a pure function on a frozen state (Object.freeze), and runs the hook in a small test component: it adds two missions, starts, completes and removes them.