Update test format (#821)

* Lint tests, format JSON

* Update test formatting

---------

Co-authored-by: philon- <philon-@users.noreply.github.com>
This commit is contained in:
Jeremy Gallant
2025-08-31 21:30:11 -04:00
committed by GitHub
co-authored by philon-
parent ee131c3681
commit 90cf136b8c
13 changed files with 124 additions and 93 deletions
@@ -1,8 +1,7 @@
import { DeleteMessagesDialog } from "@components/Dialog/DeleteMessagesDialog/DeleteMessagesDialog.tsx";
import { useMessageStore } from "@core/stores";
import { fireEvent, render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
// Ensure the path is correct for import
import { useMessageStore } from "@core/stores";
vi.mock("@core/stores", () => ({
useMessageStore: vi.fn(() => ({
@@ -1,4 +1,4 @@
import { fireEvent, render, screen, act } from "@testing-library/react";
import { act, fireEvent, render, screen } from "@testing-library/react";
import type {
ButtonHTMLAttributes,
ClassAttributes,
@@ -11,10 +11,12 @@ import { RebootDialog } from "./RebootDialog.tsx";
const rebootMock = vi.fn();
const rebootOtaMock = vi.fn();
let mockConnection: {
rebootOta: (delay: number) => void,
reboot: (delay: number) => void
} | undefined = {
let mockConnection:
| {
rebootOta: (delay: number) => void;
reboot: (delay: number) => void;
}
| undefined = {
reboot: rebootMock,
rebootOta: rebootOtaMock,
};
@@ -82,42 +84,44 @@ describe("RebootDialog", () => {
expect(
screen.getByRole("heading", { name: /reboot device/i, level: 1 }),
).toBeInTheDocument();
expect(screen.getByRole("button", { name: /reboot now/i })).toBeInTheDocument();
expect(
screen.getByRole("button", { name: /reboot now/i }),
).toBeInTheDocument();
});
it("calls correct reboot function based on OTA checkbox state", () => {
render(<RebootDialog open onOpenChange={() => {}} />);
it("calls correct reboot function based on OTA checkbox state", () => {
render(<RebootDialog open onOpenChange={() => {}} />);
// Schedule non-OTA reboot
act(() => {
fireEvent.click(screen.getByTestId("scheduleRebootBtn"));
});
expect(rebootMock).toHaveBeenCalledWith(5);
expect(rebootOtaMock).not.toHaveBeenCalled();
rebootMock.mockClear();
rebootOtaMock.mockClear();
// Cancel scheduled
act(() => {
fireEvent.click(screen.getByTestId("cancelRebootBtn"));
});
expect(rebootMock).toHaveBeenCalledWith(-1);
expect(rebootOtaMock).not.toHaveBeenCalled();
rebootMock.mockClear();
rebootOtaMock.mockClear();
// Schedule OTA reboot
act(() => {
fireEvent.click(screen.getByText(/reboot into ota mode/i));
});
act(() => {
fireEvent.click(screen.getByTestId("scheduleRebootBtn"));
});
expect(rebootOtaMock).toHaveBeenCalledWith(5);
expect(rebootMock).not.toHaveBeenCalled();
// Schedule non-OTA reboot
act(() => {
fireEvent.click(screen.getByTestId("scheduleRebootBtn"));
});
expect(rebootMock).toHaveBeenCalledWith(5);
expect(rebootOtaMock).not.toHaveBeenCalled();
rebootMock.mockClear();
rebootOtaMock.mockClear();
// Cancel scheduled
act(() => {
fireEvent.click(screen.getByTestId("cancelRebootBtn"));
});
expect(rebootMock).toHaveBeenCalledWith(-1);
expect(rebootOtaMock).not.toHaveBeenCalled();
rebootMock.mockClear();
rebootOtaMock.mockClear();
// Schedule OTA reboot
act(() => {
fireEvent.click(screen.getByText(/reboot into ota mode/i));
});
act(() => {
fireEvent.click(screen.getByTestId("scheduleRebootBtn"));
});
expect(rebootOtaMock).toHaveBeenCalledWith(5);
expect(rebootMock).not.toHaveBeenCalled();
});
it("schedules a reboot with delay and calls rebootOta", async () => {
const onOpenChangeMock = vi.fn();
@@ -132,7 +136,7 @@ describe("RebootDialog", () => {
act(() => {
fireEvent.click(screen.getByTestId("scheduleRebootBtn"));
});
expect(rebootMock).toHaveBeenCalledWith(3);
expect(screen.getByText(/reboot has been scheduled/i)).toBeInTheDocument();
@@ -142,7 +146,6 @@ describe("RebootDialog", () => {
});
expect(onOpenChangeMock).toHaveBeenCalledWith(false);
});
it("triggers an instant reboot", async () => {
@@ -196,6 +199,8 @@ describe("RebootDialog", () => {
fireEvent.click(screen.getByRole("button", { name: /cancel/i }));
});
expect(rebootMock).toHaveBeenCalledWith(-1);
expect(screen.queryByText(/reboot has been scheduled/i)).not.toBeInTheDocument();
expect(
screen.queryByText(/reboot has been scheduled/i),
).not.toBeInTheDocument();
});
});
@@ -1,11 +1,17 @@
import { CurrentDeviceContext, useDeviceStore, useMessageStore } from "@core/stores";
import {
CurrentDeviceContext,
useDeviceStore,
useMessageStore,
} from "@core/stores";
import { render } from "@testing-library/react";
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { RefreshKeysDialog } from "./RefreshKeysDialog.tsx";
import { useRefreshKeysDialog } from "./useRefreshKeysDialog.ts";
vi.mock("@core/stores", async () => {
const actual = (await vi.importActual("@core/stores")) as typeof import("@core/stores");
const actual = (await vi.importActual(
"@core/stores",
)) as typeof import("@core/stores");
return {
...actual,
useMessageStore: vi.fn(),
@@ -38,7 +38,7 @@ const mockDevice = {
vi.mock("@core/stores", () => ({
CurrentDeviceContext: {
_currentValue: { deviceId: 123 },
_currentValue: { deviceId: 123 },
},
useDevice: () => ({
setDialogOpen: mockDevice.setDialogOpen,
@@ -1,6 +1,6 @@
import { TraceRoute } from "@components/PageComponents/Messages/TraceRoute.tsx";
import { mockNodeDBStore } from "@core/stores/nodeDBStore/nodeDBStore.mock.ts";
import { useNodeDB } from "@core/stores";
import { mockNodeDBStore } from "@core/stores/nodeDBStore/nodeDBStore.mock.ts";
import { Protobuf } from "@meshtastic/core";
import { render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
@@ -64,7 +64,6 @@ describe("TraceRoute", () => {
]);
beforeEach(() => {
vi.resetAllMocks();
vi.mocked(useNodeDB).mockReturnValue({
...mockNodeDBStore,
@@ -58,7 +58,9 @@ describe("Checkbox", () => {
it("controlled: reflects external prop changes after onChange", () => {
const onChange = vi.fn();
const { rerender } = render(<Checkbox checked={false} onChange={onChange} />);
const { rerender } = render(
<Checkbox checked={false} onChange={onChange} />,
);
const checkbox = screen.getByRole("checkbox");
const presentation = screen.getByRole("presentation");
@@ -79,7 +81,9 @@ describe("Checkbox", () => {
it("renders children inside the label", () => {
render(<Checkbox>Test Label</Checkbox>);
expect(screen.getByTestId("label-component")).toHaveTextContent("Test Label");
expect(screen.getByTestId("label-component")).toHaveTextContent(
"Test Label",
);
});
it("applies custom className to wrapper label", () => {
@@ -112,7 +116,10 @@ describe("Checkbox", () => {
it("passes through additional props to the input", () => {
render(<Checkbox data-testid="extra-prop" />);
expect(screen.getByRole("checkbox")).toHaveAttribute("data-testid", "extra-prop");
expect(screen.getByRole("checkbox")).toHaveAttribute(
"data-testid",
"extra-prop",
);
});
it("uncontrolled: toggles checked state when clicking the visual box", () => {
@@ -18,7 +18,7 @@ const mockSendAdminMessage = vi.fn();
vi.mock("@core/stores", () => ({
CurrentDeviceContext: {
_currentValue: { deviceId: 1234 },
_currentValue: { deviceId: 1234 },
},
useNodeDB: () => ({
updateFavorite: mockUpdateFavorite,
@@ -17,8 +17,8 @@ const mockToast = vi.fn();
const mockSendAdminMessage = vi.fn();
vi.mock("@core/stores", () => ({
CurrentDeviceContext: {
_currentValue: { deviceId: 1234 },
CurrentDeviceContext: {
_currentValue: { deviceId: 1234 },
},
useNodeDB: () => ({
updateIgnore: mockUpdateIgnore,
@@ -28,7 +28,7 @@ describe("useToast", () => {
vi.runAllTimers();
});
const toast = result.current.toasts[0];
const toast = result.current.toasts[0]!;
expect(result.current.toasts.length).toBe(1);
expect(toast.title).toBe("Backup Reminder");
expect(toast.description).toBe("Don't forget to backup!");
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import * as idb from "idb-keyval";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createStorage } from "./indexDB";
type PersistStorage<T> = ReturnType<typeof createStorage<T>>;
@@ -109,7 +109,7 @@ describe("indexDB.ts persistence (steps 1–5)", () => {
const out = await roundTrip({ state: { m }, version: 0 });
const m2 = out!.state.m as Map<number, { key: Uint8Array }>;
expect(m2 instanceof Map).toBe(true);
const got = m2.get(42)!;
expect(got.key instanceof Uint8Array).toBe(true);
expect(Array.from(got.key)).toEqual([7, 8]);
@@ -155,4 +155,4 @@ describe("indexDB.ts persistence (steps 1–5)", () => {
expect(m2 instanceof Map).toBe(true);
expect(m2.get(1)).toBe("x");
});
});
});