22import React , { useRef } from 'react' ;
33import TestRenderer from 'react-test-renderer' ;
44import { fireEvent , render } from '@testing-library/react' ;
5+ import '@testing-library/jest-dom' ;
6+ import userEvent from '@testing-library/user-event' ;
57import TextareaCodeEditor from '../' ;
68
79it ( 'Should output a TextareaCodeEditor' , async ( ) => {
8- const component = TestRenderer . create ( < TextareaCodeEditor /> ) ;
10+ const component = TestRenderer . create ( < TextareaCodeEditor placeholder = "Please enter JS code." /> ) ;
911 let tree = component . toJSON ( ) ;
1012 if ( tree && ! Array . isArray ( tree ) ) {
1113 expect ( tree . type ) . toEqual ( 'div' ) ;
@@ -23,6 +25,7 @@ it('Should output a TextareaCodeEditor', async () => {
2325 if ( typeof child === 'object' ) {
2426 expect ( / ^ ( d i v | t e x t a r e a ) $ / . test ( child . type || '' ) ) . toBeTruthy ( ) ;
2527 if ( child . type === 'textarea' ) {
28+ expect ( child . props . placeholder ) . toEqual ( 'Please enter JS code.' ) ;
2629 expect ( child . props . autoComplete ) . toEqual ( 'off' ) ;
2730 expect ( child . props . autoCorrect ) . toEqual ( 'off' ) ;
2831 expect ( child . props . spellCheck ) . toEqual ( 'false' ) ;
@@ -120,3 +123,136 @@ it('TextareaCodeEditor onChange', async () => {
120123 fireEvent . input ( firstChild . firstChild , { target : { value : 'a' } } ) ;
121124 }
122125} ) ;
126+
127+ it ( 'TextareaCodeEditor Tab Input' , async ( ) => {
128+ const {
129+ container : { firstChild } ,
130+ } = render (
131+ < TextareaCodeEditor language = "js" data-testid = "textarea" autoFocus value = "console.log('This is a bad example')" /> ,
132+ ) ;
133+
134+ if ( firstChild && firstChild . firstChild ) {
135+ ( firstChild . firstChild as any ) . setSelectionRange ( 23 , 26 ) ;
136+ userEvent . type ( firstChild . firstChild as any , '{backspace}good' ) ;
137+
138+ expect ( firstChild . firstChild ) . toHaveFocus ( ) ;
139+ expect ( firstChild . firstChild ) . toHaveValue ( `console.log('This is a good example')` ) ;
140+ fireEvent . keyDown ( firstChild . firstChild , {
141+ key : 'Tab' ,
142+ code : 'Tab' ,
143+ charCode : 9 ,
144+ } ) ;
145+ }
146+ } ) ;
147+
148+ it ( 'TextareaCodeEditor onKeyDown Tab Input' , async ( ) => {
149+ const {
150+ container : { firstChild } ,
151+ } = render (
152+ < TextareaCodeEditor
153+ language = "js"
154+ data-testid = "textarea"
155+ autoFocus
156+ value = "console.log('This is a bad example')"
157+ onKeyDown = { ( evn ) => {
158+ expect ( evn . code . toLowerCase ( ) ) . toEqual ( 'tab' ) ;
159+ expect ( ( evn . target as any ) . value ) . toEqual ( `console.log('This is a example')` ) ;
160+ } }
161+ /> ,
162+ ) ;
163+
164+ if ( firstChild && firstChild . firstChild ) {
165+ ( firstChild . firstChild as any ) . setSelectionRange ( 23 , 26 ) ;
166+ fireEvent . keyDown ( firstChild . firstChild , {
167+ key : 'Tab' ,
168+ code : 'Tab' ,
169+ charCode : 9 ,
170+ } ) ;
171+ }
172+ } ) ;
173+
174+ it ( 'TextareaCodeEditor onKeyDown Tab One-Line Input' , async ( ) => {
175+ const {
176+ container : { firstChild } ,
177+ } = render (
178+ < TextareaCodeEditor
179+ language = "js"
180+ data-testid = "textarea"
181+ autoFocus
182+ value = { `console.log('This is a bad example')\nconsole.log('This is a good example')` }
183+ onKeyDown = { ( evn ) => {
184+ expect ( evn . code . toLowerCase ( ) ) . toEqual ( 'tab' ) ;
185+ expect ( ( evn . target as any ) . value ) . toEqual (
186+ ` console.log('This is a bad example')\n console.log('This is a good example')` ,
187+ ) ;
188+ } }
189+ /> ,
190+ ) ;
191+
192+ if ( firstChild && firstChild . firstChild ) {
193+ ( firstChild . firstChild as any ) . setSelectionRange ( 4 , 60 ) ;
194+ fireEvent . keyDown ( firstChild . firstChild , {
195+ key : 'Tab' ,
196+ code : 'Tab' ,
197+ charCode : 9 ,
198+ } ) ;
199+ }
200+ } ) ;
201+
202+ it ( 'TextareaCodeEditor onKeyDown Tab Multi-Line Input' , async ( ) => {
203+ const example = `\nfunction stopPropagation(e) {\n e.stopPropagation();\n e.preventDefault();\n}` ;
204+ const expected = `\nfunction stopPropagation(e) {\ne.stopPropagation();\ne.preventDefault();\n}` ;
205+ const {
206+ container : { firstChild } ,
207+ } = render (
208+ < TextareaCodeEditor
209+ language = "js"
210+ data-testid = "textarea"
211+ autoFocus
212+ value = { example }
213+ onKeyDown = { ( evn ) => {
214+ expect ( evn . code . toLowerCase ( ) ) . toEqual ( 'tab' ) ;
215+ expect ( ( evn . target as any ) . value ) . toEqual ( expected ) ;
216+ } }
217+ /> ,
218+ ) ;
219+
220+ if ( firstChild && firstChild . firstChild ) {
221+ ( firstChild . firstChild as any ) . setSelectionRange ( 38 , 67 ) ;
222+ fireEvent . keyDown ( firstChild . firstChild , {
223+ key : 'Tab' ,
224+ code : 'Tab' ,
225+ charCode : 9 ,
226+ shiftKey : true ,
227+ } ) ;
228+ }
229+ } ) ;
230+
231+ it ( 'TextareaCodeEditor onKeyDown Tab Multi-Line 2 Input' , async ( ) => {
232+ const example = `\nfunction stopPropagation(e) {\n e.stopPropagation();\n e.preventDefault();\n}` ;
233+ const expected = `\nfunction stopPropagation(e) {\ne.stopPropagation();\ne.preventDefault();\n}` ;
234+ const {
235+ container : { firstChild } ,
236+ } = render (
237+ < TextareaCodeEditor
238+ language = "js"
239+ data-testid = "textarea"
240+ autoFocus
241+ value = { example }
242+ onKeyDown = { ( evn ) => {
243+ expect ( evn . code . toLowerCase ( ) ) . toEqual ( 'tab' ) ;
244+ expect ( ( evn . target as any ) . value ) . toEqual ( expected ) ;
245+ } }
246+ /> ,
247+ ) ;
248+
249+ if ( firstChild && firstChild . firstChild ) {
250+ ( firstChild . firstChild as any ) . setSelectionRange ( 6 , 67 ) ;
251+ fireEvent . keyDown ( firstChild . firstChild , {
252+ key : 'Tab' ,
253+ code : 'Tab' ,
254+ charCode : 9 ,
255+ shiftKey : true ,
256+ } ) ;
257+ }
258+ } ) ;
0 commit comments