|
| 1 | +import * as React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { MemoryRouter } from 'react-router-dom'; |
| 5 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 6 | +import { ErrorBoundary } from '../ErrorBoundary'; |
| 7 | + |
| 8 | +// Silence React's own error-boundary console output in test runs |
| 9 | +beforeEach(() => { |
| 10 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 11 | +}); |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + vi.restoreAllMocks(); |
| 15 | +}); |
| 16 | + |
| 17 | +// A component that always throws during render |
| 18 | +function Bomb({ message }: { message: string }) { |
| 19 | + throw new Error(message); |
| 20 | +} |
| 21 | + |
| 22 | +// Wraps the boundary in a MemoryRouter |
| 23 | +function harness( |
| 24 | + children: React.ReactNode, |
| 25 | + onGoHome = vi.fn(), |
| 26 | + onGoToSettings = vi.fn(), |
| 27 | + onReport?: (id: string, msg: string) => void |
| 28 | +) { |
| 29 | + return render( |
| 30 | + <MemoryRouter> |
| 31 | + <ErrorBoundary onGoHome={onGoHome} onGoToSettings={onGoToSettings} onReport={onReport}> |
| 32 | + {children} |
| 33 | + </ErrorBoundary> |
| 34 | + </MemoryRouter> |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +describe('ErrorBoundary & ErrorFallback', () => { |
| 39 | + it('renders children normally when nothing throws', () => { |
| 40 | + harness(<div>All good</div>); |
| 41 | + expect(screen.getByText('All good')).toBeInTheDocument(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('catches a render error and shows the fallback — no white-screen', () => { |
| 45 | + harness(<Bomb message="boom" />); |
| 46 | + expect(screen.getByRole('heading', { name: /something went wrong/i })).toBeInTheDocument(); |
| 47 | + expect(screen.getByText('boom')).toBeInTheDocument(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('shows an Error ID for support correlation', () => { |
| 51 | + harness(<Bomb message="id-check" />); |
| 52 | + const el = screen.getByText(/Support Reference:/i); |
| 53 | + expect(el).toBeInTheDocument(); |
| 54 | + |
| 55 | + const idEl = screen.getByText(/ERR-[A-Z0-9]+-[A-Z0-9]+/); |
| 56 | + expect(idEl).toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('"Try again" resets the boundary and re-renders children', async () => { |
| 60 | + const user = userEvent.setup(); |
| 61 | + let shouldThrow = true; |
| 62 | + |
| 63 | + // A component whose throw behaviour we can flip between renders |
| 64 | + function Flaky() { |
| 65 | + if (shouldThrow) throw new Error('flaky'); |
| 66 | + return <div>Recovered</div>; |
| 67 | + } |
| 68 | + |
| 69 | + const { rerender } = render( |
| 70 | + <MemoryRouter> |
| 71 | + <ErrorBoundary> |
| 72 | + <Flaky /> |
| 73 | + </ErrorBoundary> |
| 74 | + </MemoryRouter> |
| 75 | + ); |
| 76 | + |
| 77 | + expect(screen.getByRole('heading', { name: /something went wrong/i })).toBeInTheDocument(); |
| 78 | + |
| 79 | + // Stop throwing before the reset re-render |
| 80 | + shouldThrow = false; |
| 81 | + await user.click(screen.getByRole('button', { name: /try again/i })); |
| 82 | + |
| 83 | + expect(await screen.findByText('Recovered')).toBeInTheDocument(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('"Go to home" calls onGoHome and clears the boundary', async () => { |
| 87 | + const user = userEvent.setup(); |
| 88 | + const onGoHome = vi.fn(); |
| 89 | + |
| 90 | + harness(<Bomb message="go-home" />, onGoHome); |
| 91 | + await user.click(screen.getByRole('button', { name: /go to home/i })); |
| 92 | + |
| 93 | + expect(onGoHome).toHaveBeenCalledOnce(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('"Go to settings" calls onGoToSettings and clears the boundary', async () => { |
| 97 | + const user = userEvent.setup(); |
| 98 | + const onGoToSettings = vi.fn(); |
| 99 | + |
| 100 | + harness(<Bomb message="go-settings" />, vi.fn(), onGoToSettings); |
| 101 | + await user.click(screen.getByRole('button', { name: /go to settings/i })); |
| 102 | + |
| 103 | + expect(onGoToSettings).toHaveBeenCalledOnce(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('redacts sensitive key material from displayed error text and console logs', () => { |
| 107 | + harness(<Bomb message="private_key: SABCD1234SENSITIVE" />); |
| 108 | + expect(screen.queryByText(/SABCD1234/)).not.toBeInTheDocument(); |
| 109 | + expect(screen.getByText(/redacted/i)).toBeInTheDocument(); |
| 110 | + |
| 111 | + // Verify console error was called with sanitized message |
| 112 | + const errorSpy = console.error as jest.Mock; |
| 113 | + expect(errorSpy).toHaveBeenCalled(); |
| 114 | + const lastCallArgs = errorSpy.mock.calls.find((call) => call[0] === '[ErrorBoundary]'); |
| 115 | + expect(lastCallArgs).toBeDefined(); |
| 116 | + expect(lastCallArgs[1].message).toMatch(/redacted/i); |
| 117 | + expect(lastCallArgs[1].message).not.toContain('SABCD1234SENSITIVE'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('redacts Stellar secret key (56 chars starting with S)', () => { |
| 121 | + // 56 characters starting with S (S + 55 valid base32 characters) |
| 122 | + const stellarSecretKey = 'SAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA──────────────────────'; |
| 123 | + harness(<Bomb message={`Secret is ${stellarSecretKey}`} />); |
| 124 | + expect(screen.queryByText(stellarSecretKey)).not.toBeInTheDocument(); |
| 125 | + expect(screen.getByText(/redacted/i)).toBeInTheDocument(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('calls onReport with the error ID and sanitized message', () => { |
| 129 | + const onReport = vi.fn(); |
| 130 | + harness(<Bomb message="report me" />, vi.fn(), vi.fn(), onReport); |
| 131 | + |
| 132 | + expect(onReport).toHaveBeenCalledOnce(); |
| 133 | + const [errorId, sanitized] = onReport.mock.calls[0] as [string, string]; |
| 134 | + expect(errorId).toMatch(/ERR-[A-Z0-9]+-[A-Z0-9]+/); |
| 135 | + expect(sanitized).toBe('report me'); |
| 136 | + }); |
| 137 | +}); |
0 commit comments