fix: moving focus after navigations (#28937)

This commit is contained in:
Stephen Zhou
2025-12-01 09:55:04 +08:00
committed by GitHub
parent a087ace697
commit b91d22375f
2 changed files with 10 additions and 9 deletions

View File

@@ -116,7 +116,7 @@ describe('useTabSearchParams', () => {
setActiveTab('settings') setActiveTab('settings')
}) })
expect(mockPush).toHaveBeenCalledWith('/test-path?category=settings') expect(mockPush).toHaveBeenCalledWith('/test-path?category=settings', { scroll: false })
expect(mockReplace).not.toHaveBeenCalled() expect(mockReplace).not.toHaveBeenCalled()
}) })
@@ -137,7 +137,7 @@ describe('useTabSearchParams', () => {
setActiveTab('settings') setActiveTab('settings')
}) })
expect(mockReplace).toHaveBeenCalledWith('/test-path?category=settings') expect(mockReplace).toHaveBeenCalledWith('/test-path?category=settings', { scroll: false })
expect(mockPush).not.toHaveBeenCalled() expect(mockPush).not.toHaveBeenCalled()
}) })
@@ -157,6 +157,7 @@ describe('useTabSearchParams', () => {
expect(mockPush).toHaveBeenCalledWith( expect(mockPush).toHaveBeenCalledWith(
'/test-path?category=settings%20%26%20config', '/test-path?category=settings%20%26%20config',
{ scroll: false },
) )
}) })
@@ -211,7 +212,7 @@ describe('useTabSearchParams', () => {
setActiveTab('profile') setActiveTab('profile')
}) })
expect(mockPush).toHaveBeenCalledWith('/test-path?tab=profile') expect(mockPush).toHaveBeenCalledWith('/test-path?tab=profile', { scroll: false })
}) })
}) })
@@ -294,7 +295,7 @@ describe('useTabSearchParams', () => {
const [activeTab] = result.current const [activeTab] = result.current
expect(activeTab).toBe('') expect(activeTab).toBe('')
expect(mockPush).toHaveBeenCalledWith('/test-path?category=') expect(mockPush).toHaveBeenCalledWith('/test-path?category=', { scroll: false })
}) })
/** /**
@@ -345,7 +346,7 @@ describe('useTabSearchParams', () => {
setActiveTab('settings') setActiveTab('settings')
}) })
expect(mockPush).toHaveBeenCalledWith('/fallback-path?category=settings') expect(mockPush).toHaveBeenCalledWith('/fallback-path?category=settings', { scroll: false })
// Restore mock // Restore mock
;(usePathname as jest.Mock).mockReturnValue(mockPathname) ;(usePathname as jest.Mock).mockReturnValue(mockPathname)
@@ -400,7 +401,7 @@ describe('useTabSearchParams', () => {
}) })
expect(result.current[0]).toBe('settings') expect(result.current[0]).toBe('settings')
expect(mockPush).toHaveBeenCalledWith('/test-path?category=settings') expect(mockPush).toHaveBeenCalledWith('/test-path?category=settings', { scroll: false })
// Change to profile tab // Change to profile tab
act(() => { act(() => {
@@ -409,7 +410,7 @@ describe('useTabSearchParams', () => {
}) })
expect(result.current[0]).toBe('profile') expect(result.current[0]).toBe('profile')
expect(mockPush).toHaveBeenCalledWith('/test-path?category=profile') expect(mockPush).toHaveBeenCalledWith('/test-path?category=profile', { scroll: false })
// Verify push was called twice // Verify push was called twice
expect(mockPush).toHaveBeenCalledTimes(2) expect(mockPush).toHaveBeenCalledTimes(2)
@@ -431,7 +432,7 @@ describe('useTabSearchParams', () => {
setActiveTab('advanced') setActiveTab('advanced')
}) })
expect(mockPush).toHaveBeenCalledWith('/app/123/settings?category=advanced') expect(mockPush).toHaveBeenCalledWith('/app/123/settings?category=advanced', { scroll: false })
// Restore mock // Restore mock
;(usePathname as jest.Mock).mockReturnValue(mockPathname) ;(usePathname as jest.Mock).mockReturnValue(mockPathname)

View File

@@ -40,7 +40,7 @@ export const useTabSearchParams = ({
setTab(newActiveTab) setTab(newActiveTab)
if (disableSearchParams) if (disableSearchParams)
return return
router[`${routingBehavior}`](`${pathName}?${searchParamName}=${encodeURIComponent(newActiveTab)}`) router[`${routingBehavior}`](`${pathName}?${searchParamName}=${encodeURIComponent(newActiveTab)}`, { scroll: false })
} }
return [activeTab, setActiveTab] as const return [activeTab, setActiveTab] as const