From dfd128989f959a9a6d2d4358b36f23292fce0746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Bare=C5=82kowski?= Date: Mon, 11 Mar 2019 22:28:01 +0100 Subject: [PATCH] [Docs] update `simulateError` with `getDerivedStateFromError` --- docs/api/ReactWrapper/simulateError.md | 17 +++++++++++++++-- docs/api/ShallowWrapper/simulateError.md | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/api/ReactWrapper/simulateError.md b/docs/api/ReactWrapper/simulateError.md index c34b8c2df..451618ab3 100644 --- a/docs/api/ReactWrapper/simulateError.md +++ b/docs/api/ReactWrapper/simulateError.md @@ -2,7 +2,7 @@ Simulate a component throwing an error as part of its rendering lifecycle. -This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` lifecycle method). +This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` and `static getDerivedStateFromError` lifecycle methods). #### Arguments @@ -26,6 +26,17 @@ function Something() { } class ErrorBoundary extends React.Component { + static getDerivedStateFromError(error) { + return { + hasError: true, + }; + } + + constructor(props) { + super(props); + this.state = { hasError: false }; + } + componentDidCatch(error, info) { const { spy } = this.props; spy(error, info); @@ -33,9 +44,10 @@ class ErrorBoundary extends React.Component { render() { const { children } = this.props; + const { hasError } = this.state; return ( - {children} + {hasError ? 'Error' : children} ); } @@ -50,6 +62,7 @@ const wrapper = mount(); const error = new Error('hi!'); wrapper.find(Something).simulateError(error); +expect(wrapper.state()).to.have.property('hasError', true); expect(spy).to.have.property('callCount', 1); expect(spy.args).to.deep.equal([ error, diff --git a/docs/api/ShallowWrapper/simulateError.md b/docs/api/ShallowWrapper/simulateError.md index cdc0331d8..e8f7b71dd 100644 --- a/docs/api/ShallowWrapper/simulateError.md +++ b/docs/api/ShallowWrapper/simulateError.md @@ -2,7 +2,7 @@ Simulate a component throwing an error as part of its rendering lifecycle. -This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` lifecycle method). +This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` and `static getDerivedStateFromError` lifecycle methods). #### Arguments @@ -26,6 +26,17 @@ function Something() { } class ErrorBoundary extends React.Component { + static getDerivedStateFromError(error) { + return { + hasError: true, + }; + } + + constructor(props) { + super(props); + this.state = { hasError: false }; + } + componentDidCatch(error, info) { const { spy } = this.props; spy(error, info); @@ -33,9 +44,10 @@ class ErrorBoundary extends React.Component { render() { const { children } = this.props; + const { hasError } = this.state; return ( - {children} + {hasError ? 'Error' : children} ); } @@ -50,6 +62,7 @@ const wrapper = shallow(); const error = new Error('hi!'); wrapper.find(Something).simulateError(error); +expect(wrapper.state()).to.have.property('hasError', true); expect(spy).to.have.property('callCount', 1); expect(spy.args).to.deep.equal([ error,