22import React from 'react' ;
33import { render , fireEvent , screen } from '@testing-library/react' ;
44import '@testing-library/jest-dom' ;
5- import EditorV2 from 'src/components/Editor/EditorV2' ;
65
7- // Mock ResizeObserver which is not present in JSDOM
8- class ResizeObserverMock {
9- observe ( ) { }
10- unobserve ( ) { }
11- disconnect ( ) { }
12- }
13- global . ResizeObserver = ResizeObserverMock as any ;
6+ import Editor from 'src/components/Editor/Editor' ;
147
15- describe ( 'EditorV2 Component' , ( ) => {
8+ describe ( 'Editor Component' , ( ) => {
169 const mockOnEditorChange = jest . fn ( ) ;
1710
1811 const sampleProps = {
@@ -22,66 +15,66 @@ describe('EditorV2 Component', () => {
2215 } ;
2316
2417 beforeEach ( ( ) => {
25- render ( < EditorV2 { ... sampleProps } /> ) ;
18+ jest . clearAllMocks ( ) ;
2619 } ) ;
2720
2821 it ( 'renders correctly with initial value' , ( ) => {
22+ render ( < Editor { ...sampleProps } /> ) ;
2923 const editorTextarea = screen . getByRole ( 'textbox' ) as HTMLTextAreaElement ;
3024 expect ( editorTextarea ) . toBeInTheDocument ( ) ;
3125 expect ( editorTextarea . value ) . toBe ( 'Sample Initial Value' ) ;
3226 } ) ;
3327
3428 it ( 'calls onEditorChange callback when editor value changes' , ( ) => {
29+ render ( < Editor { ...sampleProps } /> ) ;
3530 const editorTextarea = screen . getByRole ( 'textbox' ) as HTMLTextAreaElement ;
3631 fireEvent . change ( editorTextarea , { target : { value : 'Updated Value' } } ) ;
3732 expect ( mockOnEditorChange ) . toHaveBeenCalledWith ( 'Updated Value' ) ;
3833 } ) ;
3934
4035 it ( 'updates editor value when initialValue prop changes' , ( ) => {
41- const updatedProps = {
42- label : 'Updated Label' ,
43- initialValue : 'Updated Initial Value' ,
44- onEditorChange : mockOnEditorChange
45- } ;
46-
47- render ( < EditorV2 { ...updatedProps } /> ) ;
48-
49- const editorTextareas = screen . getAllByRole ( 'textbox' ) as HTMLTextAreaElement [ ] ;
50- const editorTextarea = editorTextareas [ 1 ] ;
36+ const { rerender } = render ( < Editor { ...sampleProps } /> ) ;
37+ rerender (
38+ < Editor
39+ label = 'Updated Label'
40+ initialValue = 'Updated Initial Value'
41+ onEditorChange = { mockOnEditorChange }
42+ />
43+ ) ;
5144
45+ const editorTextarea = screen . getByRole ( 'textbox' ) as HTMLTextAreaElement ;
5246 expect ( editorTextarea . value ) . toBe ( 'Updated Initial Value' ) ;
5347 } ) ;
5448
5549 test ( 'should call change text with the correct value on textarea change' , ( ) => {
56- const updatedProps = {
57- label : 'Updated Label' ,
58- initialValue : 'Updated Initial Value' ,
59- onEditorChange : mockOnEditorChange
60- } ;
61-
62- render ( < EditorV2 { ... updatedProps } /> ) ;
50+ render (
51+ < Editor
52+ label = 'Updated Label'
53+ initialValue = 'Updated Initial Value'
54+ onEditorChange = { mockOnEditorChange }
55+ />
56+ ) ;
6357
64- const editorTextareas = screen . getAllByRole ( 'textbox' ) as HTMLTextAreaElement [ ] ;
65- const editorTextarea = editorTextareas [ 1 ] ;
58+ const editorTextarea = screen . getByRole ( 'textbox' ) as HTMLTextAreaElement ;
6659 fireEvent . change ( editorTextarea , { target : { value : 'New value' } } ) ;
6760
6861 expect ( editorTextarea . value ) . toBe ( 'New value' ) ;
6962 } ) ;
7063
7164 test ( 'should call onEditorChange with an empty string if textarea value is falsy' , ( ) => {
72- const updatedProps = {
73- label : 'Updated Label' ,
74- initialValue : 'Updated Initial Value' ,
75- onEditorChange : mockOnEditorChange
76- } ;
77-
78- render ( < EditorV2 { ... updatedProps } /> ) ;
65+ render (
66+ < Editor
67+ label = 'Updated Label'
68+ initialValue = 'Updated Initial Value'
69+ onEditorChange = { mockOnEditorChange }
70+ />
71+ ) ;
7972
80- const editorTextareas = screen . getAllByRole ( 'textbox' ) as HTMLTextAreaElement [ ] ;
81- const editorTextarea = editorTextareas [ 1 ] ;
73+ const editorTextarea = screen . getByRole ( 'textbox' ) as HTMLTextAreaElement ;
8274 fireEvent . change ( editorTextarea , { target : { value : '' } } ) ;
8375
8476 expect ( editorTextarea . value ) . toBe ( '' ) ;
77+ expect ( mockOnEditorChange ) . toHaveBeenCalledWith ( '' ) ;
8578 } ) ;
8679
8780
0 commit comments