Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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()
}
311 changes: 304 additions & 7 deletions packages/taro-router/__tests__/navigation-bar-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
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 Down Expand Up @@ -42,13 +46,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 +81,299 @@ 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')

hideNavigationBarHomeButton()

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 中调用时,导航栏还未根据当前页面刷新
hideNavigationBarHomeButton()
handler.load()

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

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

hideNavigationBarHomeButton()

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

it('only applies to the page instance that hid it', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
hideNavigationBarHomeButton()
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()
hideNavigationBarHomeButton()

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()
hideNavigationBarHomeButton()
expect(getFnBtn()).toBe('none')

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

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

describe('hideHomeButton with the page handler', () => {
function createPage (path: string) {
return {
path,
options: {},
$taroParams: { stamp: 'navigation-bar-test' },
onShow: jest.fn(),
onHide: jest.fn(),
onUnload: jest.fn()
} as unknown as PageInstance
}

function createContainer (page: PageInstance) {
const el = document.createElement('div')
el.id = page.path!
el.className = 'taro_page'
document.body.appendChild(el)
return el
}

function createPageHandler (pathname: string) {
const config: SpaRouterConfig = {
pages: ['pages/index/index', 'pages/detail/index', 'pages/another/index'],
routes: [{ path: '/pages/index/index' }, { path: OTHER_PAGE }, { path: '/pages/another/index' }],
router: { mode: 'browser', basename: '', pathname },
window: { navigationStyle: 'default' }
}
return new PageHandler(config, createBrowserHistory())
}

// PageHandler 通过运行时的 location 获取当前路由,这里直接模拟路由切换
let route = OTHER_PAGE
const setRoute = (pathname: string) => {
route = pathname
}

afterEach(() => {
route = OTHER_PAGE
document.body.innerHTML = ''
})

it('stays hidden after navigateTo and navigateBack, and resets on a relaunched instance', () => {
const handler = createPageHandler(OTHER_PAGE)
jest.spyOn(handler, 'currentPage', 'get').mockImplementation(() => route)
const page = createPage(OTHER_PAGE)
createContainer(page)
// 页面在 onShow 中调用,此时路由还未刷新导航栏
;(page.onShow as jest.Mock).mockImplementationOnce(() => hideNavigationBarHomeButton())

handler.load(page, {}, 'navigation-bar-test')
expect(getFnBtn()).toBe('none')

// navigateTo 另一个页面
const nextPage = createPage('/pages/another/index')
createContainer(nextPage)
handler.hide(page)
setRoute('/pages/another/index')
handler.load(nextPage, {}, 'navigation-bar-test')
expect(getFnBtn()).toBe('back')

// navigateBack 回到原页面
handler.unload(nextPage)
setRoute(OTHER_PAGE)
handler.show(page)
expect(getFnBtn()).toBe('none')

// reLaunch 到同一路由的新实例,且没有调用该接口
handler.unload(page)
setRoute(OTHER_PAGE)
const relaunched = createPage(OTHER_PAGE)
createContainer(relaunched)
handler.load(relaunched, {}, 'navigation-bar-test')
expect(getFnBtn()).toBe('home')
})
})

describe('hideHomeButton called while a page is unloading', () => {
it('does not hide the home button of the page navigated back to', () => {
const config: SpaRouterConfig = {
pages: ['pages/index/index', 'pages/detail/index', 'pages/another/index'],
routes: [{ path: '/pages/index/index' }, { path: OTHER_PAGE }, { path: '/pages/another/index' }],
router: { mode: 'browser', basename: '', pathname: OTHER_PAGE },
window: { navigationStyle: 'default' }
}
const handler = new PageHandler(config, createBrowserHistory())
let route = OTHER_PAGE
jest.spyOn(handler, 'currentPage', 'get').mockImplementation(() => route)
const create = (path: string, onUnload?: () => void) => {
const el = document.createElement('div')
el.id = path
el.className = 'taro_page'
document.body.appendChild(el)
return {
path,
options: {},
$taroParams: { stamp: 'navigation-bar-test' },
onShow: jest.fn(),
onHide: jest.fn(),
onUnload: jest.fn(onUnload)
} as unknown as PageInstance
}

const pageA = create(OTHER_PAGE)
handler.load(pageA, {}, 'navigation-bar-test')
expect(getFnBtn()).toBe('home')

// B 在 onUnload 中调用该接口,此时 B 已出栈,栈顶是 A
const pageB = create('/pages/another/index', () => hideNavigationBarHomeButton())
handler.hide(pageA)
route = '/pages/another/index'
handler.load(pageB, {}, 'navigation-bar-test')
handler.unload(pageB)
route = OTHER_PAGE
handler.show(pageA)

expect(pageB.onUnload).toHaveBeenCalledTimes(1)
expect(getFnBtn()).toBe('home')
document.body.innerHTML = ''
})

it('ignores a call deferred by a microtask from the popped page', async () => {
const config: SpaRouterConfig = {
pages: ['pages/index/index', 'pages/detail/index', 'pages/another/index'],
routes: [{ path: '/pages/index/index' }, { path: OTHER_PAGE }, { path: '/pages/another/index' }],
router: { mode: 'browser', basename: '', pathname: OTHER_PAGE },
window: { navigationStyle: 'default' }
}
const handler = new PageHandler(config, createBrowserHistory())
let route = OTHER_PAGE
jest.spyOn(handler, 'currentPage', 'get').mockImplementation(() => route)
const create = (path: string, onUnload?: () => void) => {
const el = document.createElement('div')
el.id = path
el.className = 'taro_page'
document.body.appendChild(el)
return {
path,
options: {},
$taroParams: { stamp: 'navigation-bar-test' },
onShow: jest.fn(),
onHide: jest.fn(),
onUnload: jest.fn(onUnload)
} as unknown as PageInstance
}

const pageA = create(OTHER_PAGE)
handler.load(pageA, {}, 'navigation-bar-test')
expect(getFnBtn()).toBe('home')

// B 在 onUnload 中通过微任务延后调用该接口
let deferred: Promise<void> = Promise.resolve()
const pageB = create('/pages/another/index', () => {
deferred = Promise.resolve().then(() => hideNavigationBarHomeButton())
})
handler.hide(pageA)
route = '/pages/another/index'
handler.load(pageB, {}, 'navigation-bar-test')
handler.unload(pageB)
route = OTHER_PAGE
handler.show(pageA)
await deferred
await new Promise(resolve => setTimeout(resolve, 0))

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

// 卸载结束后,A 自己调用仍然生效
hideNavigationBarHomeButton()
expect(getFnBtn()).toBe('none')
document.body.innerHTML = ''
})

it('still applies a call made in onShow of the page shown right after the unload', () => {
const config: SpaRouterConfig = {
pages: ['pages/index/index', 'pages/detail/index', 'pages/another/index'],
routes: [{ path: '/pages/index/index' }, { path: OTHER_PAGE }, { path: '/pages/another/index' }],
router: { mode: 'browser', basename: '', pathname: OTHER_PAGE },
window: { navigationStyle: 'default' }
}
const handler = new PageHandler(config, createBrowserHistory())
let route = OTHER_PAGE
jest.spyOn(handler, 'currentPage', 'get').mockImplementation(() => route)
const create = (path: string) => {
const el = document.createElement('div')
el.id = path
el.className = 'taro_page'
document.body.appendChild(el)
return {
path,
options: {},
$taroParams: { stamp: 'navigation-bar-test' },
onShow: jest.fn(),
onHide: jest.fn(),
onUnload: jest.fn()
} as unknown as PageInstance
}

const pageA = create(OTHER_PAGE)
handler.load(pageA, {}, 'navigation-bar-test')

// reLaunch:旧页面卸载后立即加载新页面,新页面在 onShow 中隐藏首页按钮
const pageB = create('/pages/another/index')
;(pageB.onShow as jest.Mock).mockImplementationOnce(() => hideNavigationBarHomeButton())
handler.unload(pageA)
route = '/pages/another/index'
handler.load(pageB, {}, 'navigation-bar-test')

expect(getFnBtn()).toBe('none')
document.body.innerHTML = ''
})
})

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