Skip to content

Commit

Permalink
Fix missing ErrorBoundary in triggersActionsUi
Browse files Browse the repository at this point in the history
  • Loading branch information
umbopepato committed Feb 10, 2025
1 parent 997acf0 commit bcc81ec
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { render } from '@testing-library/react';
import { ErrorBoundary } from './error_boundary';

const FallbackComponent = () => <div>Fallback</div>;

const ComponentThatThrows = () => {
throw new Error();
};

describe('ErrorBoundary', () => {
it('should render its children when no error is thrown in its subtree', () => {
const { getByText } = render(
<ErrorBoundary fallback={FallbackComponent}>
<div>Children</div>
</ErrorBoundary>
);
expect(getByText('Children')).toBeInTheDocument();
});

it('should display the provided fallback component when an error is thrown in its subtree', () => {
const { getByText } = render(
<ErrorBoundary fallback={FallbackComponent}>
<ComponentThatThrows />
</ErrorBoundary>
);
expect(getByText('Fallback')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';

export type FallbackComponent = React.ComponentType<{ error: Error }>;

interface ErrorBoundaryProps {
children?: React.ReactNode;
fallback: FallbackComponent;
}

interface ErrorBoundaryState {
error: Error | null;
}

/**
* A local error boundary component with a configurable fallback UI
*/
export class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
state = {
error: null,
};

static getDerivedStateFromError(error: Error) {
return {
error,
};
}

render() {
if (this.state.error != null) {
const FallbackComponent = this.props.fallback;
return <FallbackComponent error={this.state.error} />;
}
return this.props.children ?? null;
}
}

0 comments on commit bcc81ec

Please sign in to comment.