|
1 | 1 | import { describe, it, expect, jest } from '@jest/globals'; |
2 | 2 | import z from 'zod'; |
3 | 3 |
|
| 4 | +// Helper function to create mock request with Base64 encoded input |
| 5 | +const createMockRequest = (input: any) => { |
| 6 | + const serializedInput = JSON.stringify(input, (key, value) => { |
| 7 | + if (Number.isNaN(value)) { |
| 8 | + return '__NAN__'; |
| 9 | + } |
| 10 | + return value; |
| 11 | + }); |
| 12 | + const encoded = btoa(serializedInput); |
| 13 | + const searchParams = new URLSearchParams(); |
| 14 | + searchParams.set('input', encoded); |
| 15 | + return { nextUrl: { searchParams } }; |
| 16 | +}; |
| 17 | + |
4 | 18 | // Mock the dependencies we can't import in test environment |
5 | 19 | jest.mock('next/server', () => ({ |
6 | 20 | NextResponse: { |
@@ -119,8 +133,7 @@ describe('create-trpc-api', () => { |
119 | 133 | getUser: endpoint.input(schema).action(mockAction) |
120 | 134 | }); |
121 | 135 |
|
122 | | - const searchParams = new URLSearchParams('id=123'); |
123 | | - const mockRequest = { nextUrl: { searchParams } }; |
| 136 | + const mockRequest = createMockRequest({ id: '123' }); |
124 | 137 | const handler = createTrpcAPI({ router: testRouter }); |
125 | 138 |
|
126 | 139 | await handler(mockRequest as any, { params: Promise.resolve({ trpc: 'get-user' }) }); |
@@ -162,8 +175,7 @@ describe('create-trpc-api', () => { |
162 | 175 | getUser: endpoint.input(schema).action(mockAction) |
163 | 176 | }); |
164 | 177 |
|
165 | | - const searchParams = new URLSearchParams('id='); |
166 | | - const mockRequest = { nextUrl: { searchParams } }; |
| 178 | + const mockRequest = createMockRequest({ id: '' }); |
167 | 179 | const handler = createTrpcAPI({ router: testRouter }); |
168 | 180 |
|
169 | 181 | await handler(mockRequest as any, { params: Promise.resolve({ trpc: 'get-user' }) }); |
@@ -220,8 +232,7 @@ describe('create-trpc-api', () => { |
220 | 232 | createUser: endpoint.input(schema).action(mockAction) |
221 | 233 | }); |
222 | 234 |
|
223 | | - const searchParams = new URLSearchParams('name=John&age=30'); |
224 | | - const mockRequest = { nextUrl: { searchParams } }; |
| 235 | + const mockRequest = createMockRequest({ name: 'John', age: '30' }); |
225 | 236 | const handler = createTrpcAPI({ router: testRouter }); |
226 | 237 |
|
227 | 238 | await handler(mockRequest as any, { params: Promise.resolve({ trpc: 'create-user' }) }); |
|
0 commit comments