- Test Setup
- Testing a Prop
- Testing a Function Prop
- Taking a Snapshot
-
Create the directory
src\projects\__tests__
. -
Create the file
src\projects\__tests__\ProjectCard-test.js
. -
Add the setup code below to test the component.
import { render, screen } from '@testing-library/react'; import React from 'react'; import { Project } from '../Project'; import ProjectCard from '../ProjectCard'; describe('<ProjectCard />', () => { let project; let handleEdit; beforeEach(() => { project = new Project({ id: 1, name: 'Mission Impossible', description: 'This is really difficult', budget: 100, }); handleEdit = jest.fn(); render(<ProjectCard project={project} onEdit={handleEdit} />); }); it('renders without crashing', () => { expect(screen).toBeDefined(); }); });
-
Verify the test fails.
FAIL src/projects/__tests__/ProjectCard-test.js Invariant failed: You should not use <Link> outside a <Router>
-
Wrap the component in a
MemoryRouter
.import { render, screen } from '@testing-library/react'; import React from 'react'; + import { MemoryRouter } from 'react-router-dom'; import { Project } from '../Project'; import ProjectCard from '../ProjectCard'; describe('<ProjectCard />', () => { let project; let handleEdit; beforeEach(() => { project = new Project({ id: 1, name: 'Mission Impossible', description: 'This is really difficult', budget: 100, }); handleEdit = jest.fn(); render( + <MemoryRouter> <ProjectCard project={project} onEdit={handleEdit} /> + </MemoryRouter> ); }); it('renders without crashing', () => { expect(screen).toBeDefined(); }); });
<MemoryRouter>
- a<Router>
that keeps the history of your "URL" in memory (does not read or write to the address bar). Useful in tests and non-browser environments like React Native. -
Verify the initial test now passes.
PASS src/projects/__tests__/ProjectCard-test.js
-
Open a command-prompt or terminal and run the following command to install
user-event
from the core testing library behind React testing library.npm install --save-dev @testing-library/user-event @testing-library/dom
-
Test that the project property renders correctly.
... describe('<ProjectCard />', () => { let project; let handleEdit; beforeEach(() => { project = new Project({ id: 1, name: 'Mission Impossible', description: 'This is really difficult', budget: 100, }); handleEdit = jest.fn(); render( <MemoryRouter> <ProjectCard project={project} onEdit={handleEdit} /> </MemoryRouter> ); }); it('renders without crashing', () => { expect(screen).toBeDefined(); }); + it('renders project properly', () => { + expect(screen.getByRole('heading')).toHaveTextContent(project.name); + // screen.debug(document); + screen.getByText(/this is really difficult\.\.\./i); + screen.getByText(/budget : 100/i); + }); });
-
Verify the test passes.
PASS src/projects/__tests__/ProjectCard-test.js
- Open a command-prompt or terminal and run the following command to install
user-event
from the core testing library behind React testing library.
npm install --save-dev @testing-library/user-event @testing-library/dom
-
Test that the handler prop is called when edit is clicked.
import { render, screen } from '@testing-library/react'; import React from 'react'; import { MemoryRouter } from 'react-router-dom'; import { Project } from '../Project'; import ProjectCard from '../ProjectCard'; + import userEvent from '@testing-library/user-event'; describe('<ProjectCard />', () => { let project; let handleEdit; beforeEach(() => { project = new Project({ id: 1, name: 'Mission Impossible', description: 'This is really difficult', budget: 100, }); handleEdit = jest.fn(); render( <MemoryRouter> <ProjectCard project={project} onEdit={handleEdit} /> </MemoryRouter> ); }); ... + it('handler called when edit clicked', () => { + // this query works screen.getByText(/edit/i) + // but using role is better + userEvent.click( + screen.getByRole('button') + ); + expect(handleEdit).toBeCalledTimes(1); + expect(handleEdit).toBeCalledWith(project); + }); });
-
Verify the test passes.
PASS src/projects/__tests__/ProjectCard-test.js
-
Take a snapshot of the component.
import { render, screen } from '@testing-library/react'; import React from 'react'; import { MemoryRouter } from 'react-router-dom'; import { Project } from '../Project'; import ProjectCard from '../ProjectCard'; import userEvent from '@testing-library/user-event'; + import renderer from 'react-test-renderer'; describe('<ProjectCard />', () => { let project; let handleEdit; beforeEach(() => { ... }); ... + test('snapshot', () => { + const tree = renderer + .create( + <MemoryRouter> + <ProjectCard project={project} onEdit={handleEdit} /> + </MemoryRouter> + ) + .toJSON(); + expect(tree).toMatchSnapshot(); + }); });
-
Verify the snapshot is taken.
✓ 1 snapshot written