dify/web/app/components/base/markdown/error-boundary.spec.tsx
Poojan 0ac09127c7
test: add unit tests for base components-part-4 (#32452)
Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com>
2026-02-25 17:36:58 +08:00

55 lines
1.4 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import * as React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import ErrorBoundary from './error-boundary'
import '@testing-library/jest-dom'
describe('ErrorBoundary', () => {
let consoleErrorSpy: ReturnType<typeof vi.spyOn>
beforeEach(() => {
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { })
})
afterEach(() => {
consoleErrorSpy.mockRestore()
})
it('renders children when there is no error', () => {
render(
<ErrorBoundary>
<div data-testid="child">Hello world</div>
</ErrorBoundary>,
)
expect(screen.getByTestId('child')).toHaveTextContent('Hello world')
expect(consoleErrorSpy).not.toHaveBeenCalled()
})
it('catches errors thrown in children, shows fallback UI and logs the error', () => {
const testError = new Error('Test render error')
const Thrower: React.FC = () => {
throw testError
}
render(
<ErrorBoundary>
<Thrower />
</ErrorBoundary>,
)
expect(
screen.getByText(/Oops! An error occurred/i),
).toBeInTheDocument()
expect(consoleErrorSpy).toHaveBeenCalled()
const hasLoggedOurError = consoleErrorSpy.mock.calls.some((call: unknown[]) =>
call.includes(testError),
)
expect(hasLoggedOurError).toBe(true)
})
})