7609c37d5f
新增 miniprogram/shell 承接微信 Page 生命周期与 wx 容器调用 收窄微信页面入口为 shell 页面工厂装配 更新三端 host-bridge/shell 目录门禁与认证 smoke 补充微信 shell 测试与 CommonJS 测试加载工具 同步宿主壳架构文档和项目共享记忆 补齐 api-server 外部生成测试 fixture 更新时间字段
107 lines
3.2 KiB
JavaScript
107 lines
3.2 KiB
JavaScript
import path from 'node:path';
|
|
|
|
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
|
|
|
import { loadCommonJsModule } from '../test-utils/loadCommonJsModule.js';
|
|
|
|
const TEST_TEMPLATE_ID = 'm5z7BkkBhJGbcH0cdDeHaeRU2tViDEguP38XdrRRCdU';
|
|
|
|
const subscribeBridgePath = path.resolve(
|
|
process.cwd(),
|
|
'miniprogram/host-bridge/wechatHostBridgeSubscribeMessage.js',
|
|
);
|
|
|
|
describe('subscribe-message mini program bridge', () => {
|
|
let subscribeMessageBridge;
|
|
|
|
beforeEach(() => {
|
|
globalThis.wx = {
|
|
requestSubscribeMessage: vi.fn(),
|
|
setStorageSync: vi.fn(),
|
|
navigateBack: vi.fn(),
|
|
};
|
|
globalThis.getCurrentPages = vi.fn(() => []);
|
|
subscribeMessageBridge = loadCommonJsModule(subscribeBridgePath);
|
|
});
|
|
|
|
test('requests subscribe message and stores result before returning', () => {
|
|
const {
|
|
SUBSCRIBE_RESULT_STORAGE_KEY,
|
|
createSubscribeMessagePageController,
|
|
} = subscribeMessageBridge;
|
|
const previousPage = {
|
|
data: { webViewUrl: 'https://web.test/#tab=create' },
|
|
setData: vi.fn(),
|
|
};
|
|
globalThis.getCurrentPages = vi.fn(() => [previousPage, {}]);
|
|
globalThis.wx.requestSubscribeMessage.mockImplementationOnce((options) => {
|
|
options.success?.({
|
|
m5z7BkkBhJGbcH0cdDeHaeRU2tViDEguP38XdrRRCdU: 'accept',
|
|
});
|
|
});
|
|
const page = createSubscribeMessagePageController(
|
|
{
|
|
setData: vi.fn(),
|
|
},
|
|
{ templateId: TEST_TEMPLATE_ID },
|
|
);
|
|
page.onLoad({ requestId: 'request-1' });
|
|
|
|
page.requestSubscribe();
|
|
|
|
expect(globalThis.wx.requestSubscribeMessage).toHaveBeenCalledWith({
|
|
tmplIds: [TEST_TEMPLATE_ID],
|
|
success: expect.any(Function),
|
|
fail: expect.any(Function),
|
|
});
|
|
expect(globalThis.wx.setStorageSync).toHaveBeenCalledWith(
|
|
SUBSCRIBE_RESULT_STORAGE_KEY,
|
|
'request-1:success',
|
|
);
|
|
expect(previousPage.setData).not.toHaveBeenCalled();
|
|
expect(globalThis.wx.navigateBack).toHaveBeenCalled();
|
|
});
|
|
|
|
test('skip action notifies previous web-view', () => {
|
|
const {
|
|
SUBSCRIBE_RESULT_STORAGE_KEY,
|
|
createSubscribeMessagePageController,
|
|
} = subscribeMessageBridge;
|
|
const previousPage = {
|
|
data: { webViewUrl: 'https://web.test/' },
|
|
setData: vi.fn(),
|
|
};
|
|
globalThis.getCurrentPages = vi.fn(() => [previousPage, {}]);
|
|
const page = createSubscribeMessagePageController(
|
|
{
|
|
setData: vi.fn(),
|
|
},
|
|
{ templateId: TEST_TEMPLATE_ID },
|
|
);
|
|
page.onLoad({ requestId: 'request-skip' });
|
|
|
|
page.handleSkip();
|
|
|
|
expect(globalThis.wx.setStorageSync).toHaveBeenCalledWith(
|
|
SUBSCRIBE_RESULT_STORAGE_KEY,
|
|
'request-skip:skip:user_skip',
|
|
);
|
|
expect(previousPage.setData).not.toHaveBeenCalled();
|
|
expect(globalThis.wx.navigateBack).toHaveBeenCalled();
|
|
});
|
|
|
|
test('appendSubscribeResult replaces stale subscribe hash', () => {
|
|
const { appendSubscribeResult, buildSubscribeResultValue } =
|
|
subscribeMessageBridge;
|
|
expect(
|
|
appendSubscribeResult(
|
|
'https://web.test/#old=1&wx_subscribe_result=old',
|
|
'req:skip',
|
|
),
|
|
).toBe('https://web.test/#old=1&wx_subscribe_result=req%3Askip');
|
|
expect(buildSubscribeResultValue('req-1', 'skip', 'user_cancel')).toBe(
|
|
'req-1:skip:user_cancel',
|
|
);
|
|
});
|
|
});
|