Files
Genarrative/miniprogram/shell/payment.test.js
T
kdletters bc3b464349 统一三端桥接层职责命名
微信小程序 host-bridge 与 shell 文件改为职责命名

移动壳 host-bridge 与 shell 文件改为职责命名

更新原生壳结构门禁、微信 smoke 和宿主壳文档
2026-06-18 17:48:03 +08:00

70 lines
2.1 KiB
JavaScript

import path from 'node:path';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { loadCommonJsModule } from '../test-utils/loadCommonJsModule.js';
const payBridgePath = path.resolve(
process.cwd(),
'miniprogram/host-bridge/payment.js',
);
const paymentShellPath = path.resolve(
process.cwd(),
'miniprogram/shell/payment.js',
);
describe('wechat payment shell page', () => {
let createWechatPayPage;
beforeEach(() => {
globalThis.wx = {
getSystemInfoSync: vi.fn(() => ({ SDKVersion: '2.32.0' })),
navigateBack: vi.fn(),
requestPayment: vi.fn(),
requestVirtualPayment: vi.fn(),
setStorageSync: vi.fn(),
};
globalThis.getCurrentPages = vi.fn(() => []);
const wechatPayBridge = loadCommonJsModule(payBridgePath);
const wechatPaymentShell = loadCommonJsModule(paymentShellPath, {
'../host-bridge/payment': wechatPayBridge,
});
createWechatPayPage = wechatPaymentShell.createWechatPayPage;
});
test('stores payment result and returns to previous web-view', async () => {
const previousPage = {
data: { webViewUrl: 'https://web.test/#tab=profile' },
setData: vi.fn(),
};
globalThis.getCurrentPages = vi.fn(() => [previousPage, {}]);
globalThis.wx.requestVirtualPayment.mockImplementationOnce((options) => {
options.success?.({ errMsg: 'requestVirtualPayment:ok' });
});
const page = createWechatPayPage({ setData: vi.fn() });
await page.onLoad({
requestId: 'request-1',
orderId: 'order-1',
payParams: encodeURIComponent(
JSON.stringify({
mode: 'short_series_coin',
signData: '{}',
paySig: 'pay-sig',
signature: 'user-sig',
}),
),
});
expect(globalThis.wx.setStorageSync).toHaveBeenCalledWith(
'genarrative:wechat-pay-result',
'request-1:success:order-1',
);
expect(previousPage.setData).toHaveBeenCalledWith({
webViewUrl:
'https://web.test/#tab=profile&wx_pay_result=request-1%3Asuccess%3Aorder-1',
});
expect(globalThis.wx.navigateBack).toHaveBeenCalled();
});
});