Files
Genarrative/miniprogram/shell/webView.test.js
kdletters 520aa049d3 收口微信页面事件诊断日志
微信 web-view 页面事件日志改为固定阶段标签

原生壳门禁拒绝输出 WebView 原生事件体

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

131 lines
4.0 KiB
JavaScript

import path from 'node:path';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { loadCommonJsModule } from '../test-utils/loadCommonJsModule.js';
const webViewBridgePath = path.resolve(
process.cwd(),
'miniprogram/host-bridge/webView.js',
);
const webViewShellPath = path.resolve(
process.cwd(),
'miniprogram/shell/webView.js',
);
const config = {
API_BASE_URL: 'https://www.genarrative.world',
DEV_API_BASE_URL: 'https://dev.genarrative.world',
DEV_WEB_VIEW_ENTRY_URL: 'https://dev.genarrative.world',
MINI_PROGRAM_APP_ID: 'wx-test-app',
MINI_PROGRAM_ENV: 'release',
WEB_VIEW_ENTRY_URL: 'https://www.genarrative.world',
WEB_VIEW_SOURCE_QUERY: {
clientType: 'mini_program',
clientRuntime: 'wechat_mini_program',
},
};
let createWechatWebViewPage;
function createPage() {
const page = createWechatWebViewPage();
return {
...page,
data: { ...page.data },
setData(patch) {
Object.assign(this.data, patch);
},
};
}
describe('wechat web-view shell page', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(console, 'error').mockImplementation(() => {});
vi.spyOn(console, 'info').mockImplementation(() => {});
globalThis.wx = {
getAccountInfoSync: vi.fn(() => ({
miniProgram: { envVersion: 'release' },
})),
getStorageSync: vi.fn(() => ''),
getSystemInfoSync: vi.fn(() => ({ platform: 'ios' })),
login: vi.fn(),
navigateBack: vi.fn(),
removeStorageSync: vi.fn(),
request: vi.fn(),
setStorageSync: vi.fn(),
showShareMenu: vi.fn(),
};
const webViewBridge = loadCommonJsModule(webViewBridgePath);
const webViewShell = loadCommonJsModule(webViewShellPath, {
'../config': config,
'../host-bridge/webView': webViewBridge,
});
createWechatWebViewPage = webViewShell.createWechatWebViewPage;
});
test('opens anonymous H5 web-view without eager login', async () => {
const page = createPage();
await page.onLoad({});
expect(globalThis.wx.login).not.toHaveBeenCalled();
expect(globalThis.wx.request).not.toHaveBeenCalled();
expect(page.data.loading).toBe(false);
expect(page.data.webViewUrl).toBe(
'https://www.genarrative.world?clientType=mini_program&clientRuntime=wechat_mini_program',
);
expect(globalThis.wx.showShareMenu).toHaveBeenCalledWith({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline'],
});
});
test('stores latest H5 share target for native share menu', () => {
const page = createPage();
const webViewDetail = {
data: [
{
data: {
type: 'genarrative:share-target',
payload: {
targetPath: '/works/detail',
work: 'BB-12345678',
title: '汪汪声浪',
},
},
},
],
};
page.handleWebViewMessage({
detail: webViewDetail,
});
expect(page.onShareAppMessage()).toEqual({
title: '陶泥儿',
path: '/pages/web-view/index?targetPath=%2Fworks%2Fdetail&work=BB-12345678',
});
expect(page.onShareTimeline()).toEqual({
title: '陶泥儿',
query: 'targetPath=%2Fworks%2Fdetail&work=BB-12345678',
});
expect(console.info).toHaveBeenCalledWith('[web-view] message');
expect(console.info.mock.calls.flat()).not.toContain(webViewDetail);
});
test('logs web-view page events without native detail payloads', () => {
const page = createPage();
const loadDetail = { src: 'https://www.genarrative.world/private' };
const errorDetail = { errMsg: 'private native load failed' };
page.handleWebViewLoad({ detail: loadDetail });
page.handleWebViewError({ detail: errorDetail });
expect(console.info).toHaveBeenCalledWith('[web-view] loaded');
expect(console.error).toHaveBeenCalledWith('[web-view] load failed');
expect(console.info.mock.calls.flat()).not.toContain(loadDetail);
expect(console.error.mock.calls.flat()).not.toContain(errorDetail);
});
});