Open History to see the last commit
1import { For, createSignal } from 'solid-js'
2import { Link, createFileRoute, deepEqual } from '@tanstack/solid-router'
3import {
4 getServerFnCallingPost,
5 postServerFnCallingGet,
6} from './-functions/serverFnCallingServerFn'
7β
8export const Route = createFileRoute('/function-method/')({
9 component: RouteComponent,
10})
11β
12const functions = {
13 getServerFnCallingPost: {
14 fn: getServerFnCallingPost,
15 expected: {
16 name: 'getServerFnCallingPost',
17 method: 'GET',
18 innerFnResult: {
19 method: 'POST',
20 },
21 },
22 },
23 postServerFnCallingGet: {
24 fn: postServerFnCallingGet,
25 expected: {
26 name: 'postServerFnCallingGet',
27 method: 'POST',
28 innerFnResult: {
29 method: 'GET',
30 },
31 },
32 },
33} satisfies Record<string, TestCase>
34β
35function RouteComponent() {
36 return (
37 <div class="p-2 m-2 grid gap-2" data-testid="method-route-component">
38 <h1 class="font-bold text-lg">Server functions methods E2E tests</h1>
39 <div>
40 <Link class="inline" to="/factory">
41 <h2>Go to Factory Functions and request method E2E test</h2>
42 </Link>
43 </div>
44 <For each={Object.entries(functions)}>
45 {([, testCase]) => <Test {...testCase} />}
46 </For>
47 </div>
48 )
49}
50β
51interface TestCase {
52 fn: () => Promise<any>
53 expected: any
54}
55function Test({ fn, expected }: TestCase) {
56 const [result, setResult] = createSignal<null | unknown>(null)
57 function comparison() {
58 if (result()) {
59 const isEqual = deepEqual(result(), expected)
60 return isEqual ? 'equal' : 'not equal'
61 }
62 return 'Loading...'
63 }
64β
65 return (
66 <div
67 data-testid={`test-${expected.name}`}
68 class="p-2 border border-gray-200 rounded-md"
69 >
70 <div>
71 It should return{' '}
72 <code>
73 <pre data-testid={`expected-fn-result-${expected.name}`}>
74 {JSON.stringify(expected)}
75 </pre>
76 </code>
77 </div>
78 <p>
79 fn returns:
80 <br />
81 <span data-testid={`fn-result-${expected.name}`}>
82 {result() ? JSON.stringify(result()) : 'Loading...'}
83 </span>{' '}
84 <span data-testid={`fn-comparison-${expected.name}`}>
85 {comparison()}
86 </span>
87 </p>
88 <button
89 data-testid={`btn-fn-${expected.name}`}
90 type="button"
91 class="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"
92 onClick={() => {
93 fn().then(setResult)
94 }}
95 >
96 Invoke Server Function
97 </button>
98 </div>
99 )
100}