Files
Genarrative/miniprogram/shell/shareGrid.test.js
T
kdletters abf3174c6a 锁定微信九宫切图失败提示
微信九宫切图失败不再向页面透出原生错误细节

微信九宫切图测试覆盖原生下载失败稳定提示

native shell 总门禁反查微信九宫切图失败边界
2026-06-20 13:31:56 +08:00

75 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 shareGridBridgePath = path.resolve(
process.cwd(),
'miniprogram/host-bridge/shareGrid.js',
);
const shareGridShellPath = path.resolve(
process.cwd(),
'miniprogram/shell/shareGrid.js',
);
let createWechatShareGridPage;
function createPage() {
const page = createWechatShareGridPage();
return {
...page,
data: { ...page.data },
setData(patch) {
Object.assign(this.data, patch);
},
};
}
describe('wechat share-grid shell page', () => {
beforeEach(() => {
globalThis.wx = {
navigateBack: vi.fn(),
};
const shareGridBridge = loadCommonJsModule(shareGridBridgePath);
const shareGridShell = loadCommonJsModule(shareGridShellPath, {
'../host-bridge/shareGrid': shareGridBridge,
});
createWechatShareGridPage = shareGridShell.createWechatShareGridPage;
});
test('shows a clear error when imageUrl is missing', async () => {
const page = createPage();
await page.onLoad({});
expect(page.data.loading).toBe(false);
expect(page.data.errorMessage).toBe('缺少封面图。');
});
test('hides downloadFile native failure details from the page', async () => {
const consoleError = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined);
globalThis.wx.downloadFile = vi.fn(({ fail }) => {
fail({ errMsg: 'private native download detail' });
});
const page = createPage();
await page.onLoad({
imageUrl: 'https://web.test/cover.png',
title: '九宫切图',
publicWorkCode: 'PZ-0001',
});
expect(page.data.loading).toBe(false);
expect(page.data.errorMessage).toBe('九宫切图保存失败。');
expect(page.data.errorMessage).not.toContain('private native download detail');
expect(consoleError).toHaveBeenCalledWith(
'[share-grid] download failed',
{ errMsg: 'private native download detail' },
);
consoleError.mockRestore();
});
});