Skip to content

Commit 58c0408

Browse files
authored
Merge pull request #1873 from hackforla/1334-add-frontend-tests
add basic frontend tests with Vitest + React Testing Library
2 parents 54fecb2 + 2d58f3c commit 58c0408

File tree

12 files changed

+3060
-406
lines changed

12 files changed

+3060
-406
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,21 @@ Our mission is to create a user-friendly platform for anyone interested in explo
6565
- Run `npm start`
6666
- Visit http://localhost:5173
6767

68+
### Run tests
69+
70+
- Run `npm test`
71+
72+
See [contributing.md](contributing.md#testing) for info on writing tests.
73+
6874
### Information About Technologies
6975

7076
- Frontend
7177
- Mapbox
7278
- React
7379
- MUI
80+
- Vite
81+
- Vitest
82+
- React Testing Library
7483
- Backend
7584
- DuckDb
7685
- HuggingFace

components/Map/controls/MapOverview.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import clx from 'classnames';
99
import moment from 'moment';
1010
import RequestsDonut from './RequestsDonut';
1111
import RequestsBarChart from './RequestsBarChart';
12-
import Button from '@components/common/Button';
12+
// import Button from '@components/common/Button';
1313

1414
import {
1515
setMapMode as reduxSetMapMode,

components/common/DatePicker/DatePicker.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ function DatePicker({
126126

127127
const getCoordinates = () => {
128128
if (ref.current) {
129-
const { left, top, height } = ref.current.getClientRects()[0];
129+
const { left, top, height } =
130+
ref.current.getClientRects()[0] ?? ref.current.getBoundingClientRect();
130131
const offsetFromSelectorDisplay = 2;
131132
return {
132133
left,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from "vitest";
2+
import { fireEvent, screen } from "@testing-library/react";
3+
import { renderWithProviders } from "@utils/test-utils";
4+
import FilterMenu from "./FilterMenu";
5+
6+
describe('FilterMenu', () => {
7+
it('selects/deselects all request types', () => {
8+
renderWithProviders(<FilterMenu />);
9+
10+
const requestTypes = screen.getAllByRole('checkbox');
11+
const allCheckbox = screen.getByLabelText('Select/Deselect All');
12+
13+
requestTypes.forEach(checkbox => {
14+
expect(checkbox.checked).toEqual(true);
15+
});
16+
17+
fireEvent.click(allCheckbox);
18+
requestTypes.forEach(checkbox => {
19+
expect(checkbox.checked).toEqual(false);
20+
});
21+
22+
fireEvent.click(allCheckbox);
23+
requestTypes.forEach(checkbox => {
24+
expect(checkbox.checked).toEqual(true);
25+
});
26+
});
27+
});

contributing.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ If you want to look at our setup, check out the "Actions" tab in Github, as well
1919

2020
In addition to status checks, PR's are required to have at least one reviewer before being merged into `main`.
2121

22-
## Testing (to be implemented)
23-
CI Is driven by tests, they help instill confidence in pull requests because a developer can say "All the status checks pass and my new tests pass so the PR is safe to merge". When contributing new features, it is most ideal to write at least 4 tests targeting your code.
22+
## Testing
23+
Tests help instill confidence in pull requests because a developer can say "All the status checks pass and my new tests pass so the PR is safe to merge". When contributing new features, it is most ideal to write at least 4 tests targeting your code.
2424
- One for the "happy path"
2525
- Test the endpoint/feature in the way it is intended to be used
2626
- One for the "extreme path"
@@ -29,6 +29,27 @@ CI Is driven by tests, they help instill confidence in pull requests because a d
2929
- Test with strange input, (What if I send characters to a function that expects integers)
3030
- One for the "null path"
3131
- Test with empty params/nothing/emptiness
32-
33-
34-
Our front end tests are run through Jest and RTL.
32+
33+
### Frontend testing
34+
Our frontend tests are run through [Vitest](https://vitest.dev/) and [React Testing Library](https://testing-library.com/docs/react-testing-library/intro/). Vitest serves as a drop-in replacement for [Jest](https://jestjs.io/).
35+
36+
#### Intro
37+
If you're unfamiliar with writing UI tests, we recommend reading docs for React Testing Library and [Redux](https://redux.js.org/usage/writing-tests) to learn the basic concepts of integration testing a Redux app.
38+
39+
The structure of a frontend integration test is "arrange, act, assert":
40+
41+
1. Arrange: Render some UI
42+
2. Act: Simulate user interactions
43+
3. Assert: Expect certain state changes in the UI
44+
45+
#### In 311-Data
46+
Frontend testing for 311-Data is currently a work in progress. Example tests are provided in two files:
47+
48+
1. utils.test.js demonstrates unit testing non-UI code.
49+
2. FilterMenu.test.jsx demonstrates integration testing UI code. This follows the "arrange, act, assert" procedure mentioned above.
50+
51+
You can refer to these files as examples of how to write additional tests for 311-Data with Vitest and React Testing Library.
52+
53+
#### Where to put test files
54+
55+
Test files are placed in the same directory as the file that they test. This makes the association between application code and test code clear.

0 commit comments

Comments
 (0)