fix(web): guard search page browser globals in tests

This commit is contained in:
huishi3 2026-04-09 10:25:25 +05:30
parent 69a299e40f
commit c25ea62ee7

View file

@ -18,6 +18,37 @@ import { APP_SHELL_PAGE_CLASS_NAME } from '@/app/page-shell-style'
const PAGE_SIZE = 12
function blurActiveElement() {
if (typeof document === 'undefined' || typeof HTMLElement === 'undefined') {
return
}
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur()
}
}
function scrollToTopOnPageChange() {
if (typeof window === 'undefined') {
return () => {}
}
let secondFrame = 0
const firstFrame = window.requestAnimationFrame(() => {
window.scrollTo({ top: 0, behavior: 'auto' })
secondFrame = window.requestAnimationFrame(() => {
window.scrollTo({ top: 0, behavior: 'auto' })
})
})
return () => {
window.cancelAnimationFrame(firstFrame)
if (secondFrame) {
window.cancelAnimationFrame(secondFrame)
}
}
}
/**
* Skill discovery page with synchronized URL state.
*
@ -68,24 +99,12 @@ export function SearchPage() {
useEffect(() => {
if (previousPageRef.current !== page) {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur()
}
let secondFrame = 0
const firstFrame = window.requestAnimationFrame(() => {
window.scrollTo({ top: 0, behavior: 'auto' })
secondFrame = window.requestAnimationFrame(() => {
window.scrollTo({ top: 0, behavior: 'auto' })
})
})
blurActiveElement()
const cleanupScroll = scrollToTopOnPageChange()
previousPageRef.current = page
return () => {
window.cancelAnimationFrame(firstFrame)
if (secondFrame) {
window.cancelAnimationFrame(secondFrame)
}
cleanupScroll()
}
}
@ -145,9 +164,7 @@ export function SearchPage() {
}
const handlePageChange = (newPage: number) => {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur()
}
blurActiveElement()
navigate({ to: '/search', search: { q, label: selectedLabel, sort, page: newPage, starredOnly } })
}