fix: harden async window open placeholder logic (#29393)

This commit is contained in:
yyh
2025-12-10 16:46:48 +08:00
committed by GitHub
parent bafd093fa9
commit e477e6c928
6 changed files with 213 additions and 122 deletions

View File

@@ -0,0 +1,116 @@
import { act, renderHook } from '@testing-library/react'
import { useAsyncWindowOpen } from './use-async-window-open'
describe('useAsyncWindowOpen', () => {
const originalOpen = window.open
beforeEach(() => {
jest.clearAllMocks()
})
afterAll(() => {
window.open = originalOpen
})
it('opens immediate url synchronously without calling async getter', async () => {
const openSpy = jest.fn()
window.open = openSpy
const getUrl = jest.fn()
const { result } = renderHook(() => useAsyncWindowOpen())
await act(async () => {
await result.current(getUrl, {
immediateUrl: 'https://example.com',
target: '_blank',
features: 'noopener,noreferrer',
})
})
expect(openSpy).toHaveBeenCalledWith('https://example.com', '_blank', 'noopener,noreferrer')
expect(getUrl).not.toHaveBeenCalled()
})
it('sets opener to null and redirects when async url resolves', async () => {
const close = jest.fn()
const mockWindow: any = {
location: { href: '' },
close,
opener: 'should-be-cleared',
}
const openSpy = jest.fn(() => mockWindow)
window.open = openSpy
const { result } = renderHook(() => useAsyncWindowOpen())
await act(async () => {
await result.current(async () => 'https://example.com/path')
})
expect(openSpy).toHaveBeenCalledWith('about:blank', '_blank', undefined)
expect(mockWindow.opener).toBeNull()
expect(mockWindow.location.href).toBe('https://example.com/path')
expect(close).not.toHaveBeenCalled()
})
it('closes placeholder and forwards error when async getter throws', async () => {
const close = jest.fn()
const mockWindow: any = {
location: { href: '' },
close,
opener: null,
}
const openSpy = jest.fn(() => mockWindow)
window.open = openSpy
const onError = jest.fn()
const { result } = renderHook(() => useAsyncWindowOpen())
const error = new Error('fetch failed')
await act(async () => {
await result.current(async () => {
throw error
}, { onError })
})
expect(close).toHaveBeenCalled()
expect(onError).toHaveBeenCalledWith(error)
expect(mockWindow.location.href).toBe('')
})
it('closes placeholder and reports when no url is returned', async () => {
const close = jest.fn()
const mockWindow: any = {
location: { href: '' },
close,
opener: null,
}
const openSpy = jest.fn(() => mockWindow)
window.open = openSpy
const onError = jest.fn()
const { result } = renderHook(() => useAsyncWindowOpen())
await act(async () => {
await result.current(async () => null, { onError })
})
expect(close).toHaveBeenCalled()
expect(onError).toHaveBeenCalled()
const errArg = onError.mock.calls[0][0] as Error
expect(errArg.message).toBe('No url resolved for new window')
})
it('reports failure when window.open returns null', async () => {
const openSpy = jest.fn(() => null)
window.open = openSpy
const getUrl = jest.fn()
const onError = jest.fn()
const { result } = renderHook(() => useAsyncWindowOpen())
await act(async () => {
await result.current(getUrl, { onError })
})
expect(onError).toHaveBeenCalled()
const errArg = onError.mock.calls[0][0] as Error
expect(errArg.message).toBe('Failed to open new window')
expect(getUrl).not.toHaveBeenCalled()
})
})

View File

@@ -1,72 +1,49 @@
import { useCallback } from 'react'
import Toast from '@/app/components/base/toast'
export type AsyncWindowOpenOptions = {
successMessage?: string
errorMessage?: string
windowFeatures?: string
onError?: (error: any) => void
onSuccess?: (url: string) => void
type GetUrl = () => Promise<string | null | undefined>
type AsyncWindowOpenOptions = {
immediateUrl?: string | null
target?: string
features?: string
onError?: (error: Error) => void
}
export const useAsyncWindowOpen = () => {
const openAsync = useCallback(async (
fetchUrl: () => Promise<string>,
options: AsyncWindowOpenOptions = {},
) => {
const {
successMessage,
errorMessage = 'Failed to open page',
windowFeatures = 'noopener,noreferrer',
onError,
onSuccess,
} = options
export const useAsyncWindowOpen = () => useCallback(async (getUrl: GetUrl, options?: AsyncWindowOpenOptions) => {
const {
immediateUrl,
target = '_blank',
features,
onError,
} = options ?? {}
const newWindow = window.open('', '_blank', windowFeatures)
if (immediateUrl) {
window.open(immediateUrl, target, features)
return
}
if (!newWindow) {
const error = new Error('Popup blocked by browser')
onError?.(error)
Toast.notify({
type: 'error',
message: 'Popup blocked. Please allow popups for this site.',
})
const newWindow = window.open('about:blank', target, features)
if (!newWindow) {
onError?.(new Error('Failed to open new window'))
return
}
try {
newWindow.opener = null
}
catch { /* noop */ }
try {
const url = await getUrl()
if (url) {
newWindow.location.href = url
return
}
try {
const url = await fetchUrl()
if (url) {
newWindow.location.href = url
onSuccess?.(url)
if (successMessage) {
Toast.notify({
type: 'success',
message: successMessage,
})
}
}
else {
newWindow.close()
const error = new Error('Invalid URL received')
onError?.(error)
Toast.notify({
type: 'error',
message: errorMessage,
})
}
}
catch (error) {
newWindow.close()
onError?.(error)
Toast.notify({
type: 'error',
message: errorMessage,
})
}
}, [])
return { openAsync }
}
newWindow.close()
onError?.(new Error('No url resolved for new window'))
}
catch (error) {
newWindow.close()
onError?.(error instanceof Error ? error : new Error(String(error)))
}
}, [])