Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: update exception when rise a element type error #1455

Merged
merged 6 commits into from
Nov 1, 2019
Merged
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
82 changes: 82 additions & 0 deletions packages/rax/src/vdom/__tests__/composite.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,44 @@ describe('CompositeComponent', function() {
]);
});

it('not break other component render when one component rise an error', () => {
let container = createNodeElement('div');
class MyComponent extends Component {
render() {
return [
<BrokenRender key="a" />,
<NoBrokenRender key="b" />
];
}
}

class BrokenRender extends Component {
constructor() {
throw new Error('Hello');
}
render() {
return (
<span>Hello 1</span>
);
}
}

class NoBrokenRender extends Component {
render() {
return (
<span>Hello 2</span>
);
}
}

expect(() => {
render(<MyComponent />, container);
jest.runAllTimers();
}).toThrowError(/Hello/);

expect(container.childNodes[0].childNodes[0].data).toBe('Hello 2');
});

it('catches render error in a boundary', () => {
let container = createNodeElement('div');
class ErrorBoundary extends Component {
Expand Down Expand Up @@ -247,6 +285,50 @@ describe('CompositeComponent', function() {
expect(container.childNodes[0].childNodes[0].data).toBe('Caught an error: Hello.');
});

it('catches component update error in a boundary', () => {
let container = createNodeElement('div');
class ErrorBoundary extends Component {
state = {error: null};
componentDidCatch(error) {
this.setState({error});
}
render() {
if (this.state.error) {
return (
<span>{`Caught an error: ${this.state.error.message}.`}</span>
);
}
return this.props.children;
}
}

class BrokenRender extends Component {
state = {str: 'Hello'};
componentDidMount() {
setTimeout(() => {
this.setState({
str: {
a: 2
}
});
});
}
render() {
return (
<span>{this.state.str}</span>
);
}
}

render(
<ErrorBoundary>
<BrokenRender />
</ErrorBoundary>, container);

jest.runAllTimers();
expect(container.childNodes[0].childNodes[0].data).toContain('Caught an error: Invalid element type');
});

it('catches lifeCycles errors in a boundary', () => {
let container = createNodeElement('div');
class ErrorBoundary extends Component {
Expand Down
Loading