|
| 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