- 新增 `wechat-profile` 工具库,实现微信用户信息的本地存储、格式校验与授权获取逻辑 - 编写对应单元测试覆盖核心功能流程 - 重构个人中心成员组件,新增头像、昵称展示与授权触发按钮 - 更新个人中心页面,集成微信头像授权流程与状态管理
52 lines
1.6 KiB
TypeScript
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();
|
|
});
|
|
});
|