Skip to content

Commit

Permalink
fixing more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jan 3, 2025
1 parent b091fe9 commit 1f00cb3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ test('should render an error message', () => {
<Child />
</ErrorBoundary>,
);
expect(screen.getAllByText('Unexpected error')).toBeGreaterThan(0);
expect(screen.getAllByText('Unexpected error').length).toBeGreaterThan(0);
});
121 changes: 63 additions & 58 deletions superset-frontend/src/components/ErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,84 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information.
* The ASF licenses this file to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations
* under the License.
*/
import { Component, ErrorInfo, ReactNode } from 'react';

import { Component, ReactNode, ErrorInfo, useState } from 'react';
import { t } from '@superset-ui/core';
import ErrorMessageWithStackTrace from 'src/components/ErrorMessage/ErrorMessageWithStackTrace';
import ErrorAlert from 'src/components/ErrorMessage/ErrorAlert';

export interface ErrorBoundaryProps {
interface ErrorBoundaryProps {
children: ReactNode;
onError?: (error: Error, info: ErrorInfo) => void;
showMessage?: boolean;
}

interface ErrorBoundaryState {
error: Error | null;
info: ErrorInfo | null;
}

export default class ErrorBoundary extends Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
static defaultProps: Partial<ErrorBoundaryProps> = {
showMessage: true,
};

constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { error: null, info: null };
/**
* A class component for handling React error boundaries.
*/
class ErrorBoundaryFallback extends Component<ErrorBoundaryProps> {
componentDidCatch(error: Error, info: ErrorInfo) {
const { onError } = this.props;
if (onError) {
onError(error, info);
}
}

componentDidCatch(error: Error, info: ErrorInfo): void {
this.props.onError?.(error, info);
this.setState({ error, info });
render() {
return this.props.children;
}
}

render() {
const { error, info } = this.state;
if (error) {
const firstLine = error.toString();
const messageString = `${t('Unexpected error')}${
firstLine ? `: ${firstLine}` : ''
}`;
const messageElement = (
<span>
<strong>{t('Unexpected error')}</strong>
{firstLine ? `: ${firstLine}` : ''}
</span>
);
/**
* ErrorBoundary component to handle unexpected errors and display an error message if needed.
*/
const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({
children,
onError,
showMessage = true,
}) => {
const [error, setError] = useState<Error | null>(null);
const [info, setInfo] = useState<ErrorInfo | null>(null);

if (this.props.showMessage) {
return (
<ErrorMessageWithStackTrace
subtitle={messageElement}
copyText={messageString}
stackTrace={info?.componentStack}
/>
);
}
return null;
const handleError = (caughtError: Error, caughtInfo: ErrorInfo) => {
setError(caughtError);
setInfo(caughtInfo);
if (onError) {
onError(caughtError, caughtInfo);
}
return this.props.children;
};

if (error) {
const errorMessage = error.toString();
if (showMessage) {
return (
<ErrorAlert
errorType={t('Unexpected error')}
message={errorMessage}
descriptionDetails={info?.componentStack || errorMessage}
/>
);
}
return null;
}
}

return (
<ErrorBoundaryFallback handleError={handleError}>
{children}
</ErrorBoundaryFallback>
);
};

export default ErrorBoundary;
36 changes: 14 additions & 22 deletions superset-frontend/src/features/home/EmptyState.test.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { styledMount as mount } from 'spec/helpers/theming';
import { TableTab } from 'src/views/CRUD/types';
import EmptyState, { EmptyStateProps } from './EmptyState';
Expand Down Expand Up @@ -62,11 +44,14 @@ describe('EmptyState', () => {
tableName: WelcomeTable.Recents,
},
];

variants.forEach(variant => {
it(`it renders an ${variant.tab} ${variant.tableName} empty state`, () => {
it(`renders an ${variant.tab} ${variant.tableName} empty state`, () => {
const wrapper = mount(<EmptyState {...variant} />);
expect(wrapper).toExist();
const textContainer = wrapper.find('.ant-empty-description');

// Select the first description node
const textContainer = wrapper.find('.ant-empty-description').at(0);
expect(textContainer.text()).toEqual(
variant.tab === TableTab.Favorite
? "You don't have any favorites yet!"
Expand All @@ -79,12 +64,19 @@ describe('EmptyState', () => {
expect(wrapper.find('button')).toHaveLength(1);
});
});

recents.forEach(recent => {
it(`it renders a ${recent.tab} ${recent.tableName} empty state`, () => {
it(`renders a ${recent.tab} ${recent.tableName} empty state`, () => {
const wrapper = mount(<EmptyState {...recent} />);
expect(wrapper).toExist();
const textContainer = wrapper.find('.ant-empty-description');

// Select the first description node
const textContainer = wrapper.find('.ant-empty-description').at(0);

// Validate the image
expect(wrapper.find('.ant-empty-image').children()).toHaveLength(1);

// Check the correct text is displayed
expect(textContainer.text()).toContain(
`Recently ${recent.tab?.toLowerCase()} charts, dashboards, and saved queries will appear here`,
);
Expand Down

0 comments on commit 1f00cb3

Please sign in to comment.