๐Ÿ“ฆ cloudflare / vinext

๐Ÿ“„ ssr.spec.ts ยท 81 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81import { test, expect } from "@playwright/test";

const BASE = "http://localhost:4176";

test.describe("Cloudflare Workers SSR", () => {
  test("home page renders server-side HTML", async ({ page }) => {
    await page.goto(`${BASE}/`);

    await expect(page.locator("h1")).toHaveText(
      "vinext on Cloudflare Workers",
    );
    await expect(page.locator("p").first()).toContainText(
      "server-rendered by vinext",
    );
  });

  test("SSR HTML is present without JavaScript", async ({ page }) => {
    // Block all JS to verify SSR output
    await page.route("**/*.js", (route) => route.abort());

    await page.goto(`${BASE}/`);

    await expect(page.locator("h1")).toHaveText(
      "vinext on Cloudflare Workers",
    );
    // Counter should show initial state from SSR
    await expect(page.locator('[data-testid="count"]')).toHaveText("Count: 0");
    // Timestamp should be present (server-rendered)
    const timestamp = await page.textContent('[data-testid="timestamp"]');
    expect(timestamp).toContain("Rendered at:");
  });

  test("about page renders correctly", async ({ page }) => {
    await page.goto(`${BASE}/about`);

    await expect(page.locator("h1")).toHaveText("About");
    await expect(page.locator("p").first()).toContainText(
      "vinext app deployed on Cloudflare Workers",
    );
  });

  test("each request gets a fresh server render (dynamic timestamp)", async ({
    page,
  }) => {
    await page.goto(`${BASE}/`);
    const ts1 = await page.textContent('[data-testid="timestamp"]');

    // Small delay to ensure different timestamp
    await page.waitForTimeout(50);
    await page.goto(`${BASE}/`);
    const ts2 = await page.textContent('[data-testid="timestamp"]');

    // Timestamps should be different (not cached/static)
    expect(ts1).not.toBe(ts2);
  });

  test("unknown routes return 404", async ({ page }) => {
    const response = await page.goto(`${BASE}/nonexistent-page`);
    expect(response?.status()).toBe(404);
  });

  test("root layout wraps pages with html/head/body", async ({ page }) => {
    await page.goto(`${BASE}/`);

    // Check html tag has lang
    const lang = await page.getAttribute("html", "lang");
    expect(lang).toBe("en");

    // Check title is set
    const title = await page.title();
    expect(title).toBe("vinext on Cloudflare Workers");

    // Check meta viewport
    const viewport = await page.getAttribute(
      'meta[name="viewport"]',
      "content",
    );
    expect(viewport).toBe("width=device-width, initial-scale=1");
  });
});