Open History to see the last commit
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
18}
19β
20type SplitHydrateProps = CommonHydrateProps & {
21 when: HydrationStrategy | (() => HydrationStrategy)
22 prefetch?: never
23 split?: boolean
24}
25β
26type PrefetchHydrateProps = CommonHydrateProps & {
27 when: HydrationStrategy | (() => HydrationStrategy)
28 prefetch: HydrationPrefetchStrategy
29 split?: true
30}
31β
32type FunctionPrefetchHydrateProps = CommonHydrateProps & {
33 when: HydrationStrategy | (() => HydrationStrategy)
34 prefetch: HydrationPrefetchFunction
35 split?: boolean
36}
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})