Skip to content

Commit 8c2e84b

Browse files
committed
Merge branch 'feature/pwa-developer-agent-only' of https://github.com/SalesforceCommerceCloud/pwa-kit into feature/pwa-developer-agent-only
2 parents ec79e73 + 686386d commit 8c2e84b

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
description: USE WHEN writing unit tests for components in template packages
3+
globs: ["packages/template-*/*/components/**/*.test.{js,jsx,ts,tsx}"]
4+
alwaysApply: false
5+
---
6+
7+
# 🧪 Generic Component Test Rules
8+
9+
## Structure & Best Practices
10+
- Use `describe` blocks to group tests, `test` for individual cases
11+
- Use `beforeEach` for setup, clear mocks after each test
12+
- **Arrange** → **Act** → **Assert** pattern
13+
- One behavior per test, clear descriptive names
14+
15+
## Queries & Assertions
16+
- Prefer `getByRole`, `getByLabelText`, `getByTestId`
17+
- Use `expect().toBeInTheDocument()`, `.toHaveBeenCalledTimes()`, etc.
18+
- For async: `await waitFor(() => { ... })`
19+
20+
## Mocking
21+
- `jest.fn()` for handlers, `jest.mock()` for modules
22+
- Clear mocks/storage after each test
23+
24+
```js
25+
describe('MyComponent', () => {
26+
beforeEach(() => jest.clearAllMocks())
27+
28+
test('renders and handles interaction', async () => {
29+
const mockHandler = jest.fn()
30+
render(<MyComponent onClick={mockHandler} />)
31+
32+
await userEvent.click(screen.getByRole('button'))
33+
expect(mockHandler).toHaveBeenCalledTimes(1)
34+
})
35+
})
36+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
description: USE WHEN writing unit tests in template-retail-react-app components
3+
globs: ["packages/template-retail-react-app/app/components/**/*.test.{js,jsx,ts,tsx}"]
4+
alwaysApply: false
5+
---
6+
7+
# 🛍️ Retail React App Test Rules
8+
9+
## Package-Specific Requirements
10+
- **File naming**: `index.test.js` (colocated with component)
11+
- **Always use `renderWithProviders`** (provides Commerce SDK context)
12+
- **Get user events from return value**: `const {user} = renderWithProviders(...)`
13+
- **Do NOT import `userEvent` directly**
14+
15+
## API Mocking
16+
- Use `prependHandlersToServer` or `msw` for API mocking
17+
18+
```js
19+
import {screen} from '@testing-library/react'
20+
import {renderWithProviders} from '@salesforce/retail-react-app/app/utils/test-utils'
21+
import MyComponent from '.'
22+
23+
describe('MyComponent', () => {
24+
beforeEach(() => jest.clearAllMocks())
25+
26+
test('handles user interaction', async () => {
27+
const {user} = renderWithProviders(<MyComponent />)
28+
await user.click(screen.getByText('Click Me'))
29+
expect(screen.getByText('Expected')).toBeInTheDocument()
30+
})
31+
})
32+
```

0 commit comments

Comments
 (0)