62 lines
1.6 KiB
TypeScript
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()
|
|
})
|
|
})
|