From 2f237c80cf19d4440b720e133c9573b47eab228f Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Thu, 23 Jul 2026 18:56:24 +0800 Subject: [PATCH] =?UTF-8?q?test(web):=20=E6=B7=BB=E5=8A=A0WechatCallback?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E5=9F=BA=E7=A1=80=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/pages/auth/WechatCallback.test.tsx | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 apps/web/src/test/pages/auth/WechatCallback.test.tsx diff --git a/apps/web/src/test/pages/auth/WechatCallback.test.tsx b/apps/web/src/test/pages/auth/WechatCallback.test.tsx new file mode 100644 index 000000000..abedadec1 --- /dev/null +++ b/apps/web/src/test/pages/auth/WechatCallback.test.tsx @@ -0,0 +1,72 @@ +import { describe, expect, it, vi, beforeEach } from "vitest" +import { render, screen } from "@testing-library/react" +import { MemoryRouter } from "react-router-dom" +import WechatCallback from "@/pages/auth/WechatCallback" + +// 重置 localStorage +beforeEach(() => { + vi.spyOn(Storage.prototype, "getItem").mockReturnValue(null) + vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => {}) + vi.spyOn(Storage.prototype, "removeItem").mockImplementation(() => {}) +}) + +vi.mock("react-router-dom", async () => { + const actual = await vi.importActual("react-router-dom") + return { + ...actual, + useNavigate: () => vi.fn(), + useSearchParams: () => [new URLSearchParams()], + } +}) + +vi.mock("@/api/auth", () => ({ + wechatCallback: vi.fn(), + getCurrentUser: vi.fn(), + normalizeUser: (u: unknown) => u, +})) + +vi.mock("@/store/authStore", () => ({ + useAuthStore: () => ({ + setAuth: vi.fn(), + }), +})) + +vi.mock("@/components/auth/BindContactModal", () => ({ + default: ({ open }: { open: boolean }) => ( +
+ BindContactModal +
+ ), +})) + +vi.mock("antd", async () => { + const actual = await vi.importActual("antd") + return { + ...actual, + message: { + success: vi.fn(), + error: vi.fn(), + }, + } +}) + +describe("WechatCallback Page", () => { + it("should render without crashing", () => { + const { container } = render( + + + , + ) + expect(container).toBeTruthy() + }) + + it("should show loading state initially", () => { + render( + + + , + ) + // 初始状态应该显示加载中 + expect(screen.getByText("正在登录...")).toBeTruthy() + }) +})