feat: error if stateful hook is used outside of islands (#1778)

This commit is contained in:
Marvin Hagemeister
2023-09-18 16:25:37 +02:00
committed by GitHub
parent 112d701e5c
commit f9f3cd82ee
9 changed files with 258 additions and 124 deletions
+46
View File
@@ -1,7 +1,10 @@
import { assertEquals, assertStringIncludes } from "./deps.ts";
import { Status } from "../server.ts";
import {
assertNotSelector,
assertSelector,
assertTextMany,
assertTextMatch,
fetchHtml,
waitForStyle,
withFresh,
@@ -75,3 +78,46 @@ Deno.test("middleware destination internal", async () => {
await resp.body?.cancel();
});
});
Deno.test("warns when using hooks in server components", async (t) => {
await withFresh("./tests/fixture/main.ts", async (address) => {
await t.step("useState", async () => {
const doc = await fetchHtml(`${address}/hooks-server/useState`);
assertTextMatch(doc, "p", /Hook "useState" cannot be used/);
// Check for hint
assertTextMatch(doc, "p", /Instead, use the "useSignal" hook/);
});
await t.step("useReducer", async () => {
const doc = await fetchHtml(`${address}/hooks-server/useReducer`);
assertTextMatch(doc, "p", /Hook "useReducer" cannot be used/);
});
// Valid
await t.step("does not warn in island", async () => {
const doc = await fetchHtml(`${address}/hooks-server/island`);
assertTextMany(doc, "p", ["0"]);
});
});
});
Deno.test("shows custom 500 page for rendering errors when not in dev", async (t) => {
await withFresh({
name: "./tests/fixture/main.ts",
options: {
env: {
DENO_DEPLOYMENT_ID: "foo",
},
},
}, async (address) => {
await t.step("useState", async () => {
const doc = await fetchHtml(`${address}/hooks-server/useState`);
assertNotSelector(doc, "pre");
});
await t.step("useReducer", async () => {
const doc = await fetchHtml(`${address}/hooks-server/useReducer`);
assertNotSelector(doc, "pre");
});
});
});