Skip to content
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

[EuiErrorBoundary] Add default data-test-subj #5232

Merged
merged 8 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `39.0.0`.
- Added a default `data-test-subj` to `EuiErrorBoundary` ([#5232](https://github.com/elastic/eui/pull/5232))

## [`39.0.0`](https://github.com/elastic/eui/tree/v39.0.0)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiErrorBoundary is rendered without an error 1`] = `
exports[`EuiErrorBoundary without an error thrown UI is not rendered 1`] = `
<div>
No error
</div>
Expand Down
56 changes: 37 additions & 19 deletions src/components/error_boundary/error_boundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,48 @@ const BadComponent = () => {
};

describe('EuiErrorBoundary', () => {
test('is rendered without an error', () => {
const component = takeMountedSnapshot(
mount(
describe('without an error thrown', () => {
it('UI is not rendered', () => {
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
const component = takeMountedSnapshot(
mount(
<EuiErrorBoundary {...requiredProps}>
<GoodComponent />
</EuiErrorBoundary>
)
);

expect(component).toMatchSnapshot();
});
});

describe('with an error thrown', () => {
it('UI is rendered', () => {
// Prevent the React boundary error from appearing in the terminal.
spyOn(console, 'error');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha this blows my mind, I've never actually used spyOn without the jest.spyOn prefix, but I guess it makes sense that it's a global 🤯

I am slightly confused by the comment though, AFAIK jest.spyOn doesn't silence anything without using jest.spyOn(something).mockImplementation(() => {}), so unless I'm wrong we're not actually using for the comment's purpose?

(feel free to merge w/o addressing this comment also as I know it's previous code + not a blocker)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welp, it turns out that the comment is entirely false. Removing spyOn(console, 'error'); does not result in error messages in the console. Will remove.

Thanks for the question!


// Because the error contains the stack trace, it's non-deterministic. So we'll just check that
// it contains our error message.
const errorText = mount(
<EuiErrorBoundary {...requiredProps}>
<GoodComponent />
<BadComponent />
</EuiErrorBoundary>
)
);
).text();

expect(component).toMatchSnapshot();
});
expect(errorText).toContain(errorMessage);
});

test('is rendered with an error', () => {
// Prevent the React boundary error from appearing in the terminal.
spyOn(console, 'error'); // eslint-disable-line no-undef
it('data-test-subj is rendered', () => {
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
// Prevent the React boundary error from appearing in the terminal.
spyOn(console, 'error');

// Because the error contains the stack trace, it's non-deterministic. So we'll just check that
// it contains our error message.
const errorText = mount(
<EuiErrorBoundary {...requiredProps}>
<BadComponent />
</EuiErrorBoundary>
).text();
const errorHtml = mount(
<EuiErrorBoundary {...requiredProps}>
<BadComponent />
</EuiErrorBoundary>
).html();

expect(errorText).toContain(errorMessage);
expect(errorHtml).toContain('euiErrorBoundary');
expect(errorHtml).toContain('test subject string');
});
});
});
15 changes: 8 additions & 7 deletions src/components/error_boundary/error_boundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import React, { Component, HTMLAttributes, ReactNode } from 'react';
import { CommonProps } from '../common';
import PropTypes from 'prop-types';
import classNames from 'classnames';

import { EuiText } from '../text';

Expand All @@ -29,10 +29,6 @@ export class EuiErrorBoundary extends Component<
EuiErrorBoundaryProps,
EuiErrorBoundaryState
> {
static propTypes = {
children: PropTypes.node,
};

cee-chen marked this conversation as resolved.
Show resolved Hide resolved
constructor(props: EuiErrorBoundaryProps) {
super(props);

Expand All @@ -59,12 +55,17 @@ ${stackStr}`;
}

render() {
const { children, ...rest } = this.props;
const { children, 'data-test-subj': _dataTestSubj, ...rest } = this.props;
const dataTestSubj = classNames('euiErrorBoundary', _dataTestSubj);

if (this.state.hasError) {
// You can render any custom fallback UI
return (
<div className="euiErrorBoundary" {...rest}>
<div
className="euiErrorBoundary"
data-test-subj={dataTestSubj}
{...rest}
>
<div className="euiErrorBoundary__text">
<EuiText size="xs">
<h1>Error</h1>
Expand Down