Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

TelemetryExportSettings.browser.test.tsx

83 lines3.0 KB

1import { render } from "../../../test/render";
2import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { userEvent } from "vite-plus/test/browser";
5​
6const { state, updateSettings } = vi.hoisted(() => ({
7 state: { observability: {} as typeof DEFAULT_SERVER_SETTINGS.observability },
8 updateSettings: vi.fn(async () => ({ _tag: "Success" as const, value: undefined })),
9}));
10​
11vi.mock(import("@effect/atom-react"), async (original) => ({
12 ...(await original()),
13 useAtomValue: (() => true) as never,
14}));
15vi.mock(import("../../state/use-atom-command"), async (original) => ({
16 ...(await original()),
17 useAtomCommand: ((command: unknown) =>
18 command === "updateSettings" ? updateSettings : vi.fn()) as never,
19}));
20vi.mock(import("../../state/server"), async (original) => {
21 const actual = await original();
22 return {
23 ...actual,
24 serverEnvironment: {
25 ...actual.serverEnvironment,
26 updateSettings: "updateSettings",
27 } as unknown as typeof actual.serverEnvironment,
28 };
29});
30vi.mock(import("./SettingsScopeContext"), async (original) => ({
31 ...(await original()),
32 useSettingsScope: (() => ({
33 scope: { kind: "environment" },
34 environment: {
35 environmentId: "env",
36 label: "MAC1",
37 serverConfig: { environment: { capabilities: {} } },
38 },
39 })) as never,
40}));
41vi.mock(import("./useScopedSettings"), async (original) => ({
42 ...(await original()),
43 useScopedSettings: ((select: (settings: typeof state) => unknown) => select(state)) as never,
44}));
45​
46import { TelemetryExportSettings } from "./TelemetryExportSettings";
47​
48function setup() {
49 updateSettings.mockClear();
51​
52describe("TelemetryExportSettings", () => {
53 it("does not resend a field the user restored after another client changed it", async () => {
54 setup();
55 state.observability = {
56 ...DEFAULT_SERVER_SETTINGS.observability,
57 otlpTracesUrl: "http://old/v1/traces",
58 };
59 await using screen = await render(<TelemetryExportSettings />);
60​
61 const { container } = screen;
62 const field = (id: string) => container.querySelector<HTMLInputElement>(`#${id}`)!;
63 async function replace(id: string, value: string) {
64 await userEvent.clear(field(id));
65 await userEvent.type(field(id), value);
66 }
67 await replace("otlpTracesUrl", "http://draft/v1/traces");
68 await replace("otlpTracesUrl", "http://old/v1/traces");
69 state.observability = { ...state.observability, otlpTracesUrl: "http://other/v1/traces" };
70 await screen.rerender(<TelemetryExportSettings />);
71 await replace("otlpMetricsUrl", "http://new/v1/metrics");
72 const save = [...container.querySelectorAll("button")].find(
73 (button) => button.textContent === "Save",
74 )!;
75 await userEvent.click(save);
76​
77 expect(updateSettings).toHaveBeenCalledExactlyOnceWith({
78 environmentId: "env",
79 input: { patch: { observability: { otlpMetricsUrl: "http://new/v1/metrics" } } },
80 });
81 expect(container.textContent).toContain("Saved. Restart the server to apply.");
82 });
83});