Coding task · JavaScript and React course · Module 13: Testing React
Practice: Asynchronous Component
The component does not call the API directly, it receives the loadPlanets function through props. Thanks to that, the test passes a jest.fn() mock that returns a promise with data or with an error, checks the loading state and then waits for the list or the alert with findByRole. The ignore variable in the effect prevents storing the result of a previous, outdated request (e.g. after the double effect in StrictMode).
Task
Finish PlanetLoader, which loads the planets with the loadPlanets function from props and shows three states: loading (role="status"), error (role="alert" with a Try again button) and the planet list. ___BLANK1___: after a successful load, store in the status state that the data is ready (e.g. 'success'), so that the list replaces the loading message. ___BLANK2___: when the promise is rejected, set the status to 'error'. ___BLANK3___: retry increases the attempt counter by 1, so the effect runs again and calls loadPlanets once more. In the preview the first load fails on purpose, so you will see the error message and the retry.