-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MINOR: fix the required description field saving issue without content (
#19624) * fix the required description field saving issue without content * remove the commented code
- Loading branch information
1 parent
647bab6
commit a39ee72
Showing
4 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
...ata-ui/src/main/resources/ui/src/components/common/RichTextEditor/RichTextEditor.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2025 Collate. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { EditorContentRef } from '../../Modals/ModalWithMarkdownEditor/ModalWithMarkdownEditor.interface'; | ||
import RichTextEditor from './RichTextEditor'; | ||
|
||
jest.mock('../../BlockEditor/BlockEditor', () => { | ||
return jest.fn().mockImplementation(({ content, onChange, ref }: any) => { | ||
if (ref && ref.current) { | ||
ref.current = { editor: { getHTML: jest.fn().mockReturnValue(content) } }; // mock the editor object | ||
} | ||
|
||
return ( | ||
<textarea | ||
data-testid="editor-textarea" | ||
ref={ref as React.Ref<HTMLTextAreaElement>} | ||
value={content} | ||
onChange={(e) => { | ||
onChange(e.target.value); | ||
}} | ||
/> | ||
); | ||
}); | ||
}); | ||
|
||
const onTextChangeMock = jest.fn(); | ||
|
||
describe('RichTextEditor', () => { | ||
it('renders without crashing', () => { | ||
render(<RichTextEditor onTextChange={onTextChangeMock} />); | ||
const editorElement = screen.getByTestId('editor'); | ||
|
||
expect(editorElement).toBeInTheDocument(); | ||
}); | ||
|
||
it('Passes initialValue prop correctly to BlockEditor', () => { | ||
const initialValue = 'Initial content'; | ||
render( | ||
<RichTextEditor | ||
initialValue={initialValue} | ||
onTextChange={onTextChangeMock} | ||
/> | ||
); | ||
|
||
const textarea = screen.getByTestId('editor-textarea'); | ||
|
||
expect(textarea).toHaveValue(initialValue); | ||
}); | ||
|
||
it('should trigger onTextChange when content changes', () => { | ||
render(<RichTextEditor onTextChange={onTextChangeMock} />); | ||
|
||
fireEvent.change(screen.getByTestId('editor-textarea'), { | ||
target: { value: 'New content' }, | ||
}); | ||
|
||
expect(onTextChangeMock).toHaveBeenCalledWith('New content'); | ||
}); | ||
|
||
it('should trigger onTextChange with empty content in case of value is <p></p> tags)', () => { | ||
render(<RichTextEditor onTextChange={onTextChangeMock} />); | ||
|
||
fireEvent.change(screen.getByTestId('editor-textarea'), { | ||
target: { value: '<p></p>' }, | ||
}); | ||
|
||
expect(onTextChangeMock).toHaveBeenCalledWith(''); | ||
}); | ||
|
||
it('should return empty string when value is <p></p> format content is empty', () => { | ||
const editorRef: React.RefObject<EditorContentRef> = React.createRef(); | ||
const initialValue = '<p></p>'; | ||
|
||
render(<RichTextEditor initialValue={initialValue} ref={editorRef} />); | ||
|
||
const getEditorContent = editorRef.current?.getEditorContent(); | ||
|
||
expect(getEditorContent).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters