Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/taro-h5/__tests__/ui/navigation.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { eventCenter } from '@tarojs/runtime'
import * as Taro from '@tarojs/taro-h5'

import { buildApp } from '../utils'
Expand Down Expand Up @@ -96,4 +97,26 @@ describe('navigation', () => {
})
})
})
describe('hideHomeButton', () => {
test('should ask the navigation bar to hide the home button', async () => {
const listener = jest.fn()
const success = jest.fn()
const fail = jest.fn()
const complete = jest.fn()
const expectRes = { errMsg: 'hideHomeButton:ok' }
eventCenter.on('__taroH5HideHomeButton', listener)

try {
await expect(Taro.hideHomeButton({ success, fail, complete })).resolves.toEqual(expectRes)
await expect(Taro.hideHomeButton()).resolves.toEqual(expectRes)
} finally {
eventCenter.off('__taroH5HideHomeButton', listener)
}

expect(listener).toHaveBeenCalledTimes(2)
expect(success.mock.calls).toEqual([[expectRes]])
expect(complete.mock.calls).toEqual([[expectRes]])
expect(fail).not.toHaveBeenCalled()
})
})
})
14 changes: 11 additions & 3 deletions packages/taro-h5/src/api/ui/navigation-bar/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Taro from '@tarojs/api'
import { setNavigationBarLoading, setNavigationBarStyle, setTitle } from '@tarojs/router'
import { hideNavigationBarHomeButton, setNavigationBarLoading, setNavigationBarStyle, setTitle } from '@tarojs/router'

import { getParameterError, shouldBeObject, temporarilyNotSupport } from '../../../utils'
import { getParameterError, shouldBeObject } from '../../../utils'
import { MethodHandler } from '../../../utils/handler'

// 导航栏
Expand Down Expand Up @@ -66,4 +66,12 @@ export function hideNavigationBarLoading (options: Taro.hideNavigationBarLoading
return handle.success()
}

export const hideHomeButton = /* @__PURE__ */ temporarilyNotSupport('hideHomeButton')
/**
* 隐藏当前页面导航栏中的返回首页按钮
*/
export const hideHomeButton: typeof Taro.hideHomeButton = (options = {}) => {
const { success, fail, complete } = options
const handle = new MethodHandler({ name: 'hideHomeButton', success, fail, complete })
hideNavigationBarHomeButton()
return handle.success()
}
291 changes: 284 additions & 7 deletions packages/taro-router/__tests__/navigation-bar-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Current, createPageConfig, document as taroDocument, injectPageInstance } from '@tarojs/runtime'
import { createBrowserHistory } from 'history'

import NavigationBarHandler from '../src/router/navigation-bar'
import PageHandler from '../src/router/page'
import stacks from '../src/router/stack'
import { hideNavigationBarHomeButton } from '../src/utils/navigate'

import type { PageInstance } from '@tarojs/runtime'
import type PageHandler from '../src/router/page'
import type { SpaRouterConfig } from '../types/router'

const HOME_PAGE = '/pages/index/index'
const OTHER_PAGE = '/pages/detail/index'
Expand All @@ -23,6 +28,12 @@ function createHandler (currentPage: string, options: {
} as unknown as PageHandler)
}

// 页面的生命周期执行时,Current.page 指向该页面
function hideAsCurrentPage () {
Current.page = stacks.last as any
hideNavigationBarHomeButton()
}

