This commit is contained in:
@@ -9,10 +9,6 @@ const hostEnvironmentMocks = vi.hoisted(() => ({
|
||||
embeddedInWechatMiniProgram: false
|
||||
}))
|
||||
|
||||
const miniProgramBridgeMocks = vi.hoisted(() => ({
|
||||
returnToWechatMiniProgram: vi.fn()
|
||||
}))
|
||||
|
||||
const testState = vi.hoisted(() => ({
|
||||
onLoadHandler: null as null | (() => void),
|
||||
onBackPressHandler: null as null | (() => boolean)
|
||||
@@ -31,10 +27,6 @@ vi.mock('@/utils/hostEnvironment', () => ({
|
||||
isEmbeddedInWechatMiniProgram: () => hostEnvironmentMocks.embeddedInWechatMiniProgram
|
||||
}))
|
||||
|
||||
vi.mock('@/services/WechatMiniProgramBridgeService', () => ({
|
||||
returnToWechatMiniProgram: miniProgramBridgeMocks.returnToWechatMiniProgram
|
||||
}))
|
||||
|
||||
vi.mock('@/usecases/explainUseCase', () => ({
|
||||
explainUseCase: {
|
||||
loadExplainHalls: async () => [],
|
||||
@@ -68,7 +60,6 @@ const mountExplainList = () => mount(ExplainListPage, {
|
||||
|
||||
beforeEach(() => {
|
||||
hostEnvironmentMocks.embeddedInWechatMiniProgram = false
|
||||
miniProgramBridgeMocks.returnToWechatMiniProgram.mockResolvedValue({ status: 'not-embedded' })
|
||||
testState.onLoadHandler = null
|
||||
testState.onBackPressHandler = null
|
||||
window.location.hash = '#/pages/explain/list'
|
||||
@@ -87,9 +78,9 @@ afterEach(() => {
|
||||
})
|
||||
|
||||
describe('讲解展厅列表返回', () => {
|
||||
it('首页讲解 Tab 进入时,顶部返回使用 navigateBack 保留讲解 Tab', async () => {
|
||||
it('首页通过内部页面栈进入时,顶部返回使用 navigateBack 回到导览首页', async () => {
|
||||
vi.stubGlobal('getCurrentPages', vi.fn(() => [
|
||||
{ route: 'pages/index/index', options: { tab: 'explain' } },
|
||||
{ route: 'pages/index/index', options: { tab: 'guide' } },
|
||||
{ route: 'pages/explain/list', options: {} }
|
||||
]))
|
||||
const wrapper = mountExplainList()
|
||||
@@ -100,7 +91,6 @@ describe('讲解展厅列表返回', () => {
|
||||
|
||||
expect(uni.navigateBack).toHaveBeenCalledWith(expect.objectContaining({ delta: 1 }))
|
||||
expect(uni.reLaunch).not.toHaveBeenCalled()
|
||||
expect(miniProgramBridgeMocks.returnToWechatMiniProgram).not.toHaveBeenCalled()
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
@@ -116,38 +106,38 @@ describe('讲解展厅列表返回', () => {
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('微信嵌入态直达页面由宿主提供标题与返回,并返回小程序', async () => {
|
||||
it('独立页在微信嵌入态强制提供 H5 返回,并保留宿主识别参数', async () => {
|
||||
hostEnvironmentMocks.embeddedInWechatMiniProgram = true
|
||||
miniProgramBridgeMocks.returnToWechatMiniProgram.mockResolvedValue({ status: 'returned' })
|
||||
window.location.hash = '#/pages/explain/list?embedded=wechat-mini-program&weapp=1'
|
||||
window.history.replaceState({}, '', window.location.href)
|
||||
const wrapper = mountExplainList()
|
||||
|
||||
expect(wrapper.getComponent(ExplainHallSelectStub).props('forceInternalHeader')).toBe(false)
|
||||
expect(wrapper.getComponent(ExplainHallSelectStub).props('forceInternalHeader')).toBe(true)
|
||||
await wrapper.get('[data-testid="explain-list-back"]').trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
expect(uni.navigateBack).not.toHaveBeenCalled()
|
||||
expect(uni.reLaunch).not.toHaveBeenCalled()
|
||||
expect(miniProgramBridgeMocks.returnToWechatMiniProgram).toHaveBeenCalledTimes(1)
|
||||
expect(uni.reLaunch).toHaveBeenCalledWith(expect.objectContaining({
|
||||
url: '/pages/index/index?tab=guide&embedded=wechat-mini-program&weapp=1'
|
||||
}))
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('微信嵌入态从本项目首页进入时仍返回本项目首页', async () => {
|
||||
it('微信嵌入态即使前置页是导览首页也不调用宿主返回', async () => {
|
||||
hostEnvironmentMocks.embeddedInWechatMiniProgram = true
|
||||
window.location.hash = '#/pages/explain/list?embedded=wechat-mini-program'
|
||||
window.history.replaceState({}, '', window.location.href)
|
||||
vi.stubGlobal('getCurrentPages', vi.fn(() => [
|
||||
{ route: 'pages/index/index', options: { tab: 'explain' } },
|
||||
{ route: 'pages/index/index', options: { tab: 'guide' } },
|
||||
{ route: 'pages/explain/list', options: {} }
|
||||
]))
|
||||
const wrapper = mountExplainList()
|
||||
|
||||
await wrapper.get('[data-testid="explain-list-back"]').trigger('click')
|
||||
|
||||
expect(uni.navigateBack).toHaveBeenCalledWith(expect.objectContaining({ delta: 1 }))
|
||||
expect(uni.reLaunch).not.toHaveBeenCalled()
|
||||
expect(miniProgramBridgeMocks.returnToWechatMiniProgram).not.toHaveBeenCalled()
|
||||
expect(uni.navigateBack).not.toHaveBeenCalled()
|
||||
expect(uni.reLaunch).toHaveBeenCalledWith(expect.objectContaining({
|
||||
url: '/pages/index/index?tab=guide&embedded=wechat-mini-program'
|
||||
}))
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
@@ -167,27 +157,23 @@ describe('讲解展厅列表返回', () => {
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('首页返回失败时重启到原讲解 Tab', async () => {
|
||||
it('上一页是首页但不是导览 Tab 时重启到导览首页', async () => {
|
||||
vi.stubGlobal('getCurrentPages', vi.fn(() => [
|
||||
{ route: 'pages/index/index', options: { tab: 'explain' } },
|
||||
{ route: 'pages/explain/list', options: {} }
|
||||
]))
|
||||
vi.stubGlobal('uni', {
|
||||
navigateBack: vi.fn((options: { fail?: () => void }) => options.fail?.()),
|
||||
reLaunch: vi.fn(),
|
||||
showToast: vi.fn()
|
||||
})
|
||||
const wrapper = mountExplainList()
|
||||
|
||||
await wrapper.get('[data-testid="explain-list-back"]').trigger('click')
|
||||
|
||||
expect(uni.navigateBack).not.toHaveBeenCalled()
|
||||
expect(uni.reLaunch).toHaveBeenCalledWith(expect.objectContaining({
|
||||
url: '/pages/index/index?tab=explain'
|
||||
url: '/pages/index/index?tab=guide'
|
||||
}))
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('导览首页返回失败后重启到原导览 Tab', async () => {
|
||||
it('navigateBack 失败后重启到导览首页', async () => {
|
||||
vi.stubGlobal('getCurrentPages', vi.fn(() => [
|
||||
{ route: 'pages/index/index', options: { tab: 'guide' } },
|
||||
{ route: 'pages/explain/list', options: {} }
|
||||
@@ -224,9 +210,8 @@ describe('讲解展厅列表返回', () => {
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('微信嵌入态根栈不创建 H5 history 标记,平台返回交给宿主', async () => {
|
||||
it('根栈只创建一条历史标记,浏览器返回会回到导览首页', async () => {
|
||||
hostEnvironmentMocks.embeddedInWechatMiniProgram = true
|
||||
miniProgramBridgeMocks.returnToWechatMiniProgram.mockResolvedValue({ status: 'returned' })
|
||||
window.location.hash = '#/pages/explain/list?embedded=wechat-mini-program&weapp=1'
|
||||
window.history.replaceState({}, '', window.location.href)
|
||||
const wrapper = mountExplainList()
|
||||
@@ -235,13 +220,17 @@ describe('讲解展厅列表返回', () => {
|
||||
testState.onLoadHandler?.()
|
||||
await flushPromises()
|
||||
|
||||
expect(pushState).not.toHaveBeenCalled()
|
||||
window.dispatchEvent(new PopStateEvent('popstate', { state: {} }))
|
||||
await flushPromises()
|
||||
|
||||
expect(uni.navigateBack).not.toHaveBeenCalled()
|
||||
expect(uni.reLaunch).not.toHaveBeenCalled()
|
||||
expect(testState.onBackPressHandler?.()).toBe(true)
|
||||
await flushPromises()
|
||||
expect(miniProgramBridgeMocks.returnToWechatMiniProgram).toHaveBeenCalledTimes(1)
|
||||
expect(pushState).toHaveBeenCalledTimes(1)
|
||||
const url = vi.mocked(uni.reLaunch).mock.calls[0]?.[0]?.url || ''
|
||||
const query = new URLSearchParams(url.split('?')[1])
|
||||
expect(url.split('?')[0]).toBe('/pages/index/index')
|
||||
expect(query.get('tab')).toBe('guide')
|
||||
expect(query.get('embedded')).toBe('wechat-mini-program')
|
||||
expect(query.get('weapp')).toBe('1')
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user