mirror of
https://github.com/gethomepage/homepage.git
synced 2026-04-03 16:51:20 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
54
src/widgets/dispatcharr/component.test.jsx
Normal file
54
src/widgets/dispatcharr/component.test.jsx
Normal file
@@ -0,0 +1,54 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { screen } from "@testing-library/react";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { renderWithProviders } from "test-utils/render-with-providers";
|
||||
import { findServiceBlockByLabel } from "test-utils/widget-assertions";
|
||||
|
||||
const { useWidgetAPI } = vi.hoisted(() => ({ useWidgetAPI: vi.fn() }));
|
||||
vi.mock("utils/proxy/use-widget-api", () => ({ default: useWidgetAPI }));
|
||||
|
||||
import Component from "./component";
|
||||
|
||||
function expectBlockValue(container, label, value) {
|
||||
const block = findServiceBlockByLabel(container, label);
|
||||
expect(block, `missing block for ${label}`).toBeTruthy();
|
||||
expect(block.textContent).toContain(String(value));
|
||||
}
|
||||
|
||||
describe("widgets/dispatcharr/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders placeholders while loading", () => {
|
||||
useWidgetAPI.mockImplementation(() => ({ data: undefined, error: undefined }));
|
||||
|
||||
const { container } = renderWithProviders(<Component service={{ widget: { type: "dispatcharr" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(2);
|
||||
expect(screen.getByText("dispatcharr.channels")).toBeInTheDocument();
|
||||
expect(screen.getByText("dispatcharr.streams")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders counts and stream entries when enabled", () => {
|
||||
useWidgetAPI.mockReturnValueOnce({ data: [{}, {}, {}], error: undefined }).mockReturnValueOnce({
|
||||
data: {
|
||||
count: 1,
|
||||
channels: [{ stream_name: "Stream1", clients: [{}, {}], avg_bitrate: "1000kbps" }],
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
const service = { widget: { type: "dispatcharr", enableActiveStreams: true } };
|
||||
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||
|
||||
expectBlockValue(container, "dispatcharr.channels", 3);
|
||||
expectBlockValue(container, "dispatcharr.streams", 1);
|
||||
expect(screen.getByText(/Stream1 - Clients: 2/)).toBeInTheDocument();
|
||||
expect(screen.getByText("1000kbps")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
108
src/widgets/dispatcharr/proxy.test.js
Normal file
108
src/widgets/dispatcharr/proxy.test.js
Normal file
@@ -0,0 +1,108 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import createMockRes from "test-utils/create-mock-res";
|
||||
|
||||
const { httpProxy, getServiceWidget, cache, logger } = vi.hoisted(() => {
|
||||
const store = new Map();
|
||||
|
||||
return {
|
||||
httpProxy: vi.fn(),
|
||||
getServiceWidget: vi.fn(),
|
||||
cache: {
|
||||
get: vi.fn((k) => store.get(k)),
|
||||
put: vi.fn((k, v) => store.set(k, v)),
|
||||
del: vi.fn((k) => store.delete(k)),
|
||||
_reset: () => store.clear(),
|
||||
},
|
||||
logger: {
|
||||
debug: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("utils/logger", () => ({
|
||||
default: () => logger,
|
||||
}));
|
||||
|
||||
vi.mock("utils/config/service-helpers", () => ({
|
||||
default: getServiceWidget,
|
||||
}));
|
||||
|
||||
vi.mock("utils/proxy/http", () => ({
|
||||
httpProxy,
|
||||
}));
|
||||
|
||||
vi.mock("memory-cache", () => ({
|
||||
default: cache,
|
||||
...cache,
|
||||
}));
|
||||
|
||||
vi.mock("widgets/widgets", () => ({
|
||||
default: {
|
||||
dispatcharr: {
|
||||
api: "{url}/{endpoint}",
|
||||
mappings: {
|
||||
token: { endpoint: "auth/token" },
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import dispatcharrProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/dispatcharr/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
cache._reset();
|
||||
});
|
||||
|
||||
it("logs in when token is missing and uses Bearer token for requests", async () => {
|
||||
getServiceWidget.mockResolvedValue({
|
||||
type: "dispatcharr",
|
||||
url: "http://dispatcharr",
|
||||
username: "u",
|
||||
password: "p",
|
||||
});
|
||||
|
||||
httpProxy
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ access: "t1" }))])
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from("data")]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", endpoint: "items", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await dispatcharrProxyHandler(req, res);
|
||||
|
||||
expect(httpProxy).toHaveBeenCalledTimes(2);
|
||||
expect(httpProxy.mock.calls[0][0].toString()).toBe("http://dispatcharr/auth/token");
|
||||
expect(httpProxy.mock.calls[1][1].headers.Authorization).toBe("Bearer t1");
|
||||
expect(res.body).toEqual(Buffer.from("data"));
|
||||
});
|
||||
|
||||
it("retries after a bad response by clearing cache and logging in again", async () => {
|
||||
cache.put("dispatcharrProxyHandler__token.svc", "old");
|
||||
|
||||
getServiceWidget.mockResolvedValue({
|
||||
type: "dispatcharr",
|
||||
url: "http://dispatcharr",
|
||||
username: "u",
|
||||
password: "p",
|
||||
});
|
||||
|
||||
httpProxy
|
||||
.mockResolvedValueOnce([400, "application/json", Buffer.from(JSON.stringify({ items: [] }))])
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ access: "new" }))])
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from("ok")]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", endpoint: "items", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await dispatcharrProxyHandler(req, res);
|
||||
|
||||
expect(httpProxy).toHaveBeenCalledTimes(3);
|
||||
expect(httpProxy.mock.calls[1][0].toString()).toBe("http://dispatcharr/auth/token");
|
||||
expect(httpProxy.mock.calls[2][1].headers.Authorization).toBe("Bearer new");
|
||||
expect(res.body).toEqual(Buffer.from("ok"));
|
||||
});
|
||||
});
|
||||
11
src/widgets/dispatcharr/widget.test.js
Normal file
11
src/widgets/dispatcharr/widget.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { describe, it } from "vitest";
|
||||
|
||||
import { expectWidgetConfigShape } from "test-utils/widget-config";
|
||||
|
||||
import widget from "./widget";
|
||||
|
||||
describe("dispatcharr widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user