mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-08-01 12:25:31 +00:00
A render error in any section previously white-screened the whole app (no boundary). Wrap the section content in an ErrorBoundary keyed by section: the error message + component stack render in a card, the sidebar stays usable, and navigating away recovers. Logs to the console for diagnosis.
31 lines
1 KiB
TypeScript
31 lines
1 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { render, screen } from '@testing-library/react'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { ErrorBoundary } from './ErrorBoundary'
|
|
|
|
function Boom(): never {
|
|
throw new Error('kaboom detail')
|
|
}
|
|
|
|
describe('ErrorBoundary', () => {
|
|
beforeEach(() => {
|
|
// React logs the caught error; silence it so the run stays clean.
|
|
vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
})
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('renders children when nothing throws', () => {
|
|
render(<ErrorBoundary><p>all good</p></ErrorBoundary>)
|
|
expect(screen.getByText('all good')).toBeTruthy()
|
|
})
|
|
|
|
it('shows the error message instead of white-screening when a child throws', () => {
|
|
render(<ErrorBoundary><Boom /></ErrorBoundary>)
|
|
expect(screen.getByText('This screen hit an error')).toBeTruthy()
|
|
expect(screen.getByText('kaboom detail')).toBeTruthy()
|
|
expect(screen.getByRole('button', { name: 'Reload' })).toBeTruthy()
|
|
})
|
|
})
|