mirror of
https://github.com/gethomepage/homepage.git
synced 2026-02-07 16:30:52 +08:00
Chore: homepage tests (#6278)
This commit is contained in:
36
src/test-utils/create-mock-res.js
Normal file
36
src/test-utils/create-mock-res.js
Normal 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;
|
||||
}
|
||||
13
src/test-utils/render-with-providers.jsx
Normal file
13
src/test-utils/render-with-providers.jsx
Normal 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>);
|
||||
}
|
||||
4
src/test-utils/widget-assertions.js
Normal file
4
src/test-utils/widget-assertions.js
Normal 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));
|
||||
}
|
||||
52
src/test-utils/widget-config.js
Normal file
52
src/test-utils/widget-config.js
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user