Files
Genarrative/miniprogram/shell/shareGrid.test.js
T
kdletters 0b848d38bb 收口微信九宫切图诊断日志
微信九宫切图失败日志改为固定阶段标签

原生壳门禁拒绝九宫切图日志输出原生错误对象

同步宿主壳方案和共享决策记录
2026-06-21 15:22:00 +08:00

74 lines
2.2 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');
expect(consoleError).toHaveBeenCalledWith('[share-grid] save failed');
expect(consoleError.mock.calls.flat()).not.toContain('private native download detail');
consoleError.mockRestore();
});
});