Skip to content

Commit

Permalink
remove need for ShallowWrapper#update
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Aug 13, 2017
1 parent 3db226c commit df609cf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ class ShallowWrapper {
'ShallowWrapper::getNode() can only be called when wrapping one node',
);
}
if (this.root === this) {
this.node = getRootNode(this.renderer.getNode());
this.nodes = [this.node];
}
return this.node;
}

Expand All @@ -160,6 +164,10 @@ class ShallowWrapper {
* @return {Array<ReactElement>}
*/
getNodes() {
if (this.root === this) {
this.node = getRootNode(this.renderer.getNode());
this.nodes = [this.node];
}
return this.nodes;
}

Expand Down
7 changes: 6 additions & 1 deletion src/adapters/ReactFifteenFourAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
const renderer = TestUtils.createRenderer();
let isDOM = false;
let cachedNode = null;
let cachedOutput = null;
return {
render(el, context) {
cachedNode = el;
Expand All @@ -132,7 +133,11 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
},
getNode() {
if (isDOM) {
return elementToTree(cachedNode);
// a shallow wrapper around a DOM node can never be updated.
// this is mainly to preserve reference equality that some
// tests were asserting on though
cachedOutput = cachedOutput || elementToTree(cachedNode);
return cachedOutput;
}
const output = renderer.getRenderOutput();
return {
Expand Down
10 changes: 4 additions & 6 deletions test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4156,10 +4156,10 @@ describe('shallow', () => {
const b1 = wrapper.find('a').get(1);
const c1 = wrapper.find('a').get(2);
const d1 = wrapper.find('a').get(3);
expect(a1).to.equal(a);
expect(b1).to.equal(b);
expect(c1).to.equal(c);
expect(d1).to.equal(d);
expect(a1).to.eql(a);
expect(b1).to.eql(b);
expect(c1).to.eql(c);
expect(d1).to.eql(d);
});
});

Expand Down Expand Up @@ -4253,7 +4253,6 @@ describe('shallow', () => {
const wrapper = shallow(<Test />);
wrapper.find('.async-btn').simulate('click');
setImmediate(() => {
wrapper.update(); // TODO(lmr): this is a breaking change...
expect(wrapper.find('.show-me').length).to.equal(1);
done();
});
Expand All @@ -4262,7 +4261,6 @@ describe('shallow', () => {
it('should have updated output after child prop callback invokes setState', () => {
const wrapper = shallow(<Test />);
wrapper.find(Child).props().callback();
wrapper.update(); // TODO(lmr): this is a breaking change...
expect(wrapper.find('.show-me').length).to.equal(1);
});
});
Expand Down

0 comments on commit df609cf

Please sign in to comment.