Welcome to Module 4 Project! In this project, you will practice testing functions and components with Jest and React Testing Library.
To successfully complete this project, you will need the following technical knowledge:
- Core JavaScript to manipulate data.
- Jest to unit-test functions.
- React Testing Library (RTL) to test React components.
Here is an overview of the tasks you will complete:
-
You will write integration tests with React Testing Library to check that a form written in React includes the required pieces of text.
-
You will make the form support limited internationalization (i18n) by allowing the user of the app to toggle between English and Spanish.
-
You will write further integration tests to test the multi-language feature of the form.
-
You will write an auxiliary helper function and unit-test it.
Here are the steps to set up this project:
-
Clone this repository to your computer.
-
Within your terminal, navigate to the project folder and execute
npm install
. -
After successful installation execute
npm run dev
and, in a different terminal,npm test
. -
You will load the app in Chrome by navigating the browser to
http://localhost:3003
.
❗ Note: On the event of NPM errors during setup, delete the node_modules
folder and the package-lock.json
file, and retry npm i
and npm run dev
.
Here are some files of interest you will need to complete your tasks:
-
Inside a JSON document you will find an object in JSON format.
- The object contains the visible texts of headings and buttons, and also input labels and input placeholders.
- The object can be imported into other modules. The
import
statement will even take care of parsing the JSON for you!
-
Inside the App.js module you will find the form that renders in Chrome.
-
Inside the App.test.js module is where you will write unit and integration tests.
Have fun with the tasks, and check out the Solution Video for this project if you need help!
-
Start by "eyeballing" that the visible English texts that render in Chrome match the "en" texts found in the JSON file.
-
Inside the App test file, use React Testing Library to test that all the English shows correctly:
- Use
screen.getByText
to check that the various texts are visible. - Use
screen.getByPlaceholderText
to check that the placeholder texts are visible. - Use
screen.getByLabelText
to check that the input labels are visible.
- Use
-
An example test is written for your benefit. Note that the test is using hard-coded text instead of benefitting from the data found inside the JSON. This is workable but not very maintainable: changes in the JSON made by the UX expert in your team will not propagate to the tests. It's recommended that you import the JSON into the test file and use it as the source of truth!
-
Complete 13 passing tests, corresponding to the "en" keys inside the JSON.
Improve the App component so the user can toggle the UI between Spanish and English. The component receives a lang
prop that defaults to "en". This is used to initialize a state that controls which language is used. This state can be toggled by clicking on the little flag, but all headings, buttons, placeholders and labels are using hard-coded text in English right now.
-
Import the JSON file into the App component, and use it to extract the correct bits of text depending on the
language
component state. -
Edit the JSX of the component to use the correct texts, labels and placeholders, depending on the
language
state. -
At the end of your task, the form should switch correctly between Spanish and English!
-
After you are done, check that the tests you wrote for the English language are passing, and didn't break over the course of Task 2.
❗ Having tests to back you up is extremely useful whenever performing major surgery inside a component like we are doing in this task!
-
Inside the App test file, use React Testing Library to test that all the Spanish shows correctly, following the pattern used in Task 1.
-
At this point, if you are not importing the JSON inside the test file, you should do it now.
-
Complete 13 passing tests, corresponding to the "esp" keys inside the JSON.
❗ The JSON should be the source of truth for both the tests and the UI at this point. A non-technical person can easily maintain the JSON file, and little edits done to the visible texts inside this document will propagate to the component and the tests, for improved maintainability. Try this out by editing some texts in the JSON yourself, and see the changes carry over to Chrome, while the tests still pass.
-
Inside the test file, implement the
getEntriesByKeyPrefix
function as per the instructions found in the file. -
Use Jest to test the function as per the instructions found in the file.
-
Refactor your tests to use the helper function! (Optional step.)
❗ Watch the end of the solution video of this project, to learn how to make your tests DRYer and more dynamic.
I feel very stuck. What can I do?
Check out the Solution Video for this project in your learning platform. In it, an industry expert will walk you through their thinking in detail while they solve the tasks. The Solution Videos are highly recommended even if you are not stuck: you will learn lots of tricks.
I am getting errors when I run npm install. What is going on?
This project requires Node to be correctly installed on your computer to work. Sometimes Node can be installed but misconfigured. Try deleting node_modules
and running npm install
. If that fails, try deleting both node_modules
and package-lock.json
before reinstalling. If all fails, please request support!
Do I need to install extra libraries with NPM?
No. Everything you need should be installed already.
My page does not work! How do I debug it?
With React, it's very important that we use the React Dev Tools to monitor the state of our components as we interact with the App. If the state is not adjusting like it should, that's good to know. If the state does change but the UI does not respond, that's a different thing.
If your code has a syntax problem, the app will print error messages in the console. Focus on the first message. Place console logs right before the crash site (errors usually inform of the line number where the problem originates) and see if your variables contain the data you think they do. Comment out chunks of code until you get it to compile!
How do I run tests against my code?
Run npm test
to execute all test files in the project.