- use mocks in unit tests
- test asynchronous API calls that are made in a component
- test React components as the props change
As a developer, you will be asked to write tests for the feature you are building, and even sometimes on components and features other developers have built. This project will mimic a situation where you are asked to test someone else's code.
Get the project fired up and start using it as a user would. Try to go through the user sequences for this feature that you think users would go through. Once you have those in mind, you will have an idea of what to test in your application.
- Create a forked copy of this project.
- Add your team lead as collaborator on Github.
- Clone your OWN version of the repository in your terminal
- CD into the project base directory
cd React-Testing-TV-Show
- Download project dependencies by running
npm install
- Start up the app using
npm start
- Create a new branch: git checkout -b
<firstName-lastName>
. - Implement the project on your newly created
<firstName-lastName>
branch, committing changes regularly. - Push commits: git push origin
<firstName-lastName>
.
Follow these steps for completing your project.
- Submit a Pull-Request to merge Branch into master (student's Repository). Please don't merge your own pull request
- Add your team lead as a reviewer on the pull-request
- Your team lead will count the project as complete by merging the branch back into master.
- Do your magic!
Your challenge for this module: write tests for both the App.js
component and the Episodesjs
component. Take note of where the state is being managed, where the async call is, and where different data peices are being rendered. Understanding all of this will be important so you know how to test each component.
Moving the async call
The async call being inside the component makes it hard to test the asynchronous nature of the component. Let's move the async function into an /api
directory so we can easily mock that function and make the async tests easier.
- Create a directory called
/api
in thesrc
directory - Create a file inside
/api
calledfetchShow.js
- Move
fetchShow
into that new file and export it (fetchShow is in theuseEffect
- pay attention to how this was setting state. You will still need to set state in this effect hook the exact same way...) - Import
fetchShow
intoApp.js
so you can make your async call from youruseEffect
hook. - You will need to
return
theaxios.get
call, and the data inside your.then()
. This is necessary because when you callfetchShow
in your useEffect, you need to chain off the promise for a new.then()
, then you need the data to be returned once the promise is resolved - Inside your
.then()
in theuseEffect
hook, set your data again.
- Note that you need
axios
in the new file
This should look something like this:
// fetchShow.js
export const fetchShow = () => {
return axios.get
.then(res => return res) // or res.data, however you want to set that up
}
// App.js
useEffect(() => {
fetchShow
.then(res => {
// set state with the data
}
}, []);
-
There is an utility function in this project that contains an isolated pure function. Look up how to do
unit tests
with Jest and test that function. -
Look up the
TVMaze
API. Add a dropdown with the titles of some other popular shows. Add the user sequence of choosing a different show to fetch data for different shows. -
Add React Router, and add the functionality to click an episode and navigate to an episode page.