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, fixes enzymejs#360
  • Loading branch information
jwbay committed Jul 8, 2016
1 parent 2dd924c commit 78f22ee
Show file tree
Hide file tree
Showing 5 changed files with 48 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
37 changes: 37 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3496,4 +3496,41 @@ describe('shallow', () => {
});
});

describe('out-of-band state updates', () => {
const promise = Promise.resolve();
const returnsPromise = () => promise;
const Child = () => <span />;

class Test extends React.Component {
asyncUpdate() {
returnsPromise().then(() => {
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', () => {
const wrapper = shallow(<Test />);
wrapper.find('.async-btn').simulate('click');
return promise.then(() => {
expect(wrapper.find('.show-me').length).to.equal(1);
});
});

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 78f22ee

Please sign in to comment.