mirror of
https://github.com/gethomepage/homepage.git
synced 2026-02-08 08:50:52 +08:00
Ok, make a reusable testing component
This commit is contained in:
41
src/components/services/widget/block.test.jsx
Normal file
41
src/components/services/widget/block.test.jsx
Normal file
@@ -0,0 +1,41 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import Block from "./block";
|
||||
import { BlockHighlightContext } from "./highlight-context";
|
||||
|
||||
import { renderWithProviders } from "test-utils/render-with-providers";
|
||||
|
||||
describe("components/services/widget/block", () => {
|
||||
it("renders a placeholder when value is undefined", () => {
|
||||
const { container } = renderWithProviders(<Block label="some.label" />, { settings: {} });
|
||||
|
||||
// Value slot is rendered as "-" while loading.
|
||||
expect(container.textContent).toContain("-");
|
||||
expect(container.textContent).toContain("some.label");
|
||||
});
|
||||
|
||||
it("sets highlight metadata when a rule matches", () => {
|
||||
const highlightConfig = {
|
||||
levels: { danger: "danger-class" },
|
||||
fields: {
|
||||
foo: {
|
||||
numeric: { when: "gt", value: 10, level: "danger" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const { container } = renderWithProviders(
|
||||
<BlockHighlightContext.Provider value={highlightConfig}>
|
||||
<Block label="foo.label" field="foo" value="11" />
|
||||
</BlockHighlightContext.Provider>,
|
||||
{ settings: {} },
|
||||
);
|
||||
|
||||
const el = container.querySelector(".service-block");
|
||||
expect(el).not.toBeNull();
|
||||
expect(el.getAttribute("data-highlight-level")).toBe("danger");
|
||||
expect(el.className).toContain("danger-class");
|
||||
});
|
||||
});
|
||||
53
src/components/services/widget/container.test.jsx
Normal file
53
src/components/services/widget/container.test.jsx
Normal file
@@ -0,0 +1,53 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { screen } from "@testing-library/react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { renderWithProviders } from "test-utils/render-with-providers";
|
||||
|
||||
import Container from "./container";
|
||||
|
||||
function Dummy({ label }) {
|
||||
return <div data-testid={label} />;
|
||||
}
|
||||
|
||||
describe("components/services/widget/container", () => {
|
||||
it("filters children based on widget.fields (auto-namespaced by widget type)", () => {
|
||||
renderWithProviders(
|
||||
<Container service={{ widget: { type: "omada", fields: ["connectedAp", "alerts"] } }}>
|
||||
<Dummy label="omada.connectedAp" />
|
||||
<Dummy label="omada.alerts" />
|
||||
<Dummy label="omada.activeUser" />
|
||||
</Container>,
|
||||
{ settings: {} },
|
||||
);
|
||||
|
||||
expect(screen.getByTestId("omada.connectedAp")).toBeInTheDocument();
|
||||
expect(screen.getByTestId("omada.alerts")).toBeInTheDocument();
|
||||
expect(screen.queryByTestId("omada.activeUser")).toBeNull();
|
||||
});
|
||||
|
||||
it("accepts widget.fields as a JSON string", () => {
|
||||
renderWithProviders(
|
||||
<Container service={{ widget: { type: "omada", fields: JSON.stringify(["alerts"]) } }}>
|
||||
<Dummy label="omada.connectedAp" />
|
||||
<Dummy label="omada.alerts" />
|
||||
</Container>,
|
||||
{ settings: {} },
|
||||
);
|
||||
|
||||
expect(screen.getByTestId("omada.alerts")).toBeInTheDocument();
|
||||
expect(screen.queryByTestId("omada.connectedAp")).toBeNull();
|
||||
});
|
||||
|
||||
it("supports aliased widget types when filtering (hoarder -> karakeep)", () => {
|
||||
renderWithProviders(
|
||||
<Container service={{ widget: { type: "hoarder", fields: ["hoarder.count"] } }}>
|
||||
<Dummy label="karakeep.count" />
|
||||
</Container>,
|
||||
{ settings: {} },
|
||||
);
|
||||
|
||||
expect(screen.getByTestId("karakeep.count")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user