-
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.
Merge pull request #3355 from cpojer/shallowCompare
Add shallowCompare module and use it in PureRenderMixin + tests
- Loading branch information
Showing
4 changed files
with
176 additions
and
3 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
145 changes: 145 additions & 0 deletions
145
src/addons/__tests__/ReactComponentWithPureRenderMixin-test.js
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,145 @@ | ||
/** | ||
* Copyright 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 ReactComponentWithPureRenderMixin; | ||
var ReactTestUtils; | ||
|
||
describe('ReactComponentWithPureRenderMixin', function() { | ||
|
||
beforeEach(function() { | ||
React = require('React'); | ||
ReactComponentWithPureRenderMixin = | ||
require('ReactComponentWithPureRenderMixin'); | ||
ReactTestUtils = require("../../ReactTestUtils"); | ||
}); | ||
|
||
it('provides a default shouldComponentUpdate implementation', function() { | ||
var renderCalls = 0; | ||
class PlasticWrap extends React.Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
this.state = { | ||
color: 'green' | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<Apple | ||
color={this.state.color} | ||
ref="apple" | ||
/> | ||
); | ||
} | ||
} | ||
|
||
var Apple = React.createClass({ | ||
mixins: [ReactComponentWithPureRenderMixin], | ||
|
||
getInitialState: function() { | ||
return { | ||
cut: false, | ||
slices: 1, | ||
} | ||
}, | ||
|
||
cut: function() { | ||
this.setState({ | ||
cut: true, | ||
slices: 10, | ||
}); | ||
}, | ||
|
||
eatSlice: function() { | ||
this.setState({ | ||
slices: this.state.slices - 1, | ||
}); | ||
}, | ||
|
||
render: function() { | ||
renderCalls++; | ||
return <div />; | ||
} | ||
}); | ||
|
||
var instance = ReactTestUtils.renderIntoDocument(<PlasticWrap />); | ||
expect(renderCalls).toBe(1); | ||
|
||
// Do not re-render based on props | ||
instance.setState({color: 'green'}); | ||
expect(renderCalls).toBe(1); | ||
|
||
// Re-render based on props | ||
instance.setState({color: 'red'}); | ||
expect(renderCalls).toBe(2); | ||
|
||
// Re-render base on state | ||
instance.refs.apple.cut(); | ||
expect(renderCalls).toBe(3); | ||
|
||
// No re-render based on state | ||
instance.refs.apple.cut(); | ||
expect(renderCalls).toBe(3); | ||
|
||
// Re-render based on state again | ||
instance.refs.apple.eatSlice(); | ||
expect(renderCalls).toBe(4); | ||
}); | ||
|
||
it('does not do a deep comparison', function() { | ||
function getInitialState() { | ||
return { | ||
foo: [1, 2, 3], | ||
bar: {a: 4, b: 5, c: 6}, | ||
}; | ||
} | ||
|
||
var renderCalls = 0; | ||
var initialSettings = getInitialState(); | ||
|
||
var Component = React.createClass({ | ||
mixins: [ReactComponentWithPureRenderMixin], | ||
|
||
getInitialState: function() { | ||
return initialSettings; | ||
}, | ||
|
||
render: function() { | ||
renderCalls++; | ||
return <div />; | ||
} | ||
}); | ||
|
||
var instance = ReactTestUtils.renderIntoDocument(<Component />); | ||
expect(renderCalls).toBe(1); | ||
|
||
// Do not re-render if state is equal | ||
var settings = { | ||
foo: initialSettings.foo, | ||
bar: initialSettings.bar, | ||
}; | ||
instance.setState(settings); | ||
expect(renderCalls).toBe(1); | ||
|
||
// Re-render because one field changed | ||
initialSettings.foo = [1, 2, 3]; | ||
instance.setState(initialSettings); | ||
expect(renderCalls).toBe(2); | ||
|
||
// Re-render because the object changed | ||
instance.setState(getInitialState()); | ||
expect(renderCalls).toBe(3); | ||
}); | ||
|
||
}); |
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,27 @@ | ||
/** | ||
* 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. | ||
* | ||
* @providesModule shallowCompare | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var shallowEqual = require('shallowEqual'); | ||
|
||
/** | ||
* Does a shallow comparison for props and state. | ||
* See ReactComponentWithPureRenderMixin | ||
*/ | ||
function shallowCompare(instance, nextProps, nextState) { | ||
return ( | ||
!shallowEqual(instance.props, nextProps) || | ||
!shallowEqual(instance.state, nextState) | ||
); | ||
} | ||
|
||
module.exports = shallowCompare; |
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