修复小程序内嵌页面标题同步
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
lyf
2026-07-27 19:56:53 +08:00
parent 9ea1bfab71
commit f1047414bb
6 changed files with 113 additions and 3 deletions

View File

@@ -112,7 +112,7 @@ describe('讲解展厅列表返回', () => {
window.history.replaceState({}, '', window.location.href)
const wrapper = mountExplainList()
expect(wrapper.getComponent(ExplainHallSelectStub).props('forceInternalHeader')).toBe(true)
expect(wrapper.getComponent(ExplainHallSelectStub).props('forceInternalHeader')).toBe(false)
await wrapper.get('[data-testid="explain-list-back"]').trigger('click')
expect(uni.navigateBack).not.toHaveBeenCalled()

View File

@@ -0,0 +1,61 @@
// @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()
})
})