Files
WonderQ-Project/WonderQ-MiniAPP/tests/wechat-profile.test.ts
duanshuwen e7b8c5bf43 feat: 新增微信头像授权获取与个人中心展示功能
- 新增 `wechat-profile` 工具库,实现微信用户信息的本地存储、格式校验与授权获取逻辑
- 编写对应单元测试覆盖核心功能流程
- 重构个人中心成员组件,新增头像、昵称展示与授权触发按钮
- 更新个人中心页面,集成微信头像授权流程与状态管理
2026-08-20 21:58:08 +08:00

52 lines
1.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import {
getStoredWechatProfile,
requestWechatProfile,
saveWechatProfile,
} from "@/lib/wechat-profile";
describe("wechat profile", () => {
const storage = new Map<string, unknown>();
const getUserProfile = vi.fn();
beforeEach(() => {
storage.clear();
getUserProfile.mockReset();
vi.stubGlobal("uni", {
getStorageSync: (key: string) => storage.get(key),
setStorageSync: (key: string, value: unknown) => storage.set(key, value),
getUserProfile,
});
});
it("stores and restores a valid avatar profile", () => {
saveWechatProfile({ avatarUrl: "https://example.com/avatar.jpg", nickName: "Damon" });
expect(getStoredWechatProfile()).toEqual({
avatarUrl: "https://example.com/avatar.jpg",
nickName: "Damon",
});
});
it("requests the profile only through the explicit profile API", async () => {
getUserProfile.mockImplementation(({ success }: { success: (result: unknown) => void }) => {
success({ userInfo: { avatarUrl: "https://example.com/wechat.jpg", nickName: "旅行者" } });
});
await expect(requestWechatProfile()).resolves.toEqual({
avatarUrl: "https://example.com/wechat.jpg",
nickName: "旅行者",
});
expect(getUserProfile).toHaveBeenCalledWith(
expect.objectContaining({ provider: "weixin", lang: "zh_CN" }),
);
});
it("returns null when the user declines profile permission", async () => {
getUserProfile.mockImplementation(({ fail }: { fail: () => void }) => fail());
await expect(requestWechatProfile()).resolves.toBeNull();
});
});