mirror of
https://github.com/gethomepage/homepage.git
synced 2026-10-01 08:21:18 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
// @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";
|
||||
|
||||
const { useWidgetAPI } = vi.hoisted(() => ({ useWidgetAPI: vi.fn() }));
|
||||
vi.mock("utils/proxy/use-widget-api", () => ({ default: useWidgetAPI }));
|
||||
|
||||
import Component from "./component";
|
||||
|
||||
describe("widgets/homeassistant/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders error UI when widget API errors", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: { message: "nope" } });
|
||||
|
||||
renderWithProviders(<Component service={{ widget: { type: "homeassistant", url: "http://x" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
|
||||
expect(screen.getByText("nope")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders blocks returned from the API", () => {
|
||||
useWidgetAPI.mockReturnValue({
|
||||
data: [
|
||||
{ label: "ha.temp", value: "72" },
|
||||
{ label: "ha.mode", value: "cool" },
|
||||
],
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
renderWithProviders(<Component service={{ widget: { type: "homeassistant", url: "http://x" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(screen.getByText("ha.temp")).toBeInTheDocument();
|
||||
expect(screen.getByText("72")).toBeInTheDocument();
|
||||
expect(screen.getByText("ha.mode")).toBeInTheDocument();
|
||||
expect(screen.getByText("cool")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import createMockRes from "test-utils/create-mock-res";
|
||||
|
||||
const { httpProxy, getServiceWidget, logger } = vi.hoisted(() => ({
|
||||
httpProxy: vi.fn(),
|
||||
getServiceWidget: vi.fn(),
|
||||
logger: { debug: vi.fn() },
|
||||
}));
|
||||
|
||||
vi.mock("utils/logger", () => ({
|
||||
default: () => logger,
|
||||
}));
|
||||
vi.mock("utils/config/service-helpers", () => ({
|
||||
default: getServiceWidget,
|
||||
}));
|
||||
vi.mock("utils/proxy/http", () => ({
|
||||
httpProxy,
|
||||
}));
|
||||
|
||||
import homeassistantProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/homeassistant/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("returns 400 when custom JSON cannot be parsed", async () => {
|
||||
getServiceWidget.mockResolvedValue({ url: "http://hass", key: "k", custom: "{not-json" });
|
||||
|
||||
const req = { query: { group: "g", service: "svc", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await homeassistantProxyHandler(req, res);
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(res.body).toEqual({ error: "Error parsing widget custom label" });
|
||||
});
|
||||
|
||||
it("runs default template queries and returns label/value pairs", async () => {
|
||||
getServiceWidget.mockResolvedValue({ url: "http://hass", key: "k" });
|
||||
httpProxy
|
||||
.mockResolvedValueOnce([200, "text/plain", Buffer.from("1 / 2")])
|
||||
.mockResolvedValueOnce([200, "text/plain", Buffer.from("3 / 4")])
|
||||
.mockResolvedValueOnce([200, "text/plain", Buffer.from("5 / 6")]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await homeassistantProxyHandler(req, res);
|
||||
|
||||
expect(httpProxy).toHaveBeenCalledTimes(3);
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toEqual([
|
||||
{ label: "homeassistant.people_home", value: "1 / 2" },
|
||||
{ label: "homeassistant.lights_on", value: "3 / 4" },
|
||||
{ label: "homeassistant.switches_on", value: "5 / 6" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import { describe, it } from "vitest";
|
||||
|
||||
import { expectWidgetConfigShape } from "test-utils/widget-config";
|
||||
|
||||
import widget from "./widget";
|
||||
|
||||
describe("homeassistant widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user