Skip to content

Commit

Permalink
Add renderIntoDocumentAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
jimfb authored and jim committed May 16, 2016
1 parent 151e1d7 commit d18ef57
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"plugins": [
"fbjs-scripts/babel-6/dev-expression",
"syntax-trailing-function-commas",
"babel-plugin-transform-async-to-generator",
"babel-plugin-transform-object-rest-spread",
"transform-es2015-template-literals",
"transform-es2015-literals",
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,9 @@
"unmockedModulePathPatterns": [
""
]
},
"dependencies": {
"babel-plugin-syntax-async-functions": "^6.8.0",
"babel-plugin-transform-async-to-generator": "^6.8.0"
}
}
28 changes: 26 additions & 2 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,38 @@ function findAllInRenderedTreeInternal(inst, test) {
* @lends ReactTestUtils
*/
var ReactTestUtils = {
renderIntoDocument: function(instance) {
renderIntoDocument: function(element) {
var div = document.createElement('div');
// None of our tests actually require attaching the container to the
// DOM, and doing so creates a mess that we rely on test isolation to
// clean up, so we're going to stop honoring the name of this method
// (and probably rename it eventually) if no problems arise.
// document.documentElement.appendChild(div);
return ReactDOM.render(instance, div);
return ReactDOM.render(element, div);
},

renderIntoDocumentAsync: function(element) {
invariant(typeof element.ref !== 'string',
'String refs can not be used at the top-level element of a render.'
);
var promise = new Promise(function(fulfill, reject) {
var div = document.createElement('div');
// None of our tests actually require attaching the container to the
// DOM, and doing so creates a mess that we rely on test isolation to
// clean up, so we're going to stop honoring the name of this method
// (and probably rename it eventually) if no problems arise.
// document.documentElement.appendChild(div);
var oldRef = element.ref;
var newRef = (instance) => {
if (oldRef) {
oldRef(instance);
}
fulfill(instance);
};
element = React.cloneElement(element, {ref: newRef});
ReactDOM.render(element, div);
});
return promise;
},

isElement: function(element) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,26 @@ describe('ReactTestUtils', function() {
expect(hrs.length).toBe(2);
});

it('will reject string refs in renderIntoDocumentAsync', function() {
expect(function() {
// String refs are illegal at the top level.
ReactTestUtils.renderIntoDocumentAsync(<div ref="foo" />);
}).toThrow('String refs can not be used at the top-level element of a render.');
});

pit('will fire a callback ref in renderIntoDocumentAsync', async function() {
var count = 0;
function ref(instance) {
count++;
expect(instance.tagName).toBe('DIV');
}
var instance = await ReactTestUtils.renderIntoDocumentAsync(<div ref={ref} />);
expect(instance.tagName).toBe('DIV');
expect(count).toBe(1);
});

pit('will not die if no ref in renderIntoDocumentAsync', async function() {
var instance = await ReactTestUtils.renderIntoDocumentAsync(<div />);
expect(instance.tagName).toBe('DIV');
});
});

0 comments on commit d18ef57

Please sign in to comment.