Skip to content

Commit 7aae6bb

Browse files
committed
Test: More realistic tests
1 parent 2552f49 commit 7aae6bb

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import '../../styles/setupjss';
55
import slots, { EXAMPLE_TAB_CODE_EDITOR } from '../slots';
66
import { PlaygroundRenderer } from './PlaygroundRenderer';
77

8+
const evalInContext = a =>
9+
new Function('require', 'const React = require("react");' + a).bind(null, require); // eslint-disable-line no-new-func
810
const code = '<button>OK</button>';
911
const newCode = '<button>Not OK</button>';
1012
const options = {
@@ -22,7 +24,7 @@ const options = {
2224

2325
it('should render component renderer', () => {
2426
const actual = shallow(
25-
<Playground code={code} evalInContext={a => () => a} name="name" index={0} />,
27+
<Playground code={code} evalInContext={evalInContext} name="name" index={0} />,
2628
options
2729
);
2830

@@ -31,7 +33,7 @@ it('should render component renderer', () => {
3133

3234
it('should update code via props', () => {
3335
const actual = shallow(
34-
<Playground code={code} evalInContext={a => () => a} name="name" index={0} />,
36+
<Playground code={code} evalInContext={evalInContext} name="name" index={0} />,
3537
options
3638
);
3739

@@ -45,15 +47,18 @@ it('should update code via props', () => {
4547
});
4648

4749
it('should update code with debounce', done => {
48-
const actual = shallow(<Playground code={code} evalInContext={a => a} name="name" index={0} />, {
49-
context: {
50-
...options.context,
51-
config: {
52-
...options.context.config,
53-
previewDelay: 1,
50+
const actual = shallow(
51+
<Playground code={code} evalInContext={evalInContext} name="name" index={0} />,
52+
{
53+
context: {
54+
...options.context,
55+
config: {
56+
...options.context.config,
57+
previewDelay: 1,
58+
},
5459
},
55-
},
56-
});
60+
}
61+
);
5762

5863
expect(actual.state('code')).toEqual(code);
5964

@@ -68,7 +73,7 @@ it('should update code with debounce', done => {
6873

6974
it('should open a code editor', () => {
7075
const actual = mount(
71-
<Playground code={code} evalInContext={a => () => a} name="name" index={0} />,
76+
<Playground code={code} evalInContext={evalInContext} name="name" index={0} />,
7277
options
7378
);
7479

src/rsg-components/Preview/Preview.js

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

6565
componentWillUnmount() {
66-
if (!this.mountNode) {
67-
return;
66+
this.unmountPreview();
67+
}
68+
69+
unmountPreview() {
70+
if (this.mountNode) {
71+
ReactDOM.unmountComponentAtNode(this.mountNode);
6872
}
69-
ReactDOM.unmountComponentAtNode(this.mountNode);
7073
}
7174

7275
executeCode() {
@@ -127,9 +130,7 @@ export default class Preview extends Component {
127130
}
128131

129132
handleError(err) {
130-
if (this.mountNode) {
131-
ReactDOM.unmountComponentAtNode(this.mountNode);
132-
}
133+
this.unmountPreview();
133134

134135
this.setState({
135136
error: err.toString(),
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import React from 'react';
2-
import noop from 'lodash/noop';
32
import Preview from '../Preview';
43

4+
/* eslint-disable no-console */
5+
6+
const evalInContext = a =>
7+
new Function('require', 'const React = require("react");' + a).bind(null, require); // eslint-disable-line no-new-func
58
const code = '<button>OK</button>';
69
const options = {
710
context: {
@@ -11,32 +14,31 @@ const options = {
1114
},
1215
};
1316

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;
17+
it('should unmount Wrapper component', () => {
18+
const actual = mount(<Preview code={code} evalInContext={evalInContext} />, options);
19+
const node = actual.instance().mountNode;
2020

21+
expect(node.innerHTML).toMatch('<button');
2122
actual.unmount();
22-
23-
expect(unmountComponentAtNodeMock).toBeCalled();
23+
expect(node.innerHTML).toBe('');
2424
});
2525

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;
26+
it('should not not fail when Wrapper wasn’t mounted', () => {
27+
const consoleError = console.error;
28+
console.error = () => {};
29+
30+
const actual = mount(<Preview code="pizza" evalInContext={evalInContext} />, options);
31+
const node = actual.instance().mountNode;
3232

33+
expect(node.innerHTML).toBe('');
3334
actual.unmount();
35+
expect(node.innerHTML).toBe('');
3436

35-
expect(unmountComponentAtNodeMock).not.toBeCalled();
37+
console.error = consoleError;
3638
});
3739

3840
it('should render component renderer', () => {
39-
const actual = shallow(<Preview code={code} evalInContext={noop} />, options);
41+
const actual = shallow(<Preview code={code} evalInContext={evalInContext} />, options);
4042

4143
expect(actual).toMatchSnapshot();
4244
});

test/jestsetup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ document.createRange = function() {
4242
};
4343

4444
// requestAnimationFrame “polyfill”
45-
window.requestAnimationFrame = a => a;
45+
window.requestAnimationFrame = a => a();
4646

4747
jest.mock('react-scripts/config/webpack.config.dev', () => ({ cra: true }), { virtual: true });
4848
jest.mock('webpack-dev-server', function() {

0 commit comments

Comments
 (0)