@@ -2378,6 +2378,86 @@ describeWithDOM('mount', () => {
23782378 } ) ;
23792379 } ) ;
23802380
2381+ describe ( '.simulateError(error)' , ( ) => {
2382+ class Div extends React . Component {
2383+ render ( ) {
2384+ return < div > { this . props . children } </ div > ;
2385+ }
2386+ }
2387+
2388+ class Spans extends React . Component {
2389+ render ( ) {
2390+ return < div > < span /> < span /> </ div > ;
2391+ }
2392+ }
2393+
2394+ class Nested extends React . Component {
2395+ render ( ) {
2396+ return < Div > < Spans /> </ Div > ;
2397+ }
2398+ }
2399+
2400+ it ( 'throws on host elements' , ( ) => {
2401+ const wrapper = mount ( < Div /> ) . find ( 'div' ) ;
2402+ expect ( wrapper . is ( 'div' ) ) . to . equal ( true ) ;
2403+ expect ( ( ) => wrapper . simulateError ( ) ) . to . throw ( ) ;
2404+ } ) ;
2405+
2406+ it ( 'throws on "not one" node' , ( ) => {
2407+ const wrapper = mount ( < Spans /> ) ;
2408+
2409+ const spans = wrapper . find ( 'span' ) ;
2410+ expect ( spans ) . to . have . lengthOf ( 2 ) ;
2411+ expect ( ( ) => spans . simulateError ( ) ) . to . throw ( ) ;
2412+
2413+ const navs = wrapper . find ( 'nav' ) ;
2414+ expect ( navs ) . to . have . lengthOf ( 0 ) ;
2415+ expect ( ( ) => navs . simulateError ( ) ) . to . throw ( ) ;
2416+ } ) ;
2417+
2418+ it ( 'throws when the renderer lacks `simulateError`' , ( ) => {
2419+ const wrapper = mount ( < Nested /> ) ;
2420+ delete wrapper [ sym ( '__renderer__' ) ] . simulateError ;
2421+ expect ( ( ) => wrapper . simulateError ( ) ) . to . throw ( ) ;
2422+ try {
2423+ wrapper . simulateError ( ) ;
2424+ } catch ( e ) {
2425+ expect ( e ) . not . to . equal ( undefined ) ;
2426+ }
2427+ } ) ;
2428+
2429+ it ( 'calls through to renderer’s `simulateError`' , ( ) => {
2430+ const wrapper = mount ( < Nested /> ) . find ( Div ) ;
2431+ const stub = sinon . stub ( ) . callsFake ( ( _ , __ , e ) => { throw e ; } ) ;
2432+ wrapper [ sym ( '__renderer__' ) ] . simulateError = stub ;
2433+ const error = new Error ( 'hi' ) ;
2434+ expect ( ( ) => wrapper . simulateError ( error ) ) . to . throw ( error ) ;
2435+ expect ( stub ) . to . have . property ( 'callCount' , 1 ) ;
2436+
2437+ const [ args ] = stub . args ;
2438+ expect ( args ) . to . have . lengthOf ( 3 ) ;
2439+ const [ hierarchy , rootNode , actualError ] = args ;
2440+ expect ( actualError ) . to . equal ( error ) ;
2441+ expect ( rootNode ) . to . eql ( wrapper [ sym ( '__root__' ) ] . getNodeInternal ( ) ) ;
2442+ expect ( hierarchy ) . to . have . lengthOf ( 2 ) ;
2443+ const [ divNode , spanNode ] = hierarchy ;
2444+ expect ( divNode ) . to . contain . keys ( {
2445+ type : Div ,
2446+ nodeType : 'class' ,
2447+ rendered : {
2448+ type : Spans ,
2449+ nodeType : 'class' ,
2450+ rendered : null ,
2451+ } ,
2452+ } ) ;
2453+ expect ( spanNode ) . to . contain . keys ( {
2454+ type : Spans ,
2455+ nodeType : 'class' ,
2456+ rendered : null ,
2457+ } ) ;
2458+ } ) ;
2459+ } ) ;
2460+
23812461 describe ( '.setState(newState[, callback])' , ( ) => {
23822462 it ( 'throws on a non-function callback' , ( ) => {
23832463 class Foo extends React . Component {
@@ -4511,6 +4591,118 @@ describeWithDOM('mount', () => {
45114591 } ) ;
45124592 } ) ;
45134593
4594+ describeIf ( is ( '>= 16' ) , 'componentDidCatch' , ( ) => {
4595+ describe ( 'errors inside an error boundary' , ( ) => {
4596+ const errorToThrow = new EvalError ( 'threw an error!' ) ;
4597+ // in React 16.0 - 16.2, and some older nodes, the actual error thrown isn't reported.
4598+ const reactError = new Error ( 'An error was thrown inside one of your components, but React doesn\'t know what it was. This is likely due to browser flakiness. React does its best to preserve the "Pause on exceptions" behavior of the DevTools, which requires some DEV-mode only tricks. It\'s possible that these don\'t work in your browser. Try triggering the error in production mode, or switching to a modern browser. If you suspect that this is actually an issue with React, please file an issue.' ) ;
4599+ const properErrorMessage = error => error instanceof Error && (
4600+ error . message === errorToThrow . message
4601+ || error . message === reactError . message
4602+ ) ;
4603+
4604+ const hasFragments = is ( '>= 16.2' ) ;
4605+ const MaybeFragment = hasFragments ? Fragment : 'main' ;
4606+
4607+ function Thrower ( { throws } ) {
4608+ if ( throws ) {
4609+ throw errorToThrow ;
4610+ }
4611+ return null ;
4612+ }
4613+
4614+ class ErrorBoundary extends React . Component {
4615+ constructor ( ...args ) {
4616+ super ( ...args ) ;
4617+ this . state = { throws : false } ;
4618+ }
4619+
4620+ componentDidCatch ( error , info ) {
4621+ const { spy } = this . props ;
4622+ spy ( error , info ) ;
4623+ this . setState ( { throws : false } ) ;
4624+ }
4625+
4626+ render ( ) {
4627+ const { throws } = this . state ;
4628+ return (
4629+ < div >
4630+ < MaybeFragment >
4631+ < span >
4632+ < Thrower throws = { throws } />
4633+ </ span >
4634+ </ MaybeFragment >
4635+ </ div >
4636+ ) ;
4637+ }
4638+ }
4639+
4640+ describe ( 'Thrower' , ( ) => {
4641+ it ( 'does not throw when `throws` is `false`' , ( ) => {
4642+ expect ( ( ) => mount ( < Thrower throws = { false } /> ) ) . not . to . throw ( ) ;
4643+ } ) ;
4644+
4645+ it ( 'throws when `throws` is `true`' , ( ) => {
4646+ expect ( ( ) => mount ( < Thrower throws /> ) ) . to . throw ( ) ;
4647+ try {
4648+ mount ( < Thrower throws /> ) ;
4649+ expect ( true ) . to . equal ( false , 'this line should not be reached' ) ;
4650+ } catch ( e ) {
4651+ expect ( e ) . to . satisfy ( properErrorMessage ) ;
4652+ }
4653+ } ) ;
4654+ } ) ;
4655+
4656+ it ( 'catches a simulated error' , ( ) => {
4657+ const spy = sinon . spy ( ) ;
4658+ const wrapper = mount ( < ErrorBoundary spy = { spy } /> ) ;
4659+
4660+ expect ( spy ) . to . have . property ( 'callCount' , 0 ) ;
4661+
4662+ expect ( ( ) => wrapper . find ( Thrower ) . simulateError ( errorToThrow ) ) . not . to . throw ( ) ;
4663+
4664+ expect ( spy ) . to . have . property ( 'callCount' , 1 ) ;
4665+
4666+ expect ( spy . args ) . to . be . an ( 'array' ) . and . have . lengthOf ( 1 ) ;
4667+ const [ [ actualError , info ] ] = spy . args ;
4668+ expect ( actualError ) . to . equal ( errorToThrow ) ;
4669+ expect ( info ) . to . deep . equal ( {
4670+ componentStack : `
4671+ in Thrower (created by ErrorBoundary)
4672+ in span (created by ErrorBoundary)${ hasFragments ? '' : `
4673+ in main (created by ErrorBoundary)` }
4674+ in div (created by ErrorBoundary)
4675+ in ErrorBoundary (created by WrapperComponent)
4676+ in WrapperComponent` ,
4677+ } ) ;
4678+ } ) ;
4679+
4680+ it ( 'catches errors during render' , ( ) => {
4681+ const spy = sinon . spy ( ) ;
4682+ const wrapper = mount ( < ErrorBoundary spy = { spy } /> ) ;
4683+
4684+ expect ( spy ) . to . have . property ( 'callCount' , 0 ) ;
4685+
4686+ wrapper . setState ( { throws : true } ) ;
4687+
4688+ expect ( spy ) . to . have . property ( 'callCount' , 1 ) ;
4689+
4690+ expect ( spy . args ) . to . be . an ( 'array' ) . and . have . lengthOf ( 1 ) ;
4691+ const [ [ actualError , info ] ] = spy . args ;
4692+ expect ( actualError ) . to . satisfy ( properErrorMessage ) ;
4693+ expect ( info ) . to . deep . equal ( {
4694+ componentStack : `
4695+ in Thrower (created by ErrorBoundary)
4696+ in span (created by ErrorBoundary)${ hasFragments ? '' : `
4697+ in main (created by ErrorBoundary)` }
4698+ in div (created by ErrorBoundary)
4699+ in ErrorBoundary (created by WrapperComponent)
4700+ in WrapperComponent` ,
4701+ } ) ;
4702+ } ) ;
4703+ } ) ;
4704+ } ) ;
4705+
45144706 context ( 'mounting phase' , ( ) => {
45154707 it ( 'calls componentWillMount and componentDidMount' , ( ) => {
45164708 const spy = sinon . spy ( ) ;
0 commit comments