-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtanstack-react-query-tests.mdc
More file actions
126 lines (95 loc) Β· 3.39 KB
/
Copy pathtanstack-react-query-tests.mdc
File metadata and controls
126 lines (95 loc) Β· 3.39 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
description: tRPC TanStack React Query testing patterns using testReactResource
globs: ['packages/tanstack-react-query/**/*.test.tsx']
alwaysApply: false
---
# tRPC TanStack React Query Testing Patterns
## TanStack React Query Specific Testing
### ALWAYS use testReactResource from local \_\_helpers
- **ALWAYS** use `await using ctx = testReactResource()` for TanStack React Query tests
- Import `testReactResource` from `./__helpers` (not from other packages)
- This provides TanStack-specific context creation and provider setup
### Test Resource Management
```typescript
// β
CORRECT: Use await using for automatic cleanup
await using ctx = testReactResource(appRouter, {
server: {
// server configuration
},
client(opts) {
return {
links: [
httpLink({
url: opts.httpUrl,
// client configuration
}),
],
};
},
});
```
### TanStack React Query Specific APIs
- Use `ctx.useTRPC()` for the TanStack React Query hooks
- Use `ctx.useTRPCClient()` for vanilla client access
- Use `ctx.optionsProxyClient` for options proxy functionality
- Use `ctx.optionsProxyServer` for server-side options proxy
### Test Structure for TanStack React Query
```typescript
test('tanstack react query test', async () => {
await using ctx = testReactResource(appRouter);
function MyComponent() {
const query = ctx.useTRPC().post.byId.useQuery({ id: '1' });
return (
<div>
{query.data ? `Result: ${query.data}` : 'Loading...'}
</div>
);
}
const utils = ctx.renderApp(<MyComponent />);
await vi.waitFor(() => {
expect(utils.container).toHaveTextContent('Result:');
});
});
```
### Provider Setup
- Uses `TRPCProvider` from `createTRPCContext()`
- Wraps with `QueryClientProvider` from `@tanstack/react-query`
- Access via `ctx.renderApp()` and `ctx.rerenderApp()`
### Testing Helper Context Pattern
```typescript
const testContext = () => {
const t = initTRPC.create({});
const appRouter = t.router({
// router definition
});
return {
...testReactResource(appRouter),
// additional TanStack-specific utilities
};
};
```
### Import Requirements for TanStack React Query
#### Test Helper Imports
- **ALWAYS** import `testReactResource` from `./__helpers` (local to tanstack-react-query package)
- **NEVER** import from other packages' helpers - each package has its own implementation
- Path: `import { testReactResource } from './__helpers';`
#### Other Required Imports
- Import `@testing-library/react` for rendering utilities
- Import `@testing-library/user-event` for user interactions
- Import React Query types from `@tanstack/react-query`
- Import TanStack React Query utilities from `../src`
- Import React testing utilities: `import * as React from 'react';`
### Key Differences from Legacy React Query
- Uses newer TanStack React Query provider pattern
- Has `optionsProxyClient` and `optionsProxyServer` for options proxy testing
- Uses `createTRPCContext()` instead of older React Query patterns
- More streamlined context creation and provider setup
### Helper Import Comparison
```typescript
// β
TanStack React Query (this package)
import { testReactResource } from './__helpers';
// β DON'T use legacy React Query pattern
import { createAppRouter } from './__testHelpers'; // Wrong package
// β DON'T use upgrade package pattern
import { testReactResource } from './test/__helpers'; // Wrong path
```