Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto update wrappers behind a flag #1186

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd prefer to avoid simulate; can this do .prop('onClick')() instead?

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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])),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it might be a bugfix that should go in separately from an autoUpdate option?

);
return selector ? allParents.filter(selector) : allParents;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
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