-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Ensure ReactPanelErrorBoundary handles undefined children (#1089) (
#1092) - ReactPanelErrorBoundary would throw an error if children was undefined - Just render `null` if `children` is `undefined` - Tested using the steps in DH-18461. Unable to figure out steps to reproduce in just DHC. - There may still be a fix higher up in Enterprise, but this at least ensures ReactPanelErrorBoundary is more robust and we don't totally euchre our layout
- Loading branch information
Showing
3 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
plugins/ui/src/js/src/layout/ReactPanelErrorBoundary.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import React from 'react'; | ||
import { TestUtils } from '@deephaven/test-utils'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { ReactPanelErrorBoundary } from './ReactPanelErrorBoundary'; | ||
|
||
// Mock the WidgetErrorView component | ||
jest.mock('../widget/WidgetErrorView', () => ({ | ||
__esModule: true, | ||
default: function MockWidgetErrorView({ error }: { error: Error }) { | ||
return <div data-testid="mock-error-view">{error.message}</div>; | ||
}, | ||
})); | ||
|
||
describe('ReactPanelErrorBoundary', () => { | ||
// Suppress console.error for our intentional errors | ||
beforeAll(() => { | ||
TestUtils.disableConsoleOutput(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('renders children when there is no error', () => { | ||
render( | ||
<ReactPanelErrorBoundary> | ||
<div data-testid="test-child">Test Content</div> | ||
</ReactPanelErrorBoundary> | ||
); | ||
|
||
expect(screen.getByTestId('test-child')).toBeInTheDocument(); | ||
expect(screen.getByText('Test Content')).toBeInTheDocument(); | ||
expect(screen.queryByTestId('mock-error-view')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders error view when child throws error', () => { | ||
const ErrorComponent = () => { | ||
throw new Error('Test error message'); | ||
}; | ||
|
||
render( | ||
<ReactPanelErrorBoundary> | ||
<ErrorComponent /> | ||
</ReactPanelErrorBoundary> | ||
); | ||
|
||
expect(screen.getByTestId('mock-error-view')).toBeInTheDocument(); | ||
expect(screen.getByText('Test error message')).toBeInTheDocument(); | ||
}); | ||
|
||
it('recovers when children are updated after error', () => { | ||
const ErrorComponent = () => { | ||
throw new Error('Test error message'); | ||
}; | ||
|
||
const { rerender } = render( | ||
<ReactPanelErrorBoundary> | ||
<ErrorComponent /> | ||
</ReactPanelErrorBoundary> | ||
); | ||
|
||
// Verify error state | ||
expect(screen.getByTestId('mock-error-view')).toBeInTheDocument(); | ||
expect(screen.getByText('Test error message')).toBeInTheDocument(); | ||
|
||
// Update with working component | ||
rerender( | ||
<ReactPanelErrorBoundary> | ||
<div data-testid="working-component">Working Content</div> | ||
</ReactPanelErrorBoundary> | ||
); | ||
|
||
// Verify recovery | ||
expect(screen.getByTestId('working-component')).toBeInTheDocument(); | ||
expect(screen.getByText('Working Content')).toBeInTheDocument(); | ||
expect(screen.queryByTestId('mock-error-view')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('maintains error state when props update does not include children change', () => { | ||
const ErrorComponent = () => { | ||
throw new Error('Test error message'); | ||
}; | ||
|
||
const { rerender } = render( | ||
<ReactPanelErrorBoundary> | ||
<ErrorComponent /> | ||
</ReactPanelErrorBoundary> | ||
); | ||
|
||
// Verify initial error state | ||
expect(screen.getByTestId('mock-error-view')).toBeInTheDocument(); | ||
|
||
// Rerender with same children | ||
rerender( | ||
<ReactPanelErrorBoundary> | ||
<ErrorComponent /> | ||
</ReactPanelErrorBoundary> | ||
); | ||
|
||
// Error view should still be present | ||
expect(screen.getByTestId('mock-error-view')).toBeInTheDocument(); | ||
expect(screen.getByText('Test error message')).toBeInTheDocument(); | ||
}); | ||
|
||
it('calls componentDidCatch when error occurs', () => { | ||
const errorSpy = jest.spyOn( | ||
ReactPanelErrorBoundary.prototype, | ||
'componentDidCatch' | ||
); | ||
const ErrorComponent = () => { | ||
throw new Error('Test error message'); | ||
}; | ||
|
||
render( | ||
<ReactPanelErrorBoundary> | ||
<ErrorComponent /> | ||
</ReactPanelErrorBoundary> | ||
); | ||
|
||
expect(errorSpy).toHaveBeenCalled(); | ||
expect(errorSpy.mock.calls[0][0]).toBeInstanceOf(Error); | ||
expect(errorSpy.mock.calls[0][0].message).toBe('Test error message'); | ||
|
||
errorSpy.mockRestore(); | ||
}); | ||
|
||
it('does not throw an error when children are undefined', () => { | ||
expect(() => | ||
render(<ReactPanelErrorBoundary>{undefined}</ReactPanelErrorBoundary>) | ||
).not.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters