Skip to content

Commit c6f8020

Browse files
authored
Merge pull request #309 from kbss-cvut/308-test-improvments
308 test improvments
2 parents ea1040d + aefe583 commit c6f8020

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2481
-2421
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Record Manager is an application for generic form-based collection of data. This
1111
Build configuration and deployment is described in [Setup Guide](doc/setup.md).
1212
Development is described in [Development notes](doc/development.md).
1313
Security is described in [Security](doc/security.md).
14+
Component testing is described in [Component Testing](doc/component-testing.md).

doc/component-testing.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Component Testing
2+
3+
## Libraries & Setup
4+
5+
All component tests use the following libraries and utilities:
6+
7+
| Purpose | Library / Utility |
8+
| --------------------------------- | --------------------------------------------------------------------------------------- |
9+
| Testing framework | [Vitest](https://vitest.dev/) |
10+
| Component rendering & interaction | [@testing-library/react](https://testing-library.com/docs/react-testing-library/intro/) |
11+
| Assertions | [@testing-library/jest-dom](https://github.com/testing-library/jest-dom) |
12+
| Mocking | `vi` (from Vitest) |
13+
| Internationalization utilities | `getMessageByKey`, `renderWithIntl` (custom test utils) |
14+
15+
### Example Imports
16+
17+
```js
18+
import { screen, fireEvent, waitFor } from "@testing-library/react";
19+
import "@testing-library/jest-dom";
20+
import { vi, describe, it, expect, beforeEach } from "vitest";
21+
import { getMessageByKey, renderWithIntl } from "../../utils/utils.jsx";
22+
```
23+
24+
## File Structure & Naming
25+
26+
Each test file should be located next to or within a corresponding component test folder:
27+
28+
```bash
29+
tests/components/<ComponentName>.test.jsx
30+
```
31+
32+
Example:
33+
34+
```bash
35+
tests/components/Dashboard.test.jsx
36+
```
37+
38+
Test File Structure
39+
Each test file follows the same structure:
40+
41+
1. Imports — libraries, helpers, constants, and the component under test.
42+
43+
2. defaultProps — define a reusable base props object.
44+
45+
3. Mocks — define mock handlers, data, or API responses.
46+
47+
4. renderComponent() — a reusable function to render the component with optional overridden props.
48+
49+
5. Test suite (describe) — wraps all test cases for the component.
50+
51+
6. beforeEach() — resets mocks before each test.
52+
53+
7. Test cases (it) — follow consistent naming and structure.
54+
55+
### Example Test File Structure
56+
57+
```js
58+
import { screen, fireEvent, waitFor } from "@testing-library/react";
59+
import "@testing-library/jest-dom";
60+
import { vi, describe, expect, beforeEach, it } from "vitest";
61+
import ComponentName from "../../../src/components/.../ComponentName";
62+
import { renderWithIntl, getMessageByKey } from "../../utils/utils.jsx";
63+
64+
const defaultProps = {
65+
/* base props */
66+
};
67+
68+
vi.mock("path/to/dependency", () => ({
69+
/* mock implementation */
70+
}));
71+
72+
const renderComponent = (props = {}) => {
73+
return renderWithIntl(<ComponentName {...defaultProps} {...props} />);
74+
};
75+
76+
describe("ComponentName", () => {
77+
beforeEach(() => {
78+
vi.clearAllMocks();
79+
});
80+
81+
it("renders something", () => {
82+
renderComponent();
83+
expect(screen.getByText("...")).toBeInTheDocument();
84+
});
85+
86+
it("calls handler when clicked", () => {
87+
renderComponent();
88+
fireEvent.click(screen.getByText("..."));
89+
expect(defaultProps.handlers.someHandler).toHaveBeenCalled();
90+
});
91+
});
92+
```
93+
94+
## Naming Conventions
95+
96+
Each test description should start with a **verb** describing what is being tested.
97+
Use consistent prefixes for clarity and readability.
98+
99+
| Prefix | Purpose |
100+
| --------------------------- | ----------------------------------------------------------------------------------------- |
101+
| `renders / does not render` | Verifies that the component renders certain elements or does not under certain conditions |
102+
| `disables / enables` | Verifies that a control is disabled or enabled |
103+
| `calls` | Verifies that a handler function is invoked |
104+
| `opens / closes` | Verifies modal or dropdown behavior |
105+
106+
### Example Descriptions
107+
108+
✅ "renders create record tile when user has WRITE_ALL_RECORDS role"
109+
110+
✅ "does not render statistics tile when user lacks READ_STATISTICS role"
111+
112+
✅ "calls showRecords handler when records tile is clicked"

0 commit comments

Comments
 (0)