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

createServerComponent.test-d.tsx

98 lines3.1 KB

1import { expectTypeOf, test } from 'vitest'
2import type {
3 CompositeComponentResult,
4 ValidateCompositeComponent,
5} from '../src/ServerComponentTypes'
6import { CompositeComponent } from '../src/CompositeComponent'
7​
8import { JSX } from 'react'
9​
10test('when a server component is created with no props', () => {
11 type Post = () => React.ReactNode
12 type PostComponent = CompositeComponentResult<Post>
13​
14 const node = null as unknown as JSX.Element
15 expectTypeOf(node).toEqualTypeOf<JSX.Element>()
16})
17​
18test('when a server component is created with props but no slots', () => {
19 type Post = (props: { num: number }) => React.ReactNode
20 type PostComponent = CompositeComponentResult<Post>
21​
22 const node = null as unknown as JSX.Element
23 expectTypeOf(node).toEqualTypeOf<JSX.Element>()
24 expectTypeOf(null as unknown as typeof CompositeComponent<PostComponent>)
25 .parameter(0)
26 .branded.toEqualTypeOf<{ src: PostComponent; num: number }>()
27})
28​
29test('when a server component is created with child slots and props', () => {
30 type Post = (props: {
31 children: React.ReactNode
32 num: number
33 }) => React.ReactNode
34 type PostComponent = CompositeComponentResult<Post>
35​
36 const node = null as unknown as JSX.Element
37 expectTypeOf(node).toEqualTypeOf<JSX.Element>()
38 expectTypeOf(null as unknown as typeof CompositeComponent<PostComponent>)
39 .parameter(0)
40 .branded.toEqualTypeOf<{
41 src: PostComponent
42 num: number
43 children: React.ReactNode
44 }>()
45})
46​
47test('when a server component is created with slots with no props', () => {
48 type Post = (props: { render: () => React.ReactNode }) => React.ReactNode
49 type PostComponent = CompositeComponentResult<Post>
50​
51 const node = null as unknown as JSX.Element
52 expectTypeOf(node).toEqualTypeOf<JSX.Element>()
53​
54 expectTypeOf(null as unknown as typeof CompositeComponent<PostComponent>)
55 .parameter(0)
56 .branded.toEqualTypeOf<{
57 src: PostComponent
58 render: () => React.ReactNode
59 }>()
60})
61​
62test('when a server component is created with slots with serializable props', () => {
63 type Post = (props: {
64 render: (index: number) => React.ReactNode
65 }) => React.ReactNode
66​
67 type PostComponent = CompositeComponentResult<Post>
68​
69 const node = null as unknown as JSX.Element
70 expectTypeOf(node).toEqualTypeOf<JSX.Element>()
71​
72 expectTypeOf(null as unknown as typeof CompositeComponent<PostComponent>)
73 .parameter(0)
74 .branded.toEqualTypeOf<{
75 src: PostComponent
76 render: (index: number) => React.ReactNode
77 }>()
78})
79​
80test('when a server component is created with slots with not serializable props', () => {
81 type Post = (props: {
82 render: (props: { func: () => void }) => React.ReactNode
83 }) => React.ReactNode
84​
85 expectTypeOf<ValidateCompositeComponent<Post>>().toEqualTypeOf<
86 (props: {
87 render: (props: {
88 func: 'Function is not serializable'
89 }) => React.ReactNode
90 }) => React.ReactNode
91 >()
92})
93​
94test('when server component type has incorrect return type', () => {
95 expectTypeOf<ValidateCompositeComponent<() => void>>().toEqualTypeOf<
96 (props: unknown) => React.ReactNode
97 >()
98})