49
tests/unit/hostEnvironment.spec.ts
Normal file
49
tests/unit/hostEnvironment.spec.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { isEmbeddedInWechatMiniProgram } from '@/utils/hostEnvironment'
|
||||
|
||||
const createWindow = (overrides: Record<string, unknown> = {}) => ({
|
||||
location: { search: '', hash: '' },
|
||||
navigator: { userAgent: '' },
|
||||
...overrides
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
describe('微信小程序宿主环境识别', () => {
|
||||
it('同一模块可以识别延迟注入的微信环境标识', () => {
|
||||
const testWindow = createWindow()
|
||||
vi.stubGlobal('window', testWindow)
|
||||
|
||||
expect(isEmbeddedInWechatMiniProgram()).toBe(false)
|
||||
Object.assign(testWindow, { __wxjs_environment: 'miniprogram' })
|
||||
expect(isEmbeddedInWechatMiniProgram()).toBe(true)
|
||||
})
|
||||
|
||||
it('支持宿主在普通查询或 hash 查询中显式声明环境', () => {
|
||||
vi.stubGlobal('window', createWindow({
|
||||
location: {
|
||||
search: '?embedded=weapp',
|
||||
hash: '#/pages/index/index?tab=guide'
|
||||
}
|
||||
}))
|
||||
expect(isEmbeddedInWechatMiniProgram()).toBe(true)
|
||||
|
||||
vi.stubGlobal('window', createWindow({
|
||||
location: {
|
||||
search: '',
|
||||
hash: '#/pages/index/index?tab=guide&embedded=wechat-mini-program'
|
||||
}
|
||||
}))
|
||||
expect(isEmbeddedInWechatMiniProgram()).toBe(true)
|
||||
})
|
||||
|
||||
it('普通微信 H5 不会被误判为小程序 web-view', () => {
|
||||
vi.stubGlobal('window', createWindow({
|
||||
navigator: { userAgent: 'Mozilla/5.0 MicroMessenger/8.0.50' }
|
||||
}))
|
||||
|
||||
expect(isEmbeddedInWechatMiniProgram()).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user