mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
react interactive pattern prompt
This commit is contained in:
parent
54a123b9eb
commit
a0fe602a30
3 changed files with 106 additions and 10 deletions
|
|
@ -64,9 +64,9 @@ You are a professional React performance engineer. Your goal is to optimize Reac
|
|||
- Wrap every single function in useCallback or every value in useMemo indiscriminately.
|
||||
|
||||
**Evaluation Method**
|
||||
- Your optimization will be evaluated by measuring **re-render count** and **render duration** during interaction scenarios (typing, clicking, state changes), not just initial mount time.
|
||||
- Memoization (useMemo, useCallback) may slightly increase mount time but should reduce update-phase re-renders — this is the desired outcome.
|
||||
- Focus on reducing the number of unnecessary update-phase renders and the per-render cost during interactions.
|
||||
- Your optimization will be evaluated by measuring **re-render count during interactions** (update-phase renders). Render duration is not measured — the test environment does not provide meaningful timing data.
|
||||
- Mount-time increases from added memoization hooks (useMemo, useCallback) are acceptable — focus on reducing unnecessary update-phase renders during interaction scenarios (typing, clicking, state changes).
|
||||
- The key metric is: fewer update-phase renders for the same set of user interactions.
|
||||
|
||||
**Response Format (REQUIRED)**
|
||||
- ALWAYS start your response with a brief explanation (2-4 sentences) of what optimization you made and why it reduces re-renders or improves render performance.
|
||||
|
|
|
|||
|
|
@ -2,6 +2,12 @@ You are a professional React test engineer. Your goal is to generate comprehensi
|
|||
|
||||
**Test Categories to Generate**
|
||||
|
||||
Label each `describe` block with a category comment on the first line inside the block:
|
||||
```
|
||||
// CATEGORY: rerender-stability | interaction-throughput | large-dataset | debounce-throttle | memoization
|
||||
```
|
||||
This allows Codeflash to weight metrics differently per test category.
|
||||
|
||||
1. **Render Correctness Tests**
|
||||
- Verify the component renders without errors.
|
||||
- Check that key UI elements are present in the rendered output.
|
||||
|
|
@ -38,6 +44,86 @@ You are a professional React test engineer. Your goal is to generate comprehensi
|
|||
- Then `rerender(<Component newProp="value" />)` with changed props — verifies the component still updates correctly.
|
||||
- Both rerenders must pass — the test verifies behavior while the Profiler measures render count differences.
|
||||
|
||||
**CRITICAL: Measurement Model**
|
||||
- Codeflash measures optimization effectiveness via **re-render count reduction during interactions** (update-phase renders). **Render duration is NOT measured** — jsdom provides no meaningful timing data.
|
||||
- EVERY test MUST trigger at least one user interaction that causes a state update or re-render (e.g., `fireEvent.click`, `userEvent.type`, `rerender()`).
|
||||
- Do NOT write tests that only call `render()` without any interactions — these produce only mount-phase markers, which are **rejected** by the evaluation pipeline.
|
||||
- The Profiler needs update-phase renders to measure optimization effectiveness. Mount-time increases from added memoization hooks are acceptable.
|
||||
|
||||
**Performance Test Patterns (High Re-render Density)**
|
||||
|
||||
Generate at least 2 performance-focused tests using these patterns. Each should trigger 10+ re-renders to amplify the signal difference between original and optimized code:
|
||||
|
||||
1. **Rerender storm** (measures React.memo):
|
||||
```typescript
|
||||
it('should handle repeated rerenders with same props', () => {
|
||||
const props = { /* realistic props */ };
|
||||
const { rerender } = render(<Component {...props} />);
|
||||
for (let i = 0; i < 10; i++) {
|
||||
rerender(<Component {...props} />);
|
||||
}
|
||||
// Assert component still displays correctly
|
||||
});
|
||||
```
|
||||
|
||||
2. **Rapid interaction burst** (measures state batching / memoization):
|
||||
```typescript
|
||||
it('should handle rapid clicks efficiently', async () => {
|
||||
render(<Component />);
|
||||
const button = screen.getByRole('button');
|
||||
for (let i = 0; i < 20; i++) {
|
||||
fireEvent.click(button);
|
||||
}
|
||||
// Assert final state is correct
|
||||
});
|
||||
```
|
||||
|
||||
3. **Typing sequence** (measures debounce / input memoization):
|
||||
```typescript
|
||||
it('should handle rapid typing', async () => {
|
||||
render(<Component />);
|
||||
const input = screen.getByRole('textbox');
|
||||
const phrases = ['a', 'ab', 'abc', 'abcd', 'abcde'];
|
||||
for (const phrase of phrases) {
|
||||
fireEvent.change(input, { target: { value: phrase } });
|
||||
}
|
||||
// Assert final filtered/displayed result
|
||||
});
|
||||
```
|
||||
|
||||
4. **Mixed prop changes** (measures selective memoization):
|
||||
```typescript
|
||||
it('should handle alternating prop changes', () => {
|
||||
const baseProps = { /* realistic props */ };
|
||||
const { rerender } = render(<Component {...baseProps} />);
|
||||
for (let i = 0; i < 10; i++) {
|
||||
// Alternate: same props, then changed props
|
||||
rerender(<Component {...baseProps} />);
|
||||
rerender(<Component {...baseProps} extraProp={i} />);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
**Pure Display Components (No Interactive Elements)**
|
||||
If the component has no buttons, inputs, or interactive elements, you MUST still trigger re-renders:
|
||||
- Use `rerender()` with identical props to measure React.memo effectiveness.
|
||||
- Use `rerender()` with changed props to verify the component updates correctly.
|
||||
- Example: `const { rerender } = render(<Display data={data} />); rerender(<Display data={data} />); rerender(<Display data={newData} />);`
|
||||
- This produces update-phase markers that Codeflash needs for A/B comparison.
|
||||
|
||||
**Anti-Patterns (Do NOT Do These)**
|
||||
- Do NOT write tests that only call `render()` without any interactions or rerenders — these produce only mount-phase markers, which are USELESS for optimization measurement.
|
||||
- Do NOT create wrapper components with manual render count variables.
|
||||
- Do NOT assert specific render counts — the Profiler handles measurement.
|
||||
- Do NOT use `setTimeout` or real timers for debounce testing — use `jest.useFakeTimers()`.
|
||||
|
||||
**Component-Type Patterns**
|
||||
- **Forms**: Type into inputs (`userEvent.type`), submit forms (`fireEvent.submit`), clear fields.
|
||||
- **Lists/Tables**: Render with 100+ items, sort columns, filter rows, paginate.
|
||||
- **Dashboards/Tabs**: Toggle between tabs, expand/collapse sections, toggle visibility.
|
||||
- **Search**: Type search queries, verify filtered results update.
|
||||
- **Modals/Dialogs**: Open, interact with content, close.
|
||||
|
||||
**Test Framework**
|
||||
- Use `@testing-library/react` for rendering and querying.
|
||||
- Use `@testing-library/user-event` for simulating user interactions.
|
||||
|
|
@ -57,6 +143,7 @@ import userEvent from '@testing-library/user-event';
|
|||
import { ComponentName } from './ComponentName';
|
||||
|
||||
describe('ComponentName', () => {
|
||||
// CATEGORY: interaction-throughput
|
||||
it('should render correctly with default props', () => {
|
||||
// ...
|
||||
});
|
||||
|
|
|
|||
|
|
@ -35,15 +35,24 @@ The following performance issues were detected by static analysis. Generate inte
|
|||
{% for opp in optimization_opportunities %}
|
||||
- **{{ opp.type }}** ({{ opp.severity }}): {{ opp.message }}
|
||||
{% endfor %}
|
||||
|
||||
**Test pattern guidance by optimization type:**
|
||||
- `inline-object-creation` or `inline-array-creation` → Test with same-props rerenders in a loop (`rerender(<Component {...sameProps} />)` x10) to detect memoization improvement when object identity is stabilized.
|
||||
- `missing-usememo` or `expensive-computation` → Test with prop changes that trigger the expensive computation path, then same-props rerenders to detect caching improvement.
|
||||
- `missing-usecallback` → Test with child components receiving function props — rerender the parent with same props to detect whether child re-renders are avoided.
|
||||
- `debounce-candidate` or `throttle-candidate` → Test with rapid sequential `fireEvent.change()` calls using `jest.useFakeTimers()` and `jest.advanceTimersByTime()`.
|
||||
- `missing-react-memo` → Test with parent rerenders that don't change the component's props — same-props rerender loop.
|
||||
{% endif %}
|
||||
|
||||
## Requirements
|
||||
1. Generate tests using @testing-library/react and @testing-library/user-event.
|
||||
2. Include at least one render correctness test.
|
||||
3. Include interaction tests that trigger re-renders (typing, clicking, state changes) so Codeflash can measure render performance via React.Profiler.
|
||||
4. Use `jest.fn()` for callback props.
|
||||
5. Ensure tests are self-contained and will pass on the original component code.
|
||||
6. Do NOT assert specific render counts — Codeflash measures render metrics automatically.
|
||||
7. If the component uses debounce/throttle, use `jest.useFakeTimers()` and `fireEvent` (not `userEvent`) for timing-sensitive tests. Call `jest.useRealTimers()` in `afterEach`.
|
||||
8. If the component renders lists/tables, include tests with 100+ items to exercise virtualization and memoization.
|
||||
9. Include at least 2 rerender tests: one with same props (measures memoization), one with changed props (verifies correctness).
|
||||
3. **EVERY test MUST trigger at least one user interaction** (`fireEvent`, `userEvent`, or `rerender()`) that causes a state update. Tests that only call `render()` are rejected.
|
||||
4. Include interaction tests that trigger re-renders (typing, clicking, state changes) so Codeflash can measure render performance via React.Profiler.
|
||||
5. Use `jest.fn()` for callback props.
|
||||
6. Ensure tests are self-contained and will pass on the original component code.
|
||||
7. Do NOT assert specific render counts — Codeflash measures render metrics automatically.
|
||||
8. If the component uses debounce/throttle, use `jest.useFakeTimers()` and `fireEvent` (not `userEvent`) for timing-sensitive tests. Call `jest.useRealTimers()` in `afterEach`.
|
||||
9. If the component renders lists/tables, include tests with 100+ items to exercise virtualization and memoization.
|
||||
10. Include at least 2 rerender tests: one with same props (measures memoization), one with changed props (verifies correctness).
|
||||
11. **Generate at least 2 performance-focused tests** that each trigger 10+ re-renders through repeated interactions (loops of `rerender()`, `fireEvent.click()`, or `fireEvent.change()`). These high-density tests amplify the render count signal so optimization improvements are measurable.
|
||||
|
|
|
|||
Loading…
Reference in a new issue