codeburn/app/renderer/components/ErrorBoundary.test.tsx
iamtoruk 781d37674b feat(app): error boundary so a crashing screen shows the error, not a blank
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.
2026-07-11 14:33:16 -07:00

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()
})
})