Skip to content

Commit fea4170

Browse files
scnirosapegin
authored andcommitted
Fix: Migrate to react-codemirror2 (styleguidist#574)
Closes styleguidist#573
1 parent 9b531e3 commit fea4170

File tree

6 files changed

+75
-31
lines changed

6 files changed

+75
-31
lines changed

package-lock.json

Lines changed: 6 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"minimist": "^1.2.0",
6969
"pretty-format": "^20.0.3",
7070
"prop-types": "^15.5.10",
71-
"react-codemirror": "^1.0.0",
71+
"react-codemirror2": "0.0.13",
7272
"react-dev-utils": "^3.0.2",
7373
"react-docgen": "^3.0.0-beta5",
7474
"react-docgen-displayname-handler": "^1.0.0",

src/rsg-components/Editor/Editor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import debounce from 'lodash/debounce';
4-
import Codemirror from 'react-codemirror';
4+
import CodeMirror from 'react-codemirror2';
55
import 'codemirror/mode/jsx/jsx';
66

77
// We’re explicitly specifying Webpack loaders here so we could skip specifying them in Webpack configuration.
@@ -40,7 +40,7 @@ export default class Editor extends Component {
4040
return false;
4141
}
4242

43-
handleChange(newCode) {
43+
handleChange(editor, metadata, newCode) {
4444
this.props.onChange(newCode);
4545
}
4646

@@ -51,6 +51,6 @@ export default class Editor extends Component {
5151
...codemirrorOptions,
5252
theme: highlightTheme,
5353
};
54-
return <Codemirror value={code} onChange={this.handleChange} options={options} />;
54+
return <CodeMirror value={code} onValueChange={this.handleChange} options={options} />;
5555
}
5656
}
Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,49 @@
11
import React from 'react';
2+
import PropTypes from 'prop-types';
23
import { EditorLoaderRenderer } from './EditorLoaderRenderer';
4+
import Editor from './Editor';
35

4-
// We do not test Editor component because it requires Codemirror that do not work in Node environment.
6+
const code = '<button>MyAwesomeCode</button>';
7+
const newCode = '<button>MyNewAwesomeCode</button>';
8+
const options = {
9+
context: {
10+
config: {
11+
showCode: false,
12+
highlightTheme: 'base16-light',
13+
},
14+
},
15+
childContextTypes: {
16+
config: PropTypes.object.isRequired,
17+
},
18+
};
519

6-
it('renderer should render loader', () => {
7-
const actual = shallow(<EditorLoaderRenderer classes={{}} />);
20+
describe('EditorLoaderRenderer', () => {
21+
it('should renderer should render loader', () => {
22+
const actual = shallow(<EditorLoaderRenderer classes={{}} />);
823

9-
expect(actual).toMatchSnapshot();
24+
expect(actual).toMatchSnapshot();
25+
});
26+
});
27+
28+
describe('Editor', () => {
29+
it('should renderer and editor', () => {
30+
const actual = shallow(<Editor code={code} onChange={() => {}} />, options);
31+
32+
expect(actual).toMatchSnapshot();
33+
});
34+
35+
it('should update code with debounce', done => {
36+
const onChange = jest.fn();
37+
const actual = mount(<Editor code={code} onChange={onChange} />, options);
38+
39+
expect(actual.text()).toMatch(code);
40+
41+
// Set new value by calling a method on the CodeMirror instance
42+
actual.find('div').node.querySelector('.CodeMirror').CodeMirror.setValue(newCode);
43+
44+
setTimeout(() => {
45+
expect(onChange).toBeCalledWith(newCode);
46+
done();
47+
}, 13);
48+
});
1049
});

src/rsg-components/Editor/__snapshots__/Editor.spec.js.snap

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`renderer should render loader 1`] = `
3+
exports[`Editor should renderer and editor 1`] = `
4+
<CodeMirror
5+
onValueChange={[Function]}
6+
options={
7+
Object {
8+
"lineNumbers": false,
9+
"lineWrapping": true,
10+
"matchBrackets": true,
11+
"mode": "jsx",
12+
"smartIndent": false,
13+
"theme": "base16-light",
14+
"viewportMargin": Infinity,
15+
}
16+
}
17+
value="<button>MyAwesomeCode</button>"
18+
/>
19+
`;
20+
21+
exports[`EditorLoaderRenderer should renderer should render loader 1`] = `
422
<div>
523
Loading…
624
</div>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { PlaygroundRenderer } from './PlaygroundRenderer';
77

88
const evalInContext = a =>
99
new Function('require', 'const React = require("react");' + a).bind(null, require); // eslint-disable-line no-new-func
10+
const reactCodeMirrorSelector = '.react-codemirror2';
1011
const code = '<button>OK</button>';
1112
const newCode = '<button>Not OK</button>';
1213
const props = {
@@ -72,12 +73,12 @@ it('should update code with debounce', done => {
7273
it('should open a code editor', done => {
7374
const actual = mount(<Playground {...props} />, options);
7475

75-
expect(actual.find('.ReactCodeMirror')).toHaveLength(0);
76+
expect(actual.find(reactCodeMirrorSelector)).toHaveLength(0);
7677

7778
actual.find(`button[name="${EXAMPLE_TAB_CODE_EDITOR}"]`).simulate('click');
7879

7980
setTimeout(() => {
80-
expect(actual.find('.ReactCodeMirror')).toHaveLength(1);
81+
expect(actual.find(reactCodeMirrorSelector)).toHaveLength(1);
8182
done();
8283
}, 1);
8384
});

0 commit comments

Comments
 (0)