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