/* eslint-disable no-console */
import React from 'react';

export const muteConsole = () => {
    let consoleError: Console['error'];
    beforeEach(() => {
        consoleError = console.error;
        console.error = jest.fn();
    });
    afterEach(() => {
        console.error = consoleError;
    });
};

interface Props {
    children?: React.ReactNode;
}

export class ErrorBoundary extends React.Component<Props, { error?: Error }> {
    constructor(props: Props) {
        super(props);
        this.state = { error: undefined };
    }

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

    componentDidCatch() {}

    render() {
        const { error } = this.state;

        if (error) return <div data-testid="error">{error.message}</div>;

        return this.props.children;
    }
}
