Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shallow rendering: create node mock #8592

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/docs/addons-test-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ Same as [`scryRenderedComponentsWithType()`](#scryrenderedcomponentswithtype) bu
### `createRenderer()`

```javascript
createRenderer()
createRenderer([options])
```

Call this in your tests to create a [shallow renderer](#shallow-rendering).
Call this in your tests to create a [shallow renderer](#shallow-rendering). Optionally pass `{createNodeMock: element => mock}` as an optional argument for creating mock refs.

* * *

Expand Down
24 changes: 21 additions & 3 deletions src/test/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class NoopInternalComponent {
this._debugID = getNextDebugID();
}
}
mountComponent() {}
mountComponent(transaction, hostParent, hostContainerInfo) {
this._hostContainerInfo = hostContainerInfo;
}
receiveComponent(element) {
this._renderedOutput = element;
this._currentElement = element;
Expand All @@ -42,7 +44,14 @@ class NoopInternalComponent {
return undefined;
}
getPublicInstance() {
return null;
var element = this._currentElement;
var hostContainerInfo = this._hostContainerInfo;
invariant(
hostContainerInfo,
'hostContainerInfo should be populated before ' +
'getPublicInstance is called.'
);
return hostContainerInfo.createNodeMock(element);
}
}

Expand Down Expand Up @@ -77,6 +86,14 @@ function _batchedRender(renderer, element, context) {

class ReactShallowRenderer {
_instance = null;
constructor(options) {
var defaultOptions = {
createNodeMock: function() {
return null;
},
};
this._options = Object.assign({}, defaultOptions, options);
}
getMountedInstance() {
return this._instance ? this._instance._instance : null;
}
Expand Down Expand Up @@ -136,7 +153,8 @@ class ReactShallowRenderer {
);
} else {
var instance = new ShallowComponentWrapper(element);
ReactReconciler.mountComponent(instance, transaction, null, null, context, 0);
ReactReconciler.mountComponent(instance, transaction, null, this._options, context, 0);
transaction.getReactMountReady().notifyAll();
this._instance = instance;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ var ReactTestUtils = {
};
},

createRenderer: function() {
return new ReactShallowRenderer();
createRenderer: function(options) {
return new ReactShallowRenderer(options);
},

Simulate: null,
Expand Down
27 changes: 27 additions & 0 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,33 @@ describe('ReactTestUtils', () => {
shallowRenderer.render(<SomeComponent />);
});

it('can shallow render with a ref and createNodeMock', () => {
var mockInstance = { click: () => {} };
function createNodeMock(element) {
switch (element.type) {
case 'div':
return mockInstance;
default:
return null;
}
}

var divRef = null;
class SomeComponent extends React.Component {
render() {
return (
<div ref={(node) => {
divRef = node;
}} />
);
}
}

var shallowRenderer = ReactTestUtils.createRenderer({createNodeMock});
shallowRenderer.render(<SomeComponent />);
expect(divRef).toBe(mockInstance);
});

it('lets you update shallowly rendered components', () => {
class SomeComponent extends React.Component {
state = {clicked: false};
Expand Down