Skip to content

Commit a7ab8d7

Browse files
wenboyu2sapegin
authored andcommitted
Fix: Preview should unmount the Wrapper component when it unmounts (styleguidist#540)
1 parent a010018 commit a7ab8d7

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/rsg-components/Preview/Preview.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ export default class Preview extends Component {
6262
}
6363
}
6464

65+
componentWillUnmount() {
66+
if (!this.mountNode) {
67+
return;
68+
}
69+
ReactDOM.unmountComponentAtNode(this.mountNode);
70+
}
71+
6572
executeCode() {
6673
this.setState({
6774
error: null,

src/rsg-components/Preview/Preview.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ const options = {
1111
},
1212
};
1313

14+
it('should unmount Wrapper when Preview unmounts and mountNodeMock is not null', () => {
15+
const actual = mount(<Preview code={code} evalInContext={() => noop} />, options);
16+
actual.instance().mountNode = { hi: 1 };
17+
const ReactDOM = require('react-dom');
18+
const unmountComponentAtNodeMock = jest.fn();
19+
ReactDOM.unmountComponentAtNode = unmountComponentAtNodeMock;
20+
21+
actual.unmount();
22+
23+
expect(unmountComponentAtNodeMock).toBeCalled();
24+
});
25+
26+
it('should not unmount Wrapper when Preview unmounts and mountNodeMock is null', () => {
27+
const actual = mount(<Preview code={code} evalInContext={() => noop} />, options);
28+
actual.instance().mountNode = null;
29+
const ReactDOM = require('react-dom');
30+
const unmountComponentAtNodeMock = jest.fn();
31+
ReactDOM.unmountComponentAtNode = unmountComponentAtNodeMock;
32+
33+
actual.unmount();
34+
35+
expect(unmountComponentAtNodeMock).not.toBeCalled();
36+
});
37+
1438
it('should render component renderer', () => {
1539
const actual = shallow(<Preview code={code} evalInContext={noop} />, options);
1640

0 commit comments

Comments
 (0)