function setStackSize (size: number) {
while (stacks.length > 0) stacks.pop()
for (let i = 0; i < size; i++) stacks.push({} as PageInstance)
Expand All @@ -42,13 +53,13 @@ describe('NavigationBarHandler', () => {
setStackSize(0)
})

describe('setFnBtnState', () => {
const getFnBtn = () => {
if (navigationBar.classList.contains('taro-navigation-bar-back-icon')) return 'back'
if (navigationBar.classList.contains('taro-navigation-bar-home-icon')) return 'home'
return 'none'
}
const getFnBtn = () => {
if (navigationBar.classList.contains('taro-navigation-bar-back-icon')) return 'back'
if (navigationBar.classList.contains('taro-navigation-bar-home-icon')) return 'home'
return 'none'
}

describe('setFnBtnState', () => {
it.each([
['the home page opened as the only page', HOME_PAGE, 1, 'none'],
['the home page navigated to from another page', HOME_PAGE, 2, 'back'],
Expand Down Expand Up @@ -77,6 +88,272 @@ describe('NavigationBarHandler', () => {
})
})

describe('hideHomeButton', () => {
it('hides the home button of the current page', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
expect(getFnBtn()).toBe('home')

hideAsCurrentPage()

expect(getFnBtn()).toBe('none')
})

it('keeps the home button hidden when called before the page is loaded', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)

// 页面在 onShow 中调用时,导航栏还未根据当前页面刷新
hideAsCurrentPage()
handler.load()

expect(getFnBtn()).toBe('none')
})

it('does not hide the back button', () => {
setStackSize(2)
const handler = createHandler(OTHER_PAGE)
handler.load()

hideAsCurrentPage()

expect(getFnBtn()).toBe('back')
})

it('only applies to the page instance that hid it', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
hideAsCurrentPage()
expect(getFnBtn()).toBe('none')

const anotherPage = '/pages/another/index'
setStackSize(1)
Object.assign(handler.pageContext, { currentPage: anotherPage, originPathname: anotherPage })
handler.load()
expect(getFnBtn()).toBe('home')
})

it('stays hidden after navigating to another page and back', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
hideAsCurrentPage()

const anotherPage = '/pages/another/index'
const original = stacks.last
stacks.push({} as PageInstance)
Object.assign(handler.pageContext, { currentPage: anotherPage, originPathname: anotherPage })
handler.load()
expect(getFnBtn()).toBe('back')

stacks.pop()
expect(stacks.last).toBe(original)
Object.assign(handler.pageContext, { currentPage: OTHER_PAGE, originPathname: OTHER_PAGE })
handler.load()
expect(getFnBtn()).toBe('none')
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现有用例都是在栈深为 1 的情况下切换页面,没覆盖最常见的 navigateTo 再 navigateBack 回来的流程。补一个用例确认中间出现返回按钮后,回到该页面时首页按钮仍保持隐藏,这个行为不管缓存最终按路由还是按页面实例都应该成立。

Suggested change
})
})
it('stays hidden after navigating to another page and back', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
hideNavigationBarHomeButton()
const anotherPage = '/pages/another/index'
setStackSize(2)
Object.assign(handler.pageContext, { currentPage: anotherPage, originPathname: anotherPage })
handler.load()
expect(getFnBtn()).toBe('back')
setStackSize(1)
Object.assign(handler.pageContext, { currentPage: OTHER_PAGE, originPathname: OTHER_PAGE })
handler.load()
expect(getFnBtn()).toBe('none')
})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the navigate-away-and-back case in 213c091. It checks the back button shows on the intermediate page and the home button is still hidden after returning.


it('does not carry over to a new instance of the same route', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
hideAsCurrentPage()
expect(getFnBtn()).toBe('none')

// 例如 reLaunch 到同一路由:旧实例出栈,新实例入栈且没有调用该接口
setStackSize(1)
handler.load()

expect(getFnBtn()).toBe('home')
})
})

