Skip to content

Commit

Permalink
fix: 🐛 if render/hydrate in same container drop old wrapper (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipoliko authored Oct 5, 2020
1 parent b1f1ed0 commit c7e3f9a
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions packages/plugin-testing-integration/EnzymePageRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class EnzymeReactDOM {
}

render(element, container, callback = () => {}) {
let wrapper = mount(element, { attachTo: container });
this.unmountComponentAtNode(container);

const wrapper = mount(element, { attachTo: container });

this._instances.push({ container, wrapper });

Expand All @@ -27,7 +29,9 @@ class EnzymeReactDOM {
}

hydrate(element, container, callback = () => {}) {
let wrapper = mount(element, { hydrateIn: container });
this._dropInstanceAtNode(container);

const wrapper = mount(element, { hydrateIn: container });

this._instances.push({ container, wrapper });

Expand All @@ -37,18 +41,13 @@ class EnzymeReactDOM {
}

unmountComponentAtNode(container) {
const instanceIndex = this._instances.findIndex(
instance => instance.container === container
);
const instance = this._dropInstanceAtNode(container);

if (!~instanceIndex) {
if (!instance) {
return false;
}

const { wrapper } = this._instances[instanceIndex];

this._instances.splice(instanceIndex, 1);
wrapper.detach();
instance.wrapper.detach();

return true;
}
Expand All @@ -61,6 +60,26 @@ class EnzymeReactDOM {
createPortal(...args) {
return ReactDOM.createPortal(...args);
}

_dropInstanceAtNode(container) {
const instanceIndex = this._getInstanceContainerIndex(container);

if (!~instanceIndex) {
return null;
}

const instance = this._instances[instanceIndex];

this._instances.splice(instanceIndex, 1);

return instance;
}

_getInstanceContainerIndex(container) {
return this._instances.findIndex(
instance => instance.container === container
);
}
}

class EnzymePageRenderer extends ClientPageRenderer {
Expand Down

0 comments on commit c7e3f9a

Please sign in to comment.