forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shallow rendering support (facebook#2393)
Now handles updating. Haven't looked at refs yet.
- Loading branch information
Scott Feeney
committed
Nov 17, 2014
1 parent
e4218cb
commit bfadafe
Showing
4 changed files
with
309 additions
and
12 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* Copyright 2013-2014, 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 ReactTestUtils; | ||
|
||
var mocks; | ||
var warn; | ||
|
||
describe('ReactTestUtils', function() { | ||
|
||
beforeEach(function() { | ||
mocks = require('mocks'); | ||
|
||
React = require('React'); | ||
ReactTestUtils = require('ReactTestUtils'); | ||
|
||
warn = console.warn; | ||
console.warn = mocks.getMockFunction(); | ||
}); | ||
|
||
afterEach(function() { | ||
console.warn = warn; | ||
}); | ||
|
||
it('should have shallow rendering', function() { | ||
var SomeComponent = React.createClass({ | ||
render: function() { | ||
return ( | ||
<div> | ||
<span className="child1" /> | ||
<span className="child2" /> | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
var shallowRenderer = ReactTestUtils.createRenderer(); | ||
shallowRenderer.render(<SomeComponent />); | ||
|
||
var result = shallowRenderer.getRenderOutput(); | ||
|
||
expect(result.type).toBe('div'); | ||
expect(result.props.children).toEqual([ | ||
<span className="child1" />, | ||
<span className="child2" /> | ||
]); | ||
}); | ||
|
||
it('lets you update shallowly rendered components', function() { | ||
var SomeComponent = React.createClass({ | ||
getInitialState: function() { | ||
return {clicked: false}; | ||
}, | ||
|
||
onClick: function() { | ||
this.setState({clicked: true}); | ||
}, | ||
|
||
render: function() { | ||
var className = this.state.clicked ? 'was-clicked' : ''; | ||
|
||
if (this.props.aNew === 'prop') { | ||
return ( | ||
<a | ||
href="#" | ||
onClick={this.onClick} | ||
className={className}> | ||
Test link | ||
</a> | ||
); | ||
} else { | ||
return ( | ||
<div> | ||
<span className="child1" /> | ||
<span className="child2" /> | ||
</div> | ||
); | ||
} | ||
} | ||
}); | ||
|
||
var shallowRenderer = ReactTestUtils.createRenderer(); | ||
shallowRenderer.render(<SomeComponent />); | ||
var result = shallowRenderer.getRenderOutput(); | ||
expect(result.type).toBe('div'); | ||
expect(result.props.children).toEqual([ | ||
<span className="child1" />, | ||
<span className="child2" /> | ||
]); | ||
|
||
shallowRenderer.render(<SomeComponent aNew="prop" />); | ||
var updatedResult = shallowRenderer.getRenderOutput(); | ||
expect(updatedResult.type).toBe('a'); | ||
|
||
var mockEvent = {}; | ||
updatedResult.props.onClick(mockEvent); | ||
|
||
var updatedResultCausedByClick = shallowRenderer.getRenderOutput(); | ||
expect(updatedResultCausedByClick.type).toBe('a'); | ||
expect(updatedResultCausedByClick.props.className).toBe('was-clicked'); | ||
}); | ||
}); |