-
Notifications
You must be signed in to change notification settings - Fork 47.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright 2013-2015, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React; | ||
var ReactDOM; | ||
|
||
describe('ReactErrorBoundaries', function() { | ||
|
||
beforeEach(function() { | ||
ReactDOM = require('ReactDOM'); | ||
React = require('React'); | ||
}); | ||
|
||
it('catches errors from children', function() { | ||
var log = []; | ||
|
||
class Box extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {errorMessage: null}; | ||
} | ||
render() { | ||
if (this.state.errorMessage != null) { | ||
log.push('Box renderError'); | ||
return <div>Error: {this.state.errorMessage}</div>; | ||
} | ||
log.push('Box render'); | ||
var ref = function(x) { | ||
log.push('Inquisitive ref ' + x); | ||
}; | ||
return ( | ||
<div> | ||
<Inquisitive ref={ref} /> | ||
<Angry /> | ||
</div> | ||
); | ||
} | ||
handleError(e) { | ||
this.setState({errorMessage: e.message}); | ||
} | ||
componentDidMount() { | ||
log.push('Box componentDidMount'); | ||
} | ||
componentWillUnmount() { | ||
log.push('Box componentWillUnmount'); | ||
} | ||
} | ||
|
||
class Inquisitive extends React.Component { | ||
render() { | ||
log.push('Inquisitive render'); | ||
return <div>What is love?</div>; | ||
} | ||
componentDidMount() { | ||
log.push('Inquisitive componentDidMount'); | ||
} | ||
componentWillUnmount() { | ||
log.push('Inquisitive componentWillUnmount'); | ||
} | ||
} | ||
|
||
class Angry extends React.Component { | ||
render() { | ||
log.push('Angry render'); | ||
throw new Error('Please, do not render me.'); | ||
} | ||
componentDidMount() { | ||
log.push('Angry componentDidMount'); | ||
} | ||
componentWillUnmount() { | ||
log.push('Angry componentWillUnmount'); | ||
} | ||
} | ||
|
||
var container = document.createElement('div'); | ||
ReactDOM.render(<Box />, container); | ||
expect(container.textContent).toBe('Error: Please, do not render me.'); | ||
expect(log).toEqual([ | ||
'Box render', | ||
'Inquisitive render', | ||
'Angry render', | ||
'Inquisitive ref null', | ||
'Inquisitive componentWillUnmount', | ||
'Angry componentWillUnmount', | ||
'Box renderError', | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters