Skip to content

Commit

Permalink
use getter for .node and .nodes in ShallowWrapper to ensure render ou…
Browse files Browse the repository at this point in the history
…tput is always fresh. removes need for .update
  • Loading branch information
jwbay committed Jul 10, 2016
1 parent 811ec8d commit d27d5e4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 51 deletions.
1 change: 0 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
* [text()](/docs/api/ShallowWrapper/text.md)
* [type()](/docs/api/ShallowWrapper/type.md)
* [unmount()](/docs/api/ShallowWrapper/unmount.md)
* [update()](/docs/api/ShallowWrapper/update.md)
* [Full DOM Rendering](/docs/api/mount.md)
* [at(index)](/docs/api/ReactWrapper/at.md)
* [contains(nodeOrNodes)](/docs/api/ReactWrapper/contains.md)
Expand Down
33 changes: 0 additions & 33 deletions docs/api/ShallowWrapper/update.md
Original file line number Diff line number Diff line change
@@ -1,33 +0,0 @@
# `.update() => Self`

Forces a re-render. Useful to run before checking the render output if something external
may be updating the state of the component somewhere.

NOTE: can only be called on a wrapper instance that is also the root instance.


#### Returns

`ShallowWrapper`: Returns itself.



#### Example

```jsx
class ImpureRender extends React.Component {
constructor(props) {
super(props);
this.count = 0;
}
render() {
return <div>{this.count++}</div>
}
}
```
```jsx
const wrapper = shallow(<ImpureRender />);
expect(wrapper.text()).to.equal("0");
wrapper.update();
expect(wrapper.text()).to.equal("1");
```
3 changes: 0 additions & 3 deletions docs/api/shallow.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ Manually sets context of the root component.
#### [`.instance() => ReactComponent`](ShallowWrapper/instance.md)
Returns the instance of the root component.

#### [`.update() => ShallowWrapper`](ShallowWrapper/update.md)
Calls `.forceUpdate()` on the root component instance.

#### [`.debug() => String`](ShallowWrapper/debug.md)
Returns a string representation of the current shallow render tree for debugging purposes.

Expand Down
25 changes: 11 additions & 14 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,16 @@ export default class ShallowWrapper {
}
});
});
this.node = this.renderer.getRenderOutput();
this.nodes = [this.node];
Object.defineProperty(this, 'node', {
get() {
return this.renderer.getRenderOutput();
},
});
Object.defineProperty(this, 'nodes', {
get() {
return [this.renderer.getRenderOutput()];
},
});
this.length = 1;
} else {
this.root = root;
Expand Down Expand Up @@ -121,21 +129,10 @@ export default class ShallowWrapper {
}

/**
* Forces a re-render. Useful to run before checking the render output if something external
* may be updating the state of the component somewhere.
*
* NOTE: can only be called on a wrapper instance that is also the root instance.
*
* @deprecated
* @returns {ShallowWrapper}
*/
update() {
if (this.root !== this) {
throw new Error('ShallowWrapper::update() can only be called on the root');
}
this.single(() => {
this.node = this.renderer.getRenderOutput();
this.nodes = [this.node];
});
return this;
}

Expand Down
47 changes: 47 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from 'chai';
import { shallow, render, ShallowWrapper } from '../src/';
import sinon from 'sinon';
import { describeIf, itIf, itWithData, generateEmptyRenderData } from './_helpers';
import { withSetStateAllowed } from '../src/Utils';
import { REACT013, REACT15 } from '../src/version';

describe('shallow', () => {
Expand Down Expand Up @@ -3529,4 +3530,50 @@ describe('shallow', () => {
});
});

describe('out-of-band state updates', () => {
class Child extends React.Component {
render() {
return <span />;
}
}

class Test extends React.Component {
safeSetState(newState) {
withSetStateAllowed(() => {
this.setState(newState);
});
}

asyncUpdate() {
setImmediate(() => {
this.safeSetState({ showSpan: true });
});
}

render() {
return (
<div>
{this.state && this.state.showSpan && <span className="show-me" />}
<button className="async-btn" onClick={() => this.asyncUpdate()} />
<Child callback={() => this.safeSetState({ showSpan: true })} />
</div>
);
}
}

it('should have updated output after an asynchronous setState', done => {
const wrapper = shallow(<Test />);
wrapper.find('.async-btn').simulate('click');
setImmediate(() => {
expect(wrapper.find('.show-me').length).to.equal(1);
done();
});
});

it('should have updated output after child prop callback invokes setState', () => {
const wrapper = shallow(<Test />);
wrapper.find(Child).props().callback();
expect(wrapper.find('.show-me').length).to.equal(1);
});
});
});

0 comments on commit d27d5e4

Please sign in to comment.