1- import { mount } from 'enzyme ' ;
1+ import { render , screen , waitFor } from '@testing-library/react ' ;
22import React from 'react' ;
33
44import CodeEditor from '../src/codeEditor' ;
@@ -8,43 +8,48 @@ describe('<CodeEditor/>', () => {
88 Range . prototype . getBoundingClientRect = getClientRectSpy ;
99 Range . prototype . getClientRects = getClientRectSpy ;
1010
11- const node = mount ( < CodeEditor code = "console.log('Hello, world.');" lang = "js" /> ) ;
12- const cm = node . find ( 'Controlled' ) ;
13-
1411 it ( 'should display a <CodeEditor> element' , ( ) => {
15- expect ( node . children ( '.CodeEditor' ) ) . toHaveLength ( 1 ) ;
12+ render ( < CodeEditor code = "console.log('Hello, world.');" lang = "javascript" /> ) ;
13+
14+ expect ( screen . getByRole ( 'textbox' ) ) . toBeVisible ( ) ;
1615 } ) ;
1716
1817 it ( 'should highlight code' , ( ) => {
19- expect ( cm . html ( ) ) . toContain ( 'cm-variable' ) ;
18+ render ( < CodeEditor code = "console.log('Hello, world.');" lang = "javascript" /> ) ;
19+
20+ expect ( screen . getByText ( 'console' ) ) . toHaveClass ( 'cm-variable' ) ;
2021 } ) ;
2122
2223 it ( 'should set CodeMirror options' , ( ) => {
23- expect ( 'options' in cm . props ( ) ) . toBe ( true ) ;
24+ render ( < CodeEditor code = "console.log('Hello, world.');" lang = "javascript" theme = "neo" /> ) ;
25+ // eslint-disable-next-line testing-library/no-node-access
26+ const [ editor ] = document . getElementsByClassName ( 'CodeEditor' ) ;
27+
28+ // eslint-disable-next-line testing-library/no-node-access
29+ expect ( editor . children [ 0 ] ) . toHaveClass ( 'cm-s-neo' ) ;
2430 } ) ;
2531
2632 it ( 'should set a sanitized language mode' , ( ) => {
27- expect ( node . props ( ) . lang ) . toBe ( 'js' ) ;
28- expect ( cm . props ( ) . options . mode ) . toBe ( 'javascript' ) ;
33+ render ( < CodeEditor code = "console.log('Hello, world.');" lang = "js" /> ) ;
34+
35+ expect ( screen . getByText ( 'console' ) ) . toHaveClass ( 'cm-variable' ) ;
2936 } ) ;
3037
31- it ( 'should set new language via props' , ( ) => {
32- node . setProps ( { lang : 'kotlin' } ) ;
33- expect ( node . props ( ) . lang ) . toBe ( 'kotlin' ) ;
38+ it . skip ( 'should set new language via props' , ( ) => {
39+ const { rerender } = render ( < CodeEditor code = "console.log('Hello, world.');" /> ) ;
40+ expect ( screen . getByText ( "console.log('Hello, world.');" ) ) . toBeVisible ( ) ;
3441
35- setTimeout ( ( ) => {
36- expect ( cm . props ( ) . options . mode ) . toBe ( 'clike' ) ;
37- } ) ;
42+ rerender ( < CodeEditor code = "console.log('Hello, world.');" lang = "javascript" /> ) ;
43+ return waitFor ( ( ) => expect ( screen . getByText ( 'console' ) ) . resolves . toHaveClass ( 'cm-variable' ) ) ;
3844 } ) ;
3945
4046 it ( 'should take children as a code value' , ( ) => {
4147 const props = {
4248 children : 'const res = true;' ,
43- lang : 'js ' ,
49+ lang : 'javascript ' ,
4450 } ;
4551
46- const n2 = mount ( < CodeEditor { ...props } /> ) ;
47- const cm2 = n2 . find ( 'Controlled' ) ;
48- expect ( cm2 . prop ( 'value' ) ) . toBe ( 'const res = true;' ) ;
52+ render ( < CodeEditor { ...props } /> ) ;
53+ expect ( screen . getByText ( 'const' ) ) . toBeVisible ( ) ;
4954 } ) ;
5055} ) ;
0 commit comments