fix: align miniprogram web-view auth and share

This commit is contained in:
kdletters
2026-06-04 23:23:07 +08:00
parent 2678954627
commit dd5861d5f5
3 changed files with 91 additions and 25 deletions

View File

@@ -17,6 +17,8 @@ type MiniProgramPage = {
data: Record<string, unknown>;
setData: (patch: Record<string, unknown>) => void;
onLoad: (query?: Record<string, string>) => Promise<void>;
onShareAppMessage: () => Record<string, unknown>;
onShareTimeline: () => Record<string, unknown>;
onShow: () => void;
consumePayResult: () => void;
};
@@ -29,6 +31,7 @@ function createWxMock() {
navigateBack: vi.fn(),
removeStorageSync: vi.fn(),
request: vi.fn(),
showShareMenu: vi.fn(),
setStorageSync: vi.fn(),
};
}
@@ -91,7 +94,7 @@ describe('mini-program web-view auth page', () => {
vi.clearAllMocks();
});
test('默认进入时刷新微信小程序登录态后打开 web-view', async () => {
test('默认进入时不预登录,直接打开未登录 web-view', async () => {
const wxMock = createWxMock();
wxMock.login.mockImplementation(({ success }) => {
success({ code: 'wx-login-code' });
@@ -109,19 +112,58 @@ describe('mini-program web-view auth page', () => {
await page.onLoad({});
expect(wxMock.login).toHaveBeenCalledTimes(1);
expect(wxMock.request).toHaveBeenCalledWith(
expect.objectContaining({
url: 'https://www.genarrative.world/api/auth/wechat/miniprogram-login',
method: 'POST',
data: { code: 'wx-login-code' },
}),
);
expect(wxMock.login).not.toHaveBeenCalled();
expect(wxMock.request).not.toHaveBeenCalled();
expect(page.data.loading).toBe(false);
expect(page.data.phoneBindingRequired).toBe(false);
expect(page.data.webViewUrl).toBe(
'https://www.genarrative.world/?clientType=mini_program&clientRuntime=wechat_mini_program#auth_provider=wechat&auth_token=jwt-active-wechat&auth_binding_status=active',
'https://www.genarrative.world/?clientType=mini_program&clientRuntime=wechat_mini_program',
);
expect(wxMock.showShareMenu).toHaveBeenCalledWith({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline'],
});
});
test('默认进入时即便微信新身份待绑手机号,也不弹出绑定手机号页', async () => {
const wxMock = createWxMock();
wxMock.login.mockImplementation(({ success }) => {
success({ code: 'wx-login-code' });
});
wxMock.request.mockImplementation(({ success }) => {
success({
statusCode: 200,
data: {
token: 'jwt-pending-wechat',
bindingStatus: 'pending_bind_phone',
},
});
});
const page = loadWebViewPage(wxMock);
await page.onLoad({});
expect(wxMock.login).not.toHaveBeenCalled();
expect(wxMock.request).not.toHaveBeenCalled();
expect(page.data.loading).toBe(false);
expect(page.data.phoneBindingRequired).toBe(false);
expect(page.data.webViewUrl).toBe(
'https://www.genarrative.world/?clientType=mini_program&clientRuntime=wechat_mini_program',
);
});
test('web-view 页面分享好友和朋友圈都回到小程序 web-view 入口', () => {
const wxMock = createWxMock();
const page = loadWebViewPage(wxMock);
expect(page.onShareAppMessage()).toEqual({
title: '陶泥儿',
path: '/pages/web-view/index',
});
expect(page.onShareTimeline()).toEqual({
title: '陶泥儿',
query: '',
});
});
test('默认匿名进入 web-view 仍不依赖 API_BASE_URL 配置', async () => {