Skip to content

Commit 6fdde75

Browse files
authored
chore(compass-editor): add editor tests for multiline COMPASS-9502 (#7080)
* add inline editor tests * cr fixes
1 parent 2f8b6c1 commit 6fdde75

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

package-lock.json

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

packages/compass-editor/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@mongodb-js/eslint-config-compass": "^1.4.0",
5050
"@mongodb-js/mocha-config-compass": "^1.6.8",
5151
"@mongodb-js/prettier-config-compass": "^1.2.8",
52+
"@mongodb-js/testing-library-compass": "^1.3.3",
5253
"@mongodb-js/tsconfig-compass": "^1.2.8",
5354
"@types/chai": "^4.2.21",
5455
"@types/mocha": "^9.0.0",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
import { render, userEvent } from '@mongodb-js/testing-library-compass';
3+
import { CodemirrorInlineEditor } from './editor';
4+
import type { EditorRef } from './types';
5+
import { expect } from 'chai';
6+
7+
function renderCodemirrorInlineEditor(text: string) {
8+
const editorRef = React.createRef<EditorRef>();
9+
render(<CodemirrorInlineEditor text={text} ref={editorRef} />);
10+
return editorRef;
11+
}
12+
13+
describe('Editor', function () {
14+
context('CodemirrorInlineEditor', function () {
15+
let editorRef: React.RefObject<EditorRef>;
16+
beforeEach(function () {
17+
editorRef = renderCodemirrorInlineEditor('{}');
18+
19+
const lines = document.querySelectorAll('.cm-line');
20+
expect(lines.length).to.equal(1);
21+
expect(lines[0].textContent).to.equal('{}');
22+
editorRef.current?.focus();
23+
});
24+
25+
it('renders multi lines on {enter}', function () {
26+
userEvent.keyboard('{arrowright}');
27+
userEvent.keyboard('{enter}');
28+
29+
const lines = document.querySelectorAll('.cm-line');
30+
// On enter, the editor is split into three lines:
31+
// 1. The opening brace
32+
// 2. An empty line - to allow for new content
33+
// 3. The closing brace
34+
expect(lines.length).to.equal(3);
35+
expect(lines[0].textContent).to.equal('{');
36+
expect(lines[1].textContent?.trim()).to.equal('');
37+
expect(lines[2].textContent).to.equal('}');
38+
39+
// 2 new line characters as it it allows for new content to be added
40+
// between the opening and closing braces. and it also correctly
41+
// indents the cursor position with (2) spaces.
42+
expect(editorRef.current?.editorContents).to.equal('{\n \n}');
43+
});
44+
45+
it('renders multi lines on {shift}+{enter}', function () {
46+
userEvent.keyboard('{arrowright}');
47+
userEvent.keyboard('{shift}{enter}');
48+
49+
const lines = document.querySelectorAll('.cm-line');
50+
expect(lines.length).to.equal(3);
51+
expect(lines[0].textContent).to.equal('{');
52+
expect(lines[1].textContent?.trim()).to.equal('');
53+
expect(lines[2].textContent).to.equal('}');
54+
55+
expect(editorRef.current?.editorContents).to.equal('{\n \n}');
56+
});
57+
});
58+
});

0 commit comments

Comments
 (0)