Support unmounting in ReactShallowRenderer
Reviewers: @sebmarkbage, @zpao Test Plan: ``` $ npm test ReactTestUtils ```
yungsters committed
Feb 18, 2015 at 17:34 UTC
e9e33b984fc022f68ec531826471fbd3a092e4b1
2 files changed
+24
-2
src/test/ReactTestUtils.js
+7
-2
@@ -334,7 +334,7 @@ var NoopInternalComponent = function(element) {
334
this._currentElement = element === null || element === false ?
335
ReactEmptyComponent.emptyElement :
336
element;
337
-}
337
+};
338
339
NoopInternalComponent.prototype = {
340
@@ -349,7 +349,6 @@ NoopInternalComponent.prototype = {
349
},
350
351
unmountComponent: function() {
352
-
352
}
353
354
};
@@ -374,6 +373,12 @@ ReactShallowRenderer.prototype.render = function(element, context) {
373
ReactUpdates.ReactReconcileTransaction.release(transaction);
374
};
375
376
+ReactShallowRenderer.prototype.unmount = function() {
377
+ if (this._instance) {
378
+ this._instance.unmountComponent();
379
+ }
380
+};
381
+
382
ReactShallowRenderer.prototype._render = function(element, transaction, context) {
383
if (!this._instance) {
384
var rootID = ReactInstanceHandles.createReactRootID();
src/test/__tests__/ReactTestUtils-test.js
+17
@@ -57,6 +57,23 @@ describe('ReactTestUtils', function() {
57
]);
58
});
59
60
+ it('should have shallow unmounting', function() {
61
+ var componentWillUnmount = mocks.getMockFunction();
62
+
63
+ var SomeComponent = React.createClass({
64
+ render: function() {
65
+ return <div />;
66
+ },
67
+ componentWillUnmount
68
+ });
69
+
70
+ var shallowRenderer = ReactTestUtils.createRenderer();
71
+ shallowRenderer.render(<SomeComponent />, {});
72
+ shallowRenderer.unmount();
73
+
74
+ expect(componentWillUnmount).toBeCalled();
75
+ });
76
+
77
it('can shallow render to null', function() {
78
var SomeComponent = React.createClass({
79
render: function() {