177 lines
5.6 KiB
JavaScript
177 lines
5.6 KiB
JavaScript
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
|
|
|
import wechatPayBridge from './index.shared.js';
|
|
|
|
const {
|
|
appendPayResult,
|
|
createWechatPayPage,
|
|
parsePayParams,
|
|
requestWechatPayment,
|
|
} = wechatPayBridge;
|
|
|
|
describe('wechat-pay mini program payment bridge', () => {
|
|
beforeEach(() => {
|
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
globalThis.wx = {
|
|
requestPayment: vi.fn(),
|
|
requestVirtualPayment: vi.fn(),
|
|
getSystemInfoSync: vi.fn(() => ({ SDKVersion: '2.32.0' })),
|
|
setStorageSync: vi.fn(),
|
|
navigateBack: vi.fn(),
|
|
};
|
|
globalThis.getCurrentPages = vi.fn(() => []);
|
|
});
|
|
|
|
test('routes virtual payloads to wx.requestVirtualPayment', async () => {
|
|
globalThis.wx.requestVirtualPayment.mockImplementationOnce((options) => {
|
|
options.success?.({ errMsg: 'requestVirtualPayment:ok' });
|
|
});
|
|
const payParams = {
|
|
mode: 'short_series_coin',
|
|
signData:
|
|
'{"offerId":"offer-1","buyQuantity":1,"env":0,"currencyType":"CNY","outTradeNo":"order-virtual-1","attach":"mud_points_60"}',
|
|
paySig: 'pay-sig',
|
|
signature: 'user-sig',
|
|
};
|
|
|
|
const result = await requestWechatPayment(payParams);
|
|
|
|
expect(result).toEqual({ status: 'success', errorMessage: '' });
|
|
expect(globalThis.wx.requestVirtualPayment).toHaveBeenCalledWith({
|
|
mode: 'short_series_coin',
|
|
signData: payParams.signData,
|
|
paySig: 'pay-sig',
|
|
signature: 'user-sig',
|
|
success: expect.any(Function),
|
|
fail: expect.any(Function),
|
|
});
|
|
expect(globalThis.wx.requestPayment).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('routes goods virtual payloads to wx.requestVirtualPayment', async () => {
|
|
globalThis.wx.requestVirtualPayment.mockImplementationOnce((options) => {
|
|
options.success?.({ errMsg: 'requestVirtualPayment:ok' });
|
|
});
|
|
const payParams = {
|
|
mode: 'short_series_goods',
|
|
signData:
|
|
'{"offerId":"offer-1","buyQuantity":1,"env":0,"currencyType":"CNY","productId":"member_month","goodsPrice":2800,"outTradeNo":"order-goods-1","attach":"member_month"}',
|
|
paySig: 'pay-sig',
|
|
signature: 'user-sig',
|
|
};
|
|
|
|
const result = await requestWechatPayment(payParams);
|
|
|
|
expect(result).toEqual({ status: 'success', errorMessage: '' });
|
|
expect(globalThis.wx.requestVirtualPayment).toHaveBeenCalledWith({
|
|
mode: 'short_series_goods',
|
|
signData: payParams.signData,
|
|
paySig: 'pay-sig',
|
|
signature: 'user-sig',
|
|
success: expect.any(Function),
|
|
fail: expect.any(Function),
|
|
});
|
|
expect(globalThis.wx.requestPayment).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('keeps ordinary requestPayment payloads on wx.requestPayment', async () => {
|
|
globalThis.wx.requestPayment.mockImplementationOnce((options) => {
|
|
options.success?.();
|
|
});
|
|
|
|
const result = await requestWechatPayment({
|
|
timeStamp: '1777110165',
|
|
nonceStr: 'nonce',
|
|
package: 'prepay_id=wx-prepay',
|
|
signType: 'RSA',
|
|
paySign: 'signature',
|
|
});
|
|
|
|
expect(result).toEqual({ status: 'success', errorMessage: '' });
|
|
expect(globalThis.wx.requestPayment).toHaveBeenCalledWith({
|
|
timeStamp: '1777110165',
|
|
nonceStr: 'nonce',
|
|
package: 'prepay_id=wx-prepay',
|
|
signType: 'RSA',
|
|
paySign: 'signature',
|
|
success: expect.any(Function),
|
|
fail: expect.any(Function),
|
|
});
|
|
expect(globalThis.wx.requestVirtualPayment).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('maps virtual payment cancel errCode to cancel result', async () => {
|
|
const payError = {
|
|
errCode: -2,
|
|
errMsg: 'requestVirtualPayment:fail cancel',
|
|
};
|
|
globalThis.wx.requestVirtualPayment.mockImplementationOnce((options) => {
|
|
options.fail?.(payError);
|
|
});
|
|
|
|
await expect(
|
|
requestWechatPayment({
|
|
mode: 'short_series_coin',
|
|
signData: '{}',
|
|
paySig: 'pay-sig',
|
|
signature: 'user-sig',
|
|
}),
|
|
).resolves.toEqual({
|
|
status: 'cancel',
|
|
errorMessage: JSON.stringify({
|
|
errCode: -2,
|
|
errMsg: 'requestVirtualPayment:fail cancel',
|
|
}),
|
|
});
|
|
expect(console.error).toHaveBeenCalledWith(
|
|
'[wechat-pay] requestVirtualPayment failed',
|
|
payError,
|
|
);
|
|
});
|
|
|
|
test('page notifies previous web-view after virtual payment', 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();
|
|
});
|
|
|
|
test('parsePayParams and appendPayResult keep existing behavior', () => {
|
|
expect(parsePayParams(encodeURIComponent('{"paySign":"sig"}'))).toEqual({
|
|
paySign: 'sig',
|
|
});
|
|
expect(appendPayResult('https://web.test/#old=1', 'req:fail:order-1')).toBe(
|
|
'https://web.test/#old=1&wx_pay_result=req%3Afail%3Aorder-1',
|
|
);
|
|
});
|
|
});
|