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(); }); });