Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
coverage/
dist/
public/index*
__tests__/__fixtures__/**/sample.*
39 changes: 16 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,35 @@ on: [push]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node:
- 18
- 20
react: [16, 17]
react: [16, 17, 18]

name: build (${{ matrix.node }} w/ React ${{ matrix.react }}

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- uses: actions/checkout@v4

- name: Install dependencies
run: npm ci
- name: Use Node.js ${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- name: Install React v${{ matrix.react }}
run: npm i react@${{ matrix.react }} react-dom@${{ matrix.react }}
- name: Install dependencies
run: npm ci

- name: Install React 16 Enzyme adapter
if: matrix.react == '16'
run: |
npm remove @wojtekmaj/enzyme-adapter-react-17
npm install enzyme-adapter-react-16
- name: Install Compatible @testing-library/react
if: matrix.react == '16' || matrix.react == '17'
run: |
npm install @testing-library/react@12

- name: Run tests on React 16
if: matrix.react == '16'
run: REACT_VERSION=16 npm test
- name: Install React v${{ matrix.react }}
run: npm i react@${{ matrix.react }} react-dom@${{ matrix.react }}

- name: Run tests on React 17
if: matrix.react == '17'
run: npm test
- name: Run tests on React ${{ matrix.react }}
run: REACT_VERSION=${{ matrix.react }} npm test
206 changes: 95 additions & 111 deletions __tests__/__snapshots__/codeMirror.test.js.snap

Large diffs are not rendered by default.

43 changes: 24 additions & 19 deletions __tests__/codeEditor.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from 'enzyme';
import { render, screen, waitFor } from '@testing-library/react';
import React from 'react';

import CodeEditor from '../src/codeEditor';
Expand All @@ -8,43 +8,48 @@ describe('<CodeEditor/>', () => {
Range.prototype.getBoundingClientRect = getClientRectSpy;
Range.prototype.getClientRects = getClientRectSpy;

const node = mount(<CodeEditor code="console.log('Hello, world.');" lang="js" />);
const cm = node.find('Controlled');

it('should display a <CodeEditor> element', () => {
expect(node.children('.CodeEditor')).toHaveLength(1);
render(<CodeEditor code="console.log('Hello, world.');" lang="javascript" />);

expect(screen.getByRole('textbox')).toBeVisible();
});

it('should highlight code', () => {
expect(cm.html()).toContain('cm-variable');
render(<CodeEditor code="console.log('Hello, world.');" lang="javascript" />);

expect(screen.getByText('console')).toHaveClass('cm-variable');
});

it('should set CodeMirror options', () => {
expect('options' in cm.props()).toBe(true);
render(<CodeEditor code="console.log('Hello, world.');" lang="javascript" theme="neo" />);
// eslint-disable-next-line testing-library/no-node-access
const [editor] = document.getElementsByClassName('CodeEditor');

// eslint-disable-next-line testing-library/no-node-access
expect(editor.children[0]).toHaveClass('cm-s-neo');
});

it('should set a sanitized language mode', () => {
expect(node.props().lang).toBe('js');
expect(cm.props().options.mode).toBe('javascript');
render(<CodeEditor code="console.log('Hello, world.');" lang="js" />);

expect(screen.getByText('console')).toHaveClass('cm-variable');
});

it('should set new language via props', () => {
node.setProps({ lang: 'kotlin' });
expect(node.props().lang).toBe('kotlin');
it.skip('should set new language via props', () => {
const { rerender } = render(<CodeEditor code="console.log('Hello, world.');" />);
expect(screen.getByText("console.log('Hello, world.');")).toBeVisible();

setTimeout(() => {
expect(cm.props().options.mode).toBe('clike');
});
rerender(<CodeEditor code="console.log('Hello, world.');" lang="javascript" />);
return waitFor(() => expect(screen.getByText('console')).resolves.toHaveClass('cm-variable'));
});

it('should take children as a code value', () => {
const props = {
children: 'const res = true;',
lang: 'js',
lang: 'javascript',
};

const n2 = mount(<CodeEditor {...props} />);
const cm2 = n2.find('Controlled');
expect(cm2.prop('value')).toBe('const res = true;');
render(<CodeEditor {...props} />);
expect(screen.getByText('const')).toBeVisible();
});
});
Loading