describe('hideHomeButton with the real page config', () => {
const ANOTHER_PAGE = '/pages/another/index'
const STAMP = 'navigation-bar-test'

type Lifecycles = Partial<Record<'onShow' | 'onUnload', () => void>>

let route = OTHER_PAGE
let handler: PageHandler

// 与 connect 一致:挂载页面时创建页面容器并注册页面实例,用户生命周期通过 safeExecute 在 hasLoaded 之后执行
function createPage (path: string, lifecycles: Lifecycles = {}) {
const component = { onShow: jest.fn(lifecycles.onShow), onUnload: jest.fn(lifecycles.onUnload) }
Current.app = {
mount: (_component, $taroPath, cb) => {
const el = document.createElement('div')
el.id = $taroPath
el.className = 'taro_page'
document.body.appendChild(el)
const root = taroDocument.createElement('root')
root.id = $taroPath
taroDocument.body.appendChild(root)
injectPageInstance(component as any, $taroPath)
cb()
},
unmount: (_$taroPath, cb) => cb()
} as any
const page = createPageConfig(component, path, {}, {}) as unknown as PageInstance
page.options = { stamp: STAMP }
return { page, component }
}

function load (page: PageInstance, path: string) {
route = path
handler.load(page, {}, STAMP)
}

function back (from: PageInstance, to: PageInstance, path: string) {
handler.unload(from, 1)
route = path
handler.show(to)
}

// 用户的 onShow 在 Promise 回调中执行,需要等待微任务
const flush = async () => {
for (let i = 0; i < 5; i++) await Promise.resolve()
}

// H5 端页面路径会写入页面配置,同时页面挂载后同步执行回调
const platform = process.env.TARO_PLATFORM
beforeAll(() => {
process.env.TARO_PLATFORM = 'web'
})

afterAll(() => {
process.env.TARO_PLATFORM = platform
})

beforeEach(() => {
route = OTHER_PAGE
const config: SpaRouterConfig = {
pages: ['pages/index/index', 'pages/detail/index', 'pages/another/index'],
routes: [{ path: HOME_PAGE }, { path: OTHER_PAGE }, { path: ANOTHER_PAGE }],
router: { mode: 'browser', basename: '', pathname: OTHER_PAGE },
window: { navigationStyle: 'default' }
}
handler = new PageHandler(config, createBrowserHistory())
jest.spyOn(handler, 'currentPage', 'get').mockImplementation(() => route)
})

afterEach(() => {
jest.useRealTimers()
Current.page = null
document.body.innerHTML = ''
})

it('stays hidden after navigateTo and navigateBack, and resets on a relaunched instance', async () => {
const { page: pageA, component: a } = createPage(OTHER_PAGE)
// 只在第一次 onShow 中调用
a.onShow.mockImplementationOnce(() => hideNavigationBarHomeButton())
load(pageA, OTHER_PAGE)
await flush()
expect(getFnBtn()).toBe('none')

const { page: pageB } = createPage(ANOTHER_PAGE)
handler.hide(pageA)
load(pageB, ANOTHER_PAGE)
await flush()
expect(getFnBtn()).toBe('back')

back(pageB, pageA, OTHER_PAGE)
await flush()
expect(a.onShow).toHaveBeenCalledTimes(2)
expect(getFnBtn()).toBe('none')

// reLaunch 到同一路由的新实例,且没有调用该接口
handler.unload(pageA)
const { page: relaunched } = createPage(OTHER_PAGE)
load(relaunched, OTHER_PAGE)
await flush()
expect(getFnBtn()).toBe('home')
})

describe.each([
['without animation', false],
['with animation', true]
])('on the page navigated back to %s', (_name, animated) => {
async function openAThenB (lifecyclesB: Lifecycles = {}, lifecyclesA: Lifecycles = {}) {
const a = createPage(OTHER_PAGE, lifecyclesA)
load(a.page, OTHER_PAGE)
await flush()
expect(getFnBtn()).toBe('home')

const b = createPage(ANOTHER_PAGE, lifecyclesB)
handler.hide(a.page)
load(b.page, ANOTHER_PAGE)
await flush()
return { a, b }
}

// 带动画时页面在动画结束后才卸载,之后再展示目标页面
async function goBack (from: PageInstance, to: PageInstance) {
if (animated) {
jest.useFakeTimers()
handler.unload(from, 1, true)
await jest.advanceTimersByTimeAsync(handler.animationDuration)
route = OTHER_PAGE
handler.show(to)
} else {
back(from, to, OTHER_PAGE)
}
await flush()
}

it('does not hide the home button when the popped page calls it synchronously in onUnload', async () => {
const { a, b } = await openAThenB({ onUnload: () => hideNavigationBarHomeButton() })

await goBack(b.page, a.page)

expect(b.component.onUnload).toHaveBeenCalledTimes(1)
expect(getFnBtn()).toBe('home')
})

it('does not hide the home button when the popped page calls it from a microtask in onUnload', async () => {
let deferred: Promise<void> = Promise.resolve()
const { a, b } = await openAThenB({
onUnload: () => {
deferred = Promise.resolve().then(() => hideNavigationBarHomeButton())
}
})

await goBack(b.page, a.page)
await deferred

expect(b.component.onUnload).toHaveBeenCalledTimes(1)
expect(getFnBtn()).toBe('home')
})

it('applies a call made in onShow when the page is shown again', async () => {
const { a, b } = await openAThenB()
// 只在返回后的第二次 onShow 中调用
a.component.onShow.mockImplementationOnce(() => hideNavigationBarHomeButton())

await goBack(b.page, a.page)

expect(a.component.onShow).toHaveBeenCalledTimes(2)
expect(getFnBtn()).toBe('none')
})
})

it('applies a call made in onShow of a page opened by reLaunch', async () => {
const { page: pageA } = createPage(OTHER_PAGE)
load(pageA, OTHER_PAGE)
await flush()
expect(getFnBtn()).toBe('home')

handler.unload(pageA)
const { page: relaunched } = createPage(ANOTHER_PAGE, { onShow: () => hideNavigationBarHomeButton() })
load(relaunched, ANOTHER_PAGE)
await flush()

expect(getFnBtn()).toBe('none')
})
})

describe('setNavigationBarVisible', () => {
const isVisible = () => {
const show = navigationBar.classList.contains('taro-navigation-bar-show')
Expand Down
Loading
Loading