Chore: homepage tests (#6278)

This commit is contained in:
shamoon
2026-02-04 19:58:39 -08:00
committed by GitHub
parent 7d019185a3
commit 872a3600aa
558 changed files with 32606 additions and 84 deletions

View File

@@ -0,0 +1,36 @@
import { vi } from "vitest";
export default function createMockRes() {
const res = {
statusCode: null,
body: null,
headers: {},
};
res.status = vi.fn((code) => {
res.statusCode = code;
return res;
});
res.json = vi.fn((body) => {
res.body = body;
return res;
});
res.send = vi.fn((body) => {
res.body = body;
return res;
});
res.end = vi.fn((body) => {
res.body = body;
return res;
});
res.setHeader = vi.fn((key, value) => {
res.headers[key] = value;
return res;
});
return res;
}

View File

@@ -0,0 +1,13 @@
import { render } from "@testing-library/react";
import { SettingsContext } from "utils/contexts/settings";
export function renderWithProviders(ui, { settings = {} } = {}) {
const value = {
settings,
// Most tests don't need to mutate settings; this keeps Container happy.
setSettings: () => {},
};
return render(<SettingsContext.Provider value={value}>{ui}</SettingsContext.Provider>);
}

View File

@@ -0,0 +1,4 @@
export function findServiceBlockByLabel(container, label) {
const blocks = Array.from(container.querySelectorAll(".service-block"));
return blocks.find((b) => b.textContent?.includes(label));
}

View File

@@ -0,0 +1,52 @@
import { expect } from "vitest";
export function expectWidgetConfigShape(widget) {
expect(widget).toBeTruthy();
expect(widget).toBeTypeOf("object");
if ("api" in widget) {
expect(widget.api).toBeTypeOf("string");
// Widget APIs are either service-backed (`{url}` template) or third-party API URLs.
expect(widget.api.includes("{url}") || /^https?:\/\//.test(widget.api)).toBe(true);
}
if ("proxyHandler" in widget) {
expect(widget.proxyHandler).toBeTypeOf("function");
}
if ("allowedEndpoints" in widget) {
expect(widget.allowedEndpoints).toBeInstanceOf(RegExp);
}
if ("mappings" in widget) {
expect(widget.mappings).toBeTruthy();
expect(widget.mappings).toBeTypeOf("object");
for (const [name, mapping] of Object.entries(widget.mappings)) {
expect(name).toBeTruthy();
expect(mapping).toBeTruthy();
expect(mapping).toBeTypeOf("object");
if ("endpoint" in mapping) {
expect(mapping.endpoint).toBeTypeOf("string");
expect(mapping.endpoint.length).toBeGreaterThan(0);
}
if ("map" in mapping) {
const map = mapping.map;
const proxyName = widget.proxyHandler?.name ?? "genericProxyHandler";
// Most handlers treat `map` as a transform function. A small number of custom
// proxies treat it as an options object.
expect(["function", "object"].includes(typeof map)).toBe(true);
if (typeof map === "object") {
expect(map).not.toBeNull();
expect(Array.isArray(map)).toBe(false);
// Generic handlers will call `map(resultData)`, so they must never receive an object.
expect(proxyName).not.toBe("genericProxyHandler");
expect(proxyName).not.toBe("credentialedProxyHandler");
}
}
}
}
}