-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathunit-tests-template-retail-react-app.mdc
More file actions
35 lines (28 loc) · 1.37 KB
/
unit-tests-template-retail-react-app.mdc
File metadata and controls
35 lines (28 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
---
description: USE WHEN writing unit tests in template-retail-react-app components
globs: ["packages/template-retail-react-app/app/components/**/*.test.{js,jsx,ts,tsx}"]
alwaysApply: false
---
# 🛍️ Retail React App Test Rules
## Package-Specific Requirements
- **File naming**: `index.test.js` (colocated with component)
- **Always use `renderWithProviders`** (provides Commerce SDK context)
- **Get user events from return value**: `const {user} = renderWithProviders(...)`
- **Do NOT import `userEvent` directly**
## API Mocking
- Use `prependHandlersToServer` or `msw` for API mocking
## Mock Data Usage
- **Mandatory**: Always use existing mock data from `@salesforce/retail-react-app/app/mocks/` if it is available. This ensures consistency across tests and reduces redundancy. Creating new mock data should only be considered if the required data is not already present in the mocks directory.
```js
import {screen} from '@testing-library/react'
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
import MyComponent from '.'
describe('MyComponent', () => {
beforeEach(() => jest.clearAllMocks())
test('handles user interaction', async () => {
const {user} = renderWithProviders(<MyComponent />)
await user.click(screen.getByText('Click Me'))
expect(screen.getByText('Expected')).toBeInTheDocument()
})
})
```