You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add comprehensive unit tests for ResponsiveImage component
- Create ResponsiveImage.test.js with detailed test coverage
- Verify rendering with minimal and full props
- Test lazy loading behavior and custom class names
- Handle missing image sources gracefully
- Implement tests for multiple image sources and formats
- Update PropTypes to support spread props pattern
- Enhance component testability and documentation
- Use fixture files for complex test data in `src/tests/fixtures/`
99
+
- For image testing, use mock paths like `/images/test-small.jpg` (no need for actual images)
100
+
- Add test IDs with the format `data-testid="component-name-element-type"` when needed
82
101
83
-
### 7. **Planned CI/CD Integration**
102
+
##8. CI/CD Integration
84
103
85
104
To ensure code quality, tests will be executed automatically in CI/CD pipelines:
86
105
87
106
- GitHub Actions (or another CI tool) will run tests on push and pull requests.
88
-
-A test script in `package.json` will facilitate local execution:
107
+
-Test scripts in `package.json` will facilitate local execution:
89
108
90
109
```json
110
+
{
91
111
"scripts": {
92
-
"test": "jest --coverage"
112
+
"test": "react-scripts test",
113
+
"test:coverage": "react-scripts test --coverage",
114
+
"test:watch": "react-scripts test --watch",
115
+
"test:ci": "react-scripts test --ci --watchAll=false"
93
116
}
94
-
```
117
+
}
118
+
```
95
119
96
-
### 8. **Future Publishing Considerations**
120
+
##9. Future Publishing Considerations
97
121
98
122
Before publishing to npm or deploying, the following test requirements should be met:
99
123
@@ -109,3 +133,43 @@ Before publishing to npm or deploying, the following test requirements should be
109
133
By following this guideline, testing in the project will remain well-structured, scalable, and adaptable to future technology choices.
110
134
111
135
Adjustments to these guidelines may be made as the project evolves, and recommendations from Cursor AI will be considered for further improvements.
136
+
137
+
## 10. DOM Interaction Best Practices
138
+
139
+
-**Avoid direct DOM manipulation**: Avoid using methods like `querySelector`, `getElementsByClassName`, `closest`, or direct element property access in tests
140
+
141
+
-**Use Testing Library queries**: Always prefer queries provided by React Testing Library in this order of preference:
142
+
1.`getByRole` - Most accessible and user-centric
143
+
2.`getByLabelText` - For form elements with labels
144
+
3.`getByPlaceholderText` - For input fields with placeholders
145
+
4.`getByText` - For elements containing visible text
146
+
5.`getByDisplayValue` - For form elements with a specific value
147
+
6.`getByAltText` - For images
148
+
7.`getByTitle` - For elements with title attributes
149
+
8.`getByTestId` - Last resort when no other selectors work
150
+
151
+
-**Component design considerations**:
152
+
- Design components to accept and spread additional props to their root elements:
- This makes them more testable by allowing `data-testid` attributes
163
+
164
+
-**Why this matters**:
165
+
- Tests become more resilient to implementation changes
166
+
- Encourages building accessible components
167
+
- Tests better reflect how users interact with the UI
168
+
- Improves test maintenance and clarity
169
+
170
+
-**Origin of these rules**:
171
+
- These practices come from the core philosophy of Testing Library:"The more your tests resemble the way your software is used, the more confidence they can give you."
172
+
- Enforced by `eslint-plugin-testing-library` rules, particularly `testing-library/no-node-access`
173
+
- Developed by Kent C. Dodds and the Testing Library community as a reaction against implementation-detail-focused testing
174
+
- The error "Avoid direct Node access. Prefer using the methods from Testing Library" comes from the `testing-library/no-node-access` ESLint rule
175
+
- Official documentation:<https://testing-library.com/docs/queries/about/#priority>
0 commit comments