Skip to content

Commit

Permalink
auto update wrappers behind a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Sep 28, 2017
1 parent 106b5d1 commit 09ba714
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
49 changes: 49 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Expand Up @@ -3683,6 +3683,55 @@ describeWithDOM('mount', () => {
});
});

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

class Test extends React.Component {
componentWillMount() {
this.state = {};
}

asyncSetState() {
setImmediate(() => {
this.setState({ showSpan: true });
});
}

callbackSetState() {
this.setState({ showSpan: true });
}

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

it('should have updated output after an asynchronous setState', (done) => {
const wrapper = mount(<Test />, { autoUpdate: true });
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 = mount(<Test />, { autoUpdate: true });
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 = mount(<div><i /><i /></div>).children();
Expand Down
8 changes: 3 additions & 5 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -4446,7 +4446,7 @@ describe('shallow', () => {
});
});

describe('out-of-band state updates', () => {
describe('out-of-band state updates with autoUpdate', () => {
class Child extends React.Component {
render() {
return <span />;
Expand Down Expand Up @@ -4486,19 +4486,17 @@ describe('shallow', () => {
}

it('should have updated output after an asynchronous setState', (done) => {
const wrapper = shallow(<Test />);
const wrapper = shallow(<Test />, { autoUpdate: true });
wrapper.find('.async-btn').simulate('click');
setImmediate(() => {
wrapper.update();
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 />);
const wrapper = shallow(<Test />, { autoUpdate: true });
wrapper.find(Child).props().callback();
wrapper.update();
expect(wrapper.find('.show-me').length).to.equal(1);
});
});
Expand Down
10 changes: 9 additions & 1 deletion packages/enzyme/src/ReactWrapper.js
Expand Up @@ -124,6 +124,10 @@ class ReactWrapper {
'ReactWrapper::getNode() can only be called when wrapping one node',
);
}
if (this[ROOT] === this && this[OPTIONS].autoUpdate) {
this[NODE] = this[RENDERER].getNode();
this[NODES] = [this[NODE]];
}
return this[NODES][0];
}

Expand All @@ -133,6 +137,10 @@ class ReactWrapper {
* @return {Array<ReactComponent>}
*/
getNodesInternal() {
if (this[ROOT] === this && this[OPTIONS].autoUpdate) {
this[NODE] = this[RENDERER].getNode();
this[NODES] = [this[NODE]];
}
return this[NODES];
}

Expand Down Expand Up @@ -675,7 +683,7 @@ class ReactWrapper {
*/
parents(selector) {
const allParents = this.wrap(
this.single('parents', n => parentsOfNode(n, this[ROOT].getNodeInternal())),
this.single('parents', n => parentsOfNode(n, this[ROOT][NODE])),
);
return selector ? allParents.filter(selector) : allParents;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/enzyme/src/ShallowWrapper.js
Expand Up @@ -164,6 +164,10 @@ class ShallowWrapper {
'ShallowWrapper::getNode() can only be called when wrapping one node',
);
}
if (this[ROOT] === this && this[OPTIONS].autoUpdate) {
this[NODE] = getRootNode(this[RENDERER].getNode());
this[NODES] = [this[NODE]];
}
return this[NODE];
}

Expand Down Expand Up @@ -198,6 +202,10 @@ class ShallowWrapper {
}

getNodesInternal() {
if (this[ROOT] === this && this[OPTIONS].autoUpdate) {
this[NODE] = getRootNode(this[RENDERER].getNode());
this[NODES] = [this[NODE]];
}
return this[NODES];
}

Expand Down

0 comments on commit 09ba714

Please sign in to comment.