Skip to content

Commit

Permalink
#119 tests: Add tests for useGetDevices
Browse files Browse the repository at this point in the history
  • Loading branch information
TWilkin committed Jun 13, 2024
1 parent 4f683a1 commit 12432fe
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions services/ui/src/hooks/useGetDevices.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { DeviceState, PowerPiApi } from "@powerpi/common-api";
import { ConfigFileType } from "@powerpi/common-api/dist/src/ConfigStatus";
import { QueryClient } from "react-query";
import { capture, instance, mock, resetCalls, when } from "ts-mockito";
import { renderHook, waitFor } from "../test-setup";
import { useGetDevices } from "./useGetDevices";

describe("useGetDevices", () => {
const api = mock<PowerPiApi>();

beforeEach(() => resetCalls(api));

test("gets data", async () => {
const devices = [
{
name: "Device",
display_name: "Device",
type: "socket",
since: 10,
state: DeviceState.Off,
visible: true,
},
];

when(api.getDevices()).thenResolve(devices);

const { result } = renderHook(useGetDevices, {
api: instance(api),
});

await waitFor(() => expect(result.current.devices).toStrictEqual(devices));
});

describe("onConfigChange", () => {
const queryClient = new QueryClient();
queryClient.invalidateQueries = jest.fn();

const getListener = () => capture(api.addConfigChangeListener).first()[0];

beforeEach(() => jest.resetAllMocks());

test("expected config type", () => {
renderHook(useGetDevices, {
api: instance(api),
queryClient: queryClient,
});

const listener = getListener();

listener({ type: ConfigFileType.Devices });

expect(queryClient.invalidateQueries).toHaveBeenCalledWith(["powerpi", "devices"]);
});

test("wrong config type", () => {
renderHook(useGetDevices, {
api: instance(api),
queryClient: queryClient,
});

const listener = getListener();

listener({ type: ConfigFileType.Floorplan });

expect(queryClient.invalidateQueries).not.toHaveBeenCalled();
});
});
});

0 comments on commit 12432fe

Please sign in to comment.