mirror of
https://github.com/langgenius/dify.git
synced 2025-12-20 06:32:45 +00:00
Some checks failed
autofix.ci / autofix (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Has been cancelled
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Has been cancelled
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Has been cancelled
Main CI Pipeline / Check Changed Files (push) Has been cancelled
Main CI Pipeline / API Tests (push) Has been cancelled
Main CI Pipeline / Web Tests (push) Has been cancelled
Main CI Pipeline / Style Check (push) Has been cancelled
Main CI Pipeline / VDB Tests (push) Has been cancelled
Main CI Pipeline / DB Migration Test (push) Has been cancelled
Check i18n Files and Create PR / check-and-update (push) Has been cancelled
117 lines
3.4 KiB
TypeScript
117 lines
3.4 KiB
TypeScript
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()
|
|
})
|
|
})
|