-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathindex.tsx
More file actions
235 lines (222 loc) 路 5.57 KB
/
Copy pathindex.tsx
File metadata and controls
235 lines (222 loc) 路 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { createFileRoute, deepEqual } from '@tanstack/react-router'
import React from 'react'
import { createServerFn } from '@tanstack/react-start'
import { fooFnInsideFactoryFile } from './-functions/createFooServerFn'
import {
barFn,
barFnPOST,
composedFn,
fakeFn,
fooFn,
fooFnPOST,
localFn,
localFnPOST,
nestedReexportedFactoryFn,
reexportedFactoryFn,
starReexportedFactoryFn,
} from './-functions/functions'
export const Route = createFileRoute('/factory/')({
ssr: false,
component: RouteComponent,
})
const fnInsideRoute = createServerFn({ method: 'GET' }).handler(() => {
return {
name: 'fnInsideRoute',
}
})
const functions = {
fnInsideRoute: {
fn: fnInsideRoute,
type: 'serverFn',
expected: {
name: 'fnInsideRoute',
},
},
fooFnInsideFactoryFile: {
fn: fooFnInsideFactoryFile,
type: 'serverFn',
expected: {
name: 'fooFnInsideFactoryFile',
context: { foo: 'foo', method: 'GET' },
},
},
fooFn: {
fn: fooFn,
type: 'serverFn',
expected: {
name: 'fooFn',
context: { foo: 'foo', method: 'GET' },
},
},
fooFnPOST: {
fn: fooFnPOST,
type: 'serverFn',
expected: {
name: 'fooFnPOST',
context: { foo: 'foo', method: 'POST' },
},
},
barFn: {
fn: barFn,
type: 'serverFn',
expected: {
name: 'barFn',
context: { foo: 'foo', method: 'GET', bar: 'bar' },
},
},
barFnPOST: {
fn: barFnPOST,
type: 'serverFn',
expected: {
name: 'barFnPOST',
context: { foo: 'foo', method: 'POST', bar: 'bar' },
},
},
localFn: {
fn: localFn,
type: 'serverFn',
expected: {
name: 'localFn',
context: {
foo: 'foo',
method: 'GET',
bar: 'bar',
local: 'local',
another: 'another',
},
},
},
localFnPOST: {
fn: localFnPOST,
type: 'serverFn',
expected: {
name: 'localFnPOST',
context: {
foo: 'foo',
method: 'POST',
bar: 'bar',
local: 'local',
another: 'another',
},
},
},
composedFn: {
fn: composedFn,
type: 'serverFn',
expected: {
name: 'composedFn',
context: {
foo: 'foo',
method: 'GET',
bar: 'bar',
another: 'another',
local: 'local',
},
},
},
fakeFn: {
fn: fakeFn,
type: 'localFn',
expected: {
name: 'fakeFn',
window,
},
},
// Test that re-exported factories (using `export { foo } from './module'`) work correctly
// The middleware from reexportFactory should execute and add { reexport: 'reexport-middleware-executed' } to context
reexportedFactoryFn: {
fn: reexportedFactoryFn,
type: 'serverFn',
expected: {
name: 'reexportedFactoryFn',
context: { reexport: 'reexport-middleware-executed' },
},
},
// Test that star re-exported factories (using `export * from './module'`) work correctly
// The middleware from starReexportFactory should execute and add { starReexport: 'star-reexport-middleware-executed' } to context
starReexportedFactoryFn: {
fn: starReexportedFactoryFn,
type: 'serverFn',
expected: {
name: 'starReexportedFactoryFn',
context: { starReexport: 'star-reexport-middleware-executed' },
},
},
// Test that nested star re-exported factories (A -> B -> C chain) work correctly
// The middleware from nestedReexportFactory should execute and add { nested: 'nested-middleware-executed' } to context
nestedReexportedFactoryFn: {
fn: nestedReexportedFactoryFn,
type: 'serverFn',
expected: {
name: 'nestedReexportedFactoryFn',
context: { nested: 'nested-middleware-executed' },
},
},
} satisfies Record<string, TestCase>
interface TestCase {
fn: () => Promise<any>
expected: any
type: 'serverFn' | 'localFn'
}
function Test({ fn, type, expected }: TestCase) {
const [result, setResult] = React.useState<null | unknown>(null)
function comparison() {
if (result) {
const isEqual = deepEqual(result, expected)
return isEqual ? 'equal' : 'not equal'
}
return 'Loading...'
}
return (
<div
data-testid={`test-${expected.name}`}
className="p-2 border border-gray-200 rounded-md"
>
<h2 className="font-bold text-lg"></h2>
<div>
It should return{' '}
<code>
<pre data-testid={`expected-fn-result-${expected.name}`}>
{type === 'serverFn' ? JSON.stringify(expected) : 'localFn'}
</pre>
</code>
</div>
<p>
fn returns:
<br />
<span data-testid={`fn-result-${expected.name}`}>
{result
? type === 'serverFn'
? JSON.stringify(result)
: 'localFn'
: 'Loading...'}
</span>{' '}
<span data-testid={`fn-comparison-${expected.name}`}>
{comparison()}
</span>
</p>
<button
data-testid={`btn-fn-${expected.name}`}
type="button"
className="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-xs ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
onClick={() => {
fn().then(setResult)
}}
>
Invoke Server Function
</button>
</div>
)
}
function RouteComponent() {
return (
<div className="p-2 m-2 grid gap-2" data-testid="factory-route-component">
<h1 className="font-bold text-lg">
Server functions middleware E2E tests
</h1>
{Object.entries(functions).map(([name, testCase]) => (
<Test key={name} {...testCase} />
))}
</div>
)
}