Summary
This chapter covered the most complex form of mocking: setting up component mocks with jest.mock
.
Since mocking is a complex art form, it’s best to stick with a small set of established patterns, which I’ve shown in this chapter. You can also refer to the code in Chapter 11, Test-Driving React Router, for examples that show some of the variations that have been described in this chapter.
You also learned how to test-drive a useEffect
hook before writing another matcher.
You should now feel confident with testing child components by using component mocks, Including loading data into those components through useEffect
actions.
In the next chapter, we’ll extend this technique further by pulling out callback
props from mock components and invoking them within our tests.