Skip to content

Commit c5a04fc

Browse files
committed
chore: add basic server function test that runs on both bundlers
1 parent e7eb25a commit c5a04fc

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

integration/helpers/rsc-vite/framework/references.browser.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import {
22
createServerReference as createServerReferenceImp,
3+
createFromReadableStream,
4+
encodeReply,
35
// @ts-expect-error - no types yet
46
} from "@jacob-ebey/react-server-dom-vite/client";
7+
import { unstable_createCallServer as createCallServer } from "react-router";
58

6-
export async function callServer(id: string, args: unknown) {
7-
throw new Error("callServer not implemented");
8-
}
9+
export const callServer = createCallServer({
10+
decode: (body) => createFromReadableStream(body, { callServer }),
11+
encodeAction: (args) => encodeReply(args),
12+
});
913

1014
export function createServerReference(imp: unknown, id: string, name: string) {
1115
return createServerReferenceImp(`${id}#${name}`, callServer);

integration/rsc/rsc-test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,67 @@ implementations.forEach((implementation) => {
464464

465465
test.describe("Server Actions", () => {
466466
test("Supports React Server Functions", async ({ page }) => {
467+
let port = await getPort();
468+
stop = await setupRscTest({
469+
implementation,
470+
port,
471+
files: {
472+
"src/routes/home.actions.ts": js`
473+
"use server";
474+
475+
export function incrementCounter(count: number, formData: FormData) {
476+
return count + parseInt(formData.get("by") as string || "1", 10);
477+
}
478+
`,
479+
"src/routes/home.tsx": js`
480+
"use client";
481+
482+
import { useActionState } from "react";
483+
484+
import { incrementCounter } from "./home.actions";
485+
486+
export default function ClientComponent() {
487+
const [count, incrementCounterAction, incrementing] = useActionState(incrementCounter, 0);
488+
489+
return (
490+
<div>
491+
<h2 data-home>Home: ({count})</h2>
492+
<form action={incrementCounterAction}>
493+
<input type="hidden" name="name" value="Updated" />
494+
<button type="submit" data-submit>
495+
{incrementing ? "Updating via Server Function" : "Update via Server Function"}
496+
</button>
497+
</form>
498+
</div>
499+
);
500+
}
501+
`,
502+
},
503+
});
504+
505+
await page.goto(`http://localhost:${port}/`);
506+
507+
// Verify initial server render
508+
await page.waitForSelector("[data-home]");
509+
expect(await page.locator("[data-home]").textContent()).toBe(
510+
"Home: (0)"
511+
);
512+
513+
// Submit the form to trigger server function
514+
await page.click("[data-submit]");
515+
516+
// Verify server function updated the UI
517+
await expect(page.locator("[data-home]")).toHaveText("Home: (1)");
518+
519+
// Submit again to ensure server functions work repeatedly
520+
await page.click("[data-submit]");
521+
await expect(page.locator("[data-home]")).toHaveText("Home: (2)");
522+
523+
// Ensure this is using RSC
524+
validateRSCHtml(await page.content());
525+
});
526+
527+
test("Supports Inline React Server Functions", async ({ page }) => {
467528
// FIXME: Waiting on parcel support: https://github.com/parcel-bundler/parcel/pull/10165
468529
test.skip(
469530
implementation.name === "parcel",

0 commit comments

Comments
 (0)