hyapp-admin-platform/src/features/dashboard/hooks/useDashboardPage.test.jsx
2026-07-12 00:47:34 +08:00

75 lines
2.3 KiB
JavaScript

import { renderHook } from "@testing-library/react";
import { afterEach, beforeEach, expect, test, vi } from "vitest";
const mocks = vi.hoisted(() => ({
appCode: "lalu",
getDashboardUserProfileOverview: vi.fn(),
queryFn: null,
queryOptions: null,
reload: vi.fn(),
timeZone: "Asia/Shanghai"
}));
vi.mock("@/app/app-scope/AppScopeProvider.jsx", () => ({
useAppScope: () => ({ appCode: mocks.appCode })
}));
vi.mock("@/app/timezone/TimeZoneProvider.jsx", () => ({
useTimeZone: () => ({ timeZone: mocks.timeZone })
}));
vi.mock("@/features/dashboard/api", () => ({
getDashboardUserProfileOverview: mocks.getDashboardUserProfileOverview
}));
vi.mock("@/shared/hooks/useAdminQuery.js", () => ({
useAdminQuery: (queryFn, options) => {
mocks.queryFn = queryFn;
mocks.queryOptions = options;
return { data: null, error: "", loading: false, reload: mocks.reload };
}
}));
import { useDashboardPage } from "./useDashboardPage.js";
beforeEach(() => {
mocks.appCode = "lalu";
mocks.timeZone = "Asia/Shanghai";
mocks.getDashboardUserProfileOverview.mockResolvedValue(null);
});
afterEach(() => {
vi.restoreAllMocks();
vi.clearAllMocks();
});
test("isolates the query by App and time zone and requests the current local day", async () => {
const now = Date.UTC(2026, 6, 11, 2, 30, 0);
vi.spyOn(Date, "now").mockReturnValue(now);
const { rerender } = renderHook(() => useDashboardPage());
expect(mocks.queryOptions.queryKey).toEqual(["dashboard", "user-profile-overview", "lalu", "Asia/Shanghai"]);
expect(mocks.queryOptions.refetchInterval()).toBe(Date.UTC(2026, 6, 11, 16, 0, 1) - now);
await mocks.queryFn();
expect(mocks.getDashboardUserProfileOverview).toHaveBeenLastCalledWith({
appCode: "lalu",
endMs: now,
startMs: Date.UTC(2026, 6, 10, 16, 0, 0),
statTz: "Asia/Shanghai"
});
mocks.appCode = "huwaa";
mocks.timeZone = "UTC";
rerender();
expect(mocks.queryOptions.queryKey).toEqual(["dashboard", "user-profile-overview", "huwaa", "UTC"]);
expect(mocks.queryOptions.refetchInterval()).toBe(Date.UTC(2026, 6, 12, 0, 0, 1) - now);
await mocks.queryFn();
expect(mocks.getDashboardUserProfileOverview).toHaveBeenLastCalledWith({
appCode: "huwaa",
endMs: now,
startMs: Date.UTC(2026, 6, 11, 0, 0, 0),
statTz: "UTC"
});
});