forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-config.client.test.ts
More file actions
54 lines (48 loc) · 1.88 KB
/
Copy pathruntime-config.client.test.ts
File metadata and controls
54 lines (48 loc) · 1.88 KB
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
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { getRuntimeConfig } from "./runtime-config.client";
describe("client getRuntimeConfig (shell)", () => {
const originalWindow = globalThis.window;
beforeEach(() => {
// jsdom provides `window` by default in this vitest config.
// Reset any prior injection.
delete (globalThis.window as Window & { __SHOWCASE_CONFIG__?: unknown })
.__SHOWCASE_CONFIG__;
});
afterEach(() => {
(globalThis as { window?: Window }).window = originalWindow;
});
it("returns the injected config", () => {
(window as Window & { __SHOWCASE_CONFIG__?: unknown }).__SHOWCASE_CONFIG__ =
{
baseUrl: "https://showcase.example.com",
posthogHost: "https://eu.i.posthog.com",
};
expect(getRuntimeConfig()).toEqual({
baseUrl: "https://showcase.example.com",
posthogHost: "https://eu.i.posthog.com",
});
});
it("throws when __SHOWCASE_CONFIG__ is missing (wiring bug)", () => {
expect(() => getRuntimeConfig()).toThrow(
/window\.__SHOWCASE_CONFIG__ is missing/,
);
});
it("returns SSR sentinel placeholder when window is undefined", () => {
// Simulate SSR by removing window. "use client" component
// bodies execute on the server during SSR, so this reader
// MUST be SSR-safe (returns parseable-URL placeholders so
// `new URL()` in consumers doesn't throw) and NOT throw —
// otherwise the whole server-rendered HTML 500s.
const w = globalThis.window;
// @ts-expect-error — deliberately removing window for the test
delete globalThis.window;
try {
const cfg = getRuntimeConfig();
// URL fields must be parseable.
expect(() => new URL(cfg.baseUrl)).not.toThrow();
expect(() => new URL(cfg.posthogHost)).not.toThrow();
} finally {
(globalThis as { window?: typeof w }).window = w;
}
});
});