mirror of
https://github.com/gethomepage/homepage.git
synced 2026-04-04 01:01:22 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
60
src/widgets/plex/component.test.jsx
Normal file
60
src/widgets/plex/component.test.jsx
Normal file
@@ -0,0 +1,60 @@
|
||||
// @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/plex/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders placeholders while loading", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
|
||||
|
||||
const { container } = renderWithProviders(<Component service={{ widget: { type: "plex" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||
expect(screen.getByText("plex.streams")).toBeInTheDocument();
|
||||
expect(screen.getByText("plex.albums")).toBeInTheDocument();
|
||||
expect(screen.getByText("plex.movies")).toBeInTheDocument();
|
||||
expect(screen.getByText("plex.tv")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders error UI when endpoint errors", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: { message: "nope" } });
|
||||
|
||||
renderWithProviders(<Component service={{ widget: { type: "plex" } }} />, { settings: { hideErrors: false } });
|
||||
|
||||
expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
|
||||
expect(screen.getByText("nope")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders plex unified counts when loaded", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: { streams: 1, albums: 2, movies: 3, tv: 4 }, error: undefined });
|
||||
|
||||
const { container } = renderWithProviders(<Component service={{ widget: { type: "plex" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expectBlockValue(container, "plex.streams", 1);
|
||||
expectBlockValue(container, "plex.albums", 2);
|
||||
expectBlockValue(container, "plex.movies", 3);
|
||||
expectBlockValue(container, "plex.tv", 4);
|
||||
});
|
||||
});
|
||||
93
src/widgets/plex/proxy.test.js
Normal file
93
src/widgets/plex/proxy.test.js
Normal file
@@ -0,0 +1,93 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import createMockRes from "test-utils/create-mock-res";
|
||||
|
||||
const { httpProxy, getServiceWidget, cache, xml2json, logger } = vi.hoisted(() => {
|
||||
const store = new Map();
|
||||
return {
|
||||
httpProxy: vi.fn(),
|
||||
getServiceWidget: vi.fn(),
|
||||
cache: {
|
||||
get: vi.fn((k) => (store.has(k) ? store.get(k) : null)),
|
||||
put: vi.fn((k, v) => store.set(k, v)),
|
||||
del: vi.fn((k) => store.delete(k)),
|
||||
_reset: () => store.clear(),
|
||||
},
|
||||
xml2json: vi.fn((xml) => {
|
||||
if (xml === "sessions") return JSON.stringify({ MediaContainer: { _attributes: { size: "2" } } });
|
||||
if (xml === "libraries")
|
||||
return JSON.stringify({
|
||||
MediaContainer: {
|
||||
Directory: [
|
||||
{ _attributes: { type: "movie", key: "1" } },
|
||||
{ _attributes: { type: "show", key: "2" } },
|
||||
{ _attributes: { type: "artist", key: "3" } },
|
||||
],
|
||||
},
|
||||
});
|
||||
if (xml === "movies") return JSON.stringify({ MediaContainer: { _attributes: { size: "10" } } });
|
||||
if (xml === "tv") return JSON.stringify({ MediaContainer: { _attributes: { totalSize: "20" } } });
|
||||
if (xml === "albums") return JSON.stringify({ MediaContainer: { _attributes: { size: "30" } } });
|
||||
return JSON.stringify({ MediaContainer: { _attributes: { size: "0" } } });
|
||||
}),
|
||||
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("xml-js", () => ({
|
||||
xml2json,
|
||||
}));
|
||||
vi.mock("widgets/widgets", () => ({
|
||||
default: {
|
||||
plex: {
|
||||
api: "{url}{endpoint}",
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import plexProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/plex/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
cache._reset();
|
||||
});
|
||||
|
||||
it("fetches sessions and library counts, caching intermediate results", async () => {
|
||||
getServiceWidget.mockResolvedValue({ type: "plex", url: "http://plex" });
|
||||
|
||||
httpProxy
|
||||
// sessions
|
||||
.mockResolvedValueOnce([200, "application/xml", Buffer.from("sessions")])
|
||||
// libraries
|
||||
.mockResolvedValueOnce([200, "application/xml", Buffer.from("libraries")])
|
||||
// movies
|
||||
.mockResolvedValueOnce([200, "application/xml", Buffer.from("movies")])
|
||||
// tv
|
||||
.mockResolvedValueOnce([200, "application/xml", Buffer.from("tv")])
|
||||
// albums
|
||||
.mockResolvedValueOnce([200, "application/xml", Buffer.from("albums")]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await plexProxyHandler(req, res);
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toEqual({ streams: "2", albums: 30, movies: 10, tv: 20 });
|
||||
expect(cache.put).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
11
src/widgets/plex/widget.test.js
Normal file
11
src/widgets/plex/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("plex widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user