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 9, 2016
1 parent 811ec8d commit 67699b6
Show file tree
Hide file tree
Showing 5 changed files with 51 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
40 changes: 40 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3529,4 +3529,44 @@ describe('shallow', () => {
});
});

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

class Test extends React.Component {
asyncUpdate() {
setImmediate(() => {
this.setState({ showSpan: true });
});
}

render() {
return (
<div>
{this.state && this.state.showSpan && <span className="show-me" />}
<button className="async-btn" onClick={() => this.asyncUpdate()} />
<Child callback={() => this.setState({ 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 67699b6

Please sign in to comment.