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

Hydrate.test-d.tsx

147 lines4.1 KB

1import { expectTypeOf, test } from 'vitest'
2import { visible } from '../hydration'
3import { Hydrate } from '../Hydrate'
4import type {
5 HydrateOptions,
6 HydrateProps,
7 HydrationPrefetchFunction,
8 HydrationPrefetchStrategy,
9 HydrationStrategy,
10} from '../Hydrate'
11import type { HydrationStrategy as CoreHydrationStrategy } from '@tanstack/start-client-core/hydration'
12import type { JSX } from 'solid-js'
13​
14type CommonHydrateProps = {
15 fallback?: JSX.Element
16 onHydrated?: () => void
17 children: JSX.Element
19​
20type SplitHydrateProps = CommonHydrateProps & {
21 when: HydrationStrategy | (() => HydrationStrategy)
22 prefetch?: never
23 split?: boolean
25​
26type PrefetchHydrateProps = CommonHydrateProps & {
27 when: HydrationStrategy | (() => HydrationStrategy)
28 prefetch: HydrationPrefetchStrategy
29 split?: true
31​
32type FunctionPrefetchHydrateProps = CommonHydrateProps & {
33 when: HydrationStrategy | (() => HydrationStrategy)
34 prefetch: HydrationPrefetchFunction
35 split?: boolean
37​
38test('Hydrate component accepts the public HydrateProps type', () => {
39 expectTypeOf(Hydrate).toBeFunction()
40 expectTypeOf(Hydrate).parameter(0).branded.toEqualTypeOf<HydrateProps>()
41})
42​
43test('HydrateOptions supports reusable spread props', () => {
44 const belowFoldProps = {
45 when: () => visible({ rootMargin: '800px' }),
46 } satisfies HydrateOptions
47​
48 expectTypeOf(belowFoldProps).toMatchTypeOf<HydrateOptions>()
49​
50 const withFunctionPrefetch = {
51 when: visible(),
52 split: false,
53 prefetch: (ctx) => {
54 expectTypeOf(ctx.element).toEqualTypeOf<Element | null>()
55 expectTypeOf(ctx.signal).toEqualTypeOf<AbortSignal>()
56 expectTypeOf(ctx.preload).returns.toEqualTypeOf<Promise<void>>()
57 expectTypeOf(ctx.waitFor).returns.toEqualTypeOf<
58 Promise<'prefetch' | 'hydrate' | 'abort'>
59 >()
60 },
61 } satisfies HydrateOptions
62​
63 expectTypeOf(withFunctionPrefetch).toMatchTypeOf<HydrateOptions>()
64})
65​
66test('Hydrate props are exact for strategy and prefetch forms', () => {
67 expectTypeOf<
68 Extract<HydrateProps, { prefetch?: never }>
69 >().branded.toEqualTypeOf<SplitHydrateProps>()
70 expectTypeOf<
71 Extract<HydrateProps, { prefetch: HydrationPrefetchStrategy }>
72 >().branded.toEqualTypeOf<PrefetchHydrateProps>()
73 expectTypeOf<
74 Extract<HydrateProps, { prefetch: HydrationPrefetchFunction }>
75 >().branded.toEqualTypeOf<FunctionPrefetchHydrateProps>()
76})
77​
78test('Hydrate requires a strategy', () => {
79 expectTypeOf<{
80 when: HydrationStrategy
81 children: JSX.Element
82 }>().toMatchTypeOf<HydrateProps>()
83​
84 expectTypeOf<{
85 when: () => HydrationStrategy
86 children: JSX.Element
87 }>().toMatchTypeOf<HydrateProps>()
88​
89 expectTypeOf<{
90 children: JSX.Element
91 }>().not.toMatchTypeOf<HydrateProps>()
92​
93 expectTypeOf<{
94 when: () => true
95 children: JSX.Element
96 }>().not.toMatchTypeOf<HydrateProps>()
97​
98 expectTypeOf<{
99 when: false
100 children: JSX.Element
101 }>().not.toMatchTypeOf<HydrateProps>()
102})
103​
104test('Hydrate requires a framework-renderable strategy', () => {
105 expectTypeOf<CoreHydrationStrategy>().not.toMatchTypeOf<HydrationStrategy>()
106 expectTypeOf<ReturnType<typeof visible>>().toMatchTypeOf<HydrationStrategy>()
107​
108 expectTypeOf<{
109 when: CoreHydrationStrategy
110 children: JSX.Element
111 }>().not.toMatchTypeOf<HydrateProps>()
112})
113​
114test('Hydrate enforces prefetch only with split boundaries', () => {
115 expectTypeOf<{
116 when: HydrationStrategy
117 prefetch: HydrationPrefetchStrategy
118 children: JSX.Element
119 }>().toMatchTypeOf<HydrateProps>()
120​
121 expectTypeOf<{
122 when: HydrationStrategy
123 prefetch: HydrationPrefetchStrategy
124 split: true
125 children: JSX.Element
126 }>().toMatchTypeOf<HydrateProps>()
127​
128 expectTypeOf<{
129 when: HydrationStrategy
130 prefetch: HydrationPrefetchStrategy
131 split: false
132 children: JSX.Element
133 }>().not.toMatchTypeOf<HydrateProps>()
134​
135 expectTypeOf<{
136 when: HydrationStrategy
137 prefetch: HydrationPrefetchFunction
138 split: false
139 children: JSX.Element
140 }>().toMatchTypeOf<HydrateProps>()
141​
142 expectTypeOf<{
143 when: HydrationStrategy
144 prefetch: HydrationPrefetchFunction
145 children: JSX.Element
146 }>().toMatchTypeOf<HydrateProps>()
147})