/* @vitest-environment jsdom */ import { afterEach, describe, expect, test, vi } from 'vitest'; import { buildWechatMiniProgramShareTargetMessage, postWechatMiniProgramShareTarget, } from './wechatMiniProgramShareTarget'; afterEach(() => { vi.restoreAllMocks(); Reflect.deleteProperty(window, 'wx'); window.history.replaceState(null, '', '/'); }); describe('wechatMiniProgramShareTarget', () => { test('builds a compact share target message for mini program native share', () => { expect( buildWechatMiniProgramShareTargetMessage({ targetPath: '/works/detail', work: ' BB-12345678 ', title: ' 汪汪声浪 ', }), ).toEqual({ type: 'genarrative:share-target', payload: { targetPath: '/works/detail', work: 'BB-12345678', title: '汪汪声浪', }, }); }); test('posts the current recommended work to mini program web-view host', () => { const postMessage = vi.fn(); window.history.replaceState( null, '', '/?clientRuntime=wechat_mini_program', ); window.wx = { miniProgram: { postMessage, }, }; expect( postWechatMiniProgramShareTarget({ targetPath: '/works/detail', work: 'BB-12345678', title: '汪汪声浪', }), ).toBe(true); expect(postMessage).toHaveBeenCalledWith({ data: { type: 'genarrative:share-target', payload: { targetPath: '/works/detail', work: 'BB-12345678', title: '汪汪声浪', }, }, }); }); });