Files
frontend-miniapp/tests/unit/hostNavigationTitle.spec.ts
lyf f1047414bb
Some checks failed
CI / verify (push) Has been cancelled
修复小程序内嵌页面标题同步
2026-07-27 19:56:53 +08:00

62 lines
1.6 KiB
TypeScript

// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
MINI_PROGRAM_NAVIGATION_TITLE_MESSAGE,
syncHostNavigationTitle
} from '@/utils/hostNavigationTitle'
const hostEnvironmentMocks = vi.hoisted(() => ({
embeddedInWechatMiniProgram: false
}))
vi.mock('@/utils/hostEnvironment', () => ({
isEmbeddedInWechatMiniProgram: () => hostEnvironmentMocks.embeddedInWechatMiniProgram
}))
afterEach(() => {
hostEnvironmentMocks.embeddedInWechatMiniProgram = false
vi.unstubAllGlobals()
})
describe('小程序宿主标题同步', () => {
it('嵌入态同步免费讲解标题并通知小程序宿主', () => {
const postMessage = vi.fn()
const setNavigationBarTitle = vi.fn()
hostEnvironmentMocks.embeddedInWechatMiniProgram = true
vi.stubGlobal('uni', { setNavigationBarTitle })
vi.stubGlobal('window', {
...window,
wx: {
miniProgram: { postMessage }
}
})
syncHostNavigationTitle('免费讲解')
expect(document.title).toBe('免费讲解')
expect(setNavigationBarTitle).toHaveBeenCalledWith({ title: '免费讲解' })
expect(postMessage).toHaveBeenCalledWith({
data: {
type: MINI_PROGRAM_NAVIGATION_TITLE_MESSAGE,
title: '免费讲解'
}
})
})
it('非嵌入态不向小程序桥接发送标题', () => {
const postMessage = vi.fn()
vi.stubGlobal('window', {
...window,
wx: {
miniProgram: { postMessage }
}
})
syncHostNavigationTitle('免费讲解')
expect(document.title).toBe('免费讲解')
expect(postMessage).not.toHaveBeenCalled()
})
})