Skip to content

Address sentry-javascript issue 17183 with tests #17229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react/src/errorboundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ function withErrorBoundary<P extends Record<string, any>>(
): React.FC<P> {
const componentDisplayName = WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;

const Wrapped: React.FC<P> = (props: P) => (
const Wrapped: React.FC<P> = React.memo((props: P) => (
<ErrorBoundary {...errorBoundaryOptions}>
<WrappedComponent {...props} />
</ErrorBoundary>
);
));

Wrapped.displayName = `errorBoundary(${componentDisplayName})`;

Expand Down
69 changes: 69 additions & 0 deletions packages/react/test/errorboundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,75 @@ describe('withErrorBoundary', () => {
const Component = withErrorBoundary(() => <h1>Hello World</h1>, { fallback: <h1>fallback</h1> });
expect(Component.displayName).toBe(`errorBoundary(${UNKNOWN_COMPONENT})`);
});

it('should not rerender when props are unchanged', () => {
let renderCount = 0;
const TestComponent = (props: { value: number }) => {
renderCount++;
return <div data-testid="test-component">Value: {props.value}</div>;
};

const WrappedComponent = withErrorBoundary(TestComponent, { fallback: <h1>fallback</h1> });

// Create a parent component that can trigger rerenders
const ParentComponent = () => {
const [parentState, setParentState] = useState(0);
return (
<div>
<button onClick={() => setParentState(prev => prev + 1)}>Parent Rerender</button>
<div>Parent State: {parentState}</div>
<WrappedComponent value={42} />
</div>
);
};

render(<ParentComponent />);

// Initial render should have rendered the test component
expect(renderCount).toBe(1);
expect(screen.getByTestId('test-component')).toHaveTextContent('Value: 42');

// Trigger parent rerender
fireEvent.click(screen.getByText('Parent Rerender'));

// The wrapped component should not have rerendered since props didn't change
expect(renderCount).toBe(1);
expect(screen.getByTestId('test-component')).toHaveTextContent('Value: 42');
});

it('should rerender when props change', () => {
let renderCount = 0;
const TestComponent = (props: { value: number }) => {
renderCount++;
return <div data-testid="test-component">Value: {props.value}</div>;
};

const WrappedComponent = withErrorBoundary(TestComponent, { fallback: <h1>fallback</h1> });

// Create a parent component that can change the props
const ParentComponent = () => {
const [value, setValue] = useState(42);
return (
<div>
<button onClick={() => setValue(prev => prev + 1)}>Change Value</button>
<WrappedComponent value={value} />
</div>
);
};

render(<ParentComponent />);

// Initial render
expect(renderCount).toBe(1);
expect(screen.getByTestId('test-component')).toHaveTextContent('Value: 42');

// Change the prop value
fireEvent.click(screen.getByText('Change Value'));

// The wrapped component should have rerendered since props changed
expect(renderCount).toBe(2);
expect(screen.getByTestId('test-component')).toHaveTextContent('Value: 43');
});
});

describe('ErrorBoundary', () => {
Expand Down
Loading