๐Ÿ“ฆ cloudflare / vinext

๐Ÿ“„ prefetch.test.ts ยท 66 lines
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/**
 * Next.js compat: app-prefetch (HTTP-testable parts)
 *
 * Source: https://github.com/vercel/next.js/blob/canary/test/e2e/app-dir/app-prefetch/prefetching.test.ts
 *
 * Tests RSC prefetch endpoint behavior. Most prefetch tests are browser-only;
 * these test the server-side RSC response behavior.
 */
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import {
  startFixtureServer,
  APP_FIXTURE_DIR,
  fetchHtml,
  type TestServerResult,
} from "../helpers.js";

let ctx: TestServerResult;

describe("Next.js compat: prefetch", () => {
  beforeAll(async () => {
    ctx = await startFixtureServer(APP_FIXTURE_DIR, { appRouter: true });
  });

  afterAll(async () => {
    await ctx.server.close();
  });

  it("should serve RSC payload at .rsc endpoint", async () => {
    const res = await fetch(
      `${ctx.baseUrl}/nextjs-compat/prefetch-test/target.rsc`,
      {
        headers: { Accept: "text/x-component" },
      },
    );
    expect(res.status).toBe(200);
    const contentType = res.headers.get("content-type");
    expect(contentType).toContain("text/x-component");
  });

  it("should render prefetch page with links", async () => {
    const { html } = await fetchHtml(
      ctx.baseUrl,
      "/nextjs-compat/prefetch-test",
    );
    expect(html).toContain("Prefetch Test Home");
    expect(html).toContain('id="prefetch-link"');
    expect(html).toContain('id="no-prefetch-link"');
  });

  it("should navigate to target page (prefetch=true)", async () => {
    const { html } = await fetchHtml(
      ctx.baseUrl,
      "/nextjs-compat/prefetch-test/target",
    );
    expect(html).toContain("Prefetch Target Page");
  });

  it("should navigate to target page (prefetch=false)", async () => {
    const { html } = await fetchHtml(
      ctx.baseUrl,
      "/nextjs-compat/prefetch-test/no-prefetch",
    );
    expect(html).toContain("No Prefetch Target Page");
  });
});