Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

serializer-recursion.test-d.ts

80 lines2.9 KB

1import { describe, expectTypeOf, it } from 'vitest'
2​
3import type {
4 Serializable,
5 ValidateSerializable,
6 ValidateSerializableResult,
7} from '../src/ssr/serializer/transformer'
8​
9describe('ValidateSerializable array handling', () => {
10 it('preserves nested array payloads for input validation', () => {
11 type Input = Array<{ value: string; nested: Array<{ id: number }> }>
12 expectTypeOf<
13 ValidateSerializable<Input, Serializable>
14 >().branded.toEqualTypeOf<Input>()
15 })
16​
17 it('preserves tuple structure for input validation', () => {
18 type InputTuple = readonly [{ name: string }, { count: number }]
19 expectTypeOf<
20 ValidateSerializable<InputTuple, Serializable>
21 >().branded.toEqualTypeOf<InputTuple>()
22 })
23​
24 it('preserves readonly array structure for input validation', () => {
25 type InputReadonlyArray = ReadonlyArray<{ value: string }>
26 expectTypeOf<
27 ValidateSerializable<InputReadonlyArray, Serializable>
28 >().branded.toEqualTypeOf<InputReadonlyArray>()
29 })
30​
31 it('preserves recursive payloads wrapped in Promise for input validation', () => {
32 type Recursive = { value: number; next?: Recursive }
33 type InputPromise = Promise<Recursive>
34 expectTypeOf<
35 ValidateSerializable<InputPromise, Serializable>
36 >().branded.toEqualTypeOf<InputPromise>()
37 })
38​
39 it('preserves recursive payloads wrapped in Promise<Array> for input validation', () => {
40 type Recursive = { value: number; children?: Array<Recursive> }
41 type InputPromiseArray = Promise<Array<Recursive>>
42 expectTypeOf<
43 ValidateSerializable<InputPromiseArray, Serializable>
44 >().branded.toEqualTypeOf<InputPromiseArray>()
45 })
46​
47 it('preserves recursive payloads wrapped in ReadableStream for input validation', () => {
48 type Recursive = { value: number; next?: Recursive }
49 type InputStream = ReadableStream<Recursive>
50 expectTypeOf<
51 ValidateSerializable<InputStream, Serializable>
52 >().branded.toEqualTypeOf<InputStream>()
53 })
54​
55 it('should preserve recursive payload without infinite expansion', () => {
56 type Result = Array<Result> | { [key: string]: Result }
57 expectTypeOf<
58 ValidateSerializableResult<Result, Serializable>
59 >().branded.toEqualTypeOf<Result>()
60 })
61​
62 it('should preserve recursive tuples without infinite expansion', () => {
63 type ResultTuple = readonly [
64 ReadonlyArray<ResultTuple>,
65 { [key: string]: ResultTuple },
66 ]
67 expectTypeOf<
68 ValidateSerializableResult<ResultTuple, Serializable>
69 >().branded.toEqualTypeOf<ResultTuple>()
70 })
71​
72 it('should preserve readonly recursive arrays without infinite expansion', () => {
73 type ResultReadonlyArray = ReadonlyArray<
74 ResultReadonlyArray | { [key: string]: ResultReadonlyArray }
75 >
76 expectTypeOf<
77 ValidateSerializableResult<ResultReadonlyArray, Serializable>
78 >().branded.toEqualTypeOf<ResultReadonlyArray>()
79 })
80})