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 Oct 24, 2016
1 parent 72c4dd4 commit 8e9ace4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 52 deletions.
1 change: 0 additions & 1 deletion docs/README.md
Expand Up @@ -67,7 +67,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
@@ -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
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
Expand Up @@ -84,8 +84,16 @@ 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 @@ -125,21 +133,10 @@ 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('update', () => {
this.node = this.renderer.getRenderOutput();
this.nodes = [this.node];
});
return this;
}

Expand Down
49 changes: 48 additions & 1 deletion test/ShallowWrapper-spec.jsx
Expand Up @@ -4,7 +4,7 @@ import sinon from 'sinon';

import { shallow, render, ShallowWrapper } from '../src/';
import { describeIf, itIf, itWithData, generateEmptyRenderData } from './_helpers';
import { ITERATOR_SYMBOL } from '../src/Utils';
import { ITERATOR_SYMBOL, withSetStateAllowed } from '../src/Utils';
import { REACT013, REACT15 } from '../src/version';

describe('shallow', () => {
Expand Down Expand Up @@ -3718,6 +3718,53 @@ 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);
});
});

describe('#single()', () => {
it('throws if run on multiple nodes', () => {
const wrapper = shallow(<div><i /><i /></div>).children();
Expand Down

0 comments on commit 8e9ace4

Please sign in to comment.