Skip to content

Commit

Permalink
disable save button on empty characters of string, maximum length errors
Browse files Browse the repository at this point in the history
  • Loading branch information
js-jankisalvi committed Jul 7, 2023
1 parent f91c242 commit a12a48a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,26 @@ describe('AddComment ', () => {

const markdown = screen.getByTestId('euiMarkdownEditorTextArea');

userEvent.type(markdown, 'test');
userEvent.clear(markdown);
userEvent.type(markdown, ' ');

await waitFor(() => {
expect(screen.getByText('Empty comments are not allowed.')).toBeInTheDocument();
expect(screen.getByTestId('submit-comment')).toHaveAttribute('disabled');
});
});

it('shows an error when comment is of empty characters', async () => {
appMockRender.render(<AddComment {...addCommentProps} />);

const markdown = screen.getByTestId('euiMarkdownEditorTextArea');

userEvent.clear(markdown);
userEvent.type(markdown, ' ');

await waitFor(() => {
expect(screen.getByText('Empty comments are not allowed.')).toBeInTheDocument();
expect(screen.getByTestId('submit-comment')).toHaveAttribute('disabled');
});
});

Expand All @@ -224,6 +239,7 @@ describe('AddComment ', () => {
'The length of the comment is too long. The maximum length is 30000 characters.'
)
).toBeInTheDocument();
expect(screen.getByTestId('submit-comment')).toHaveAttribute('disabled');
});
});
});
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/cases/public/components/add_comment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type { AddCommentFormSchema } from './schema';
import { schema } from './schema';
import { InsertTimeline } from '../insert_timeline';
import { useCasesContext } from '../cases_context/use_cases_context';
import { MAX_COMMENT_LENGTH } from '../../../common/constants';

const MySpinner = styled(EuiLoadingSpinner)`
position: absolute;
Expand Down Expand Up @@ -174,6 +175,9 @@ export const AddComment = React.memo(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [focusOnContext]);

const isDisabled =
isLoading || !comment?.trim().length || comment.trim().length > MAX_COMMENT_LENGTH;

return (
<span id="add-comment-permLink">
{isLoading && showLoading && <MySpinner data-test-subj="loading-spinner" size="xl" />}
Expand All @@ -200,7 +204,7 @@ export const AddComment = React.memo(
data-test-subj="submit-comment"
fill
iconType="plusInCircle"
isDisabled={!comment || isLoading}
isDisabled={isDisabled}
isLoading={isLoading}
onClick={onSubmit}
>
Expand Down
18 changes: 18 additions & 0 deletions x-pack/plugins/cases/public/components/description/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ describe('Description', () => {

await waitFor(() => {
expect(screen.getByText('A description is required.')).toBeInTheDocument();
expect(screen.getByTestId('editable-save-markdown')).toHaveAttribute('disabled');
});
});

it('shows an error when description is a sting of empty characters', async () => {
const res = appMockRender.render(
<Description {...defaultProps} onUpdateField={onUpdateField} />
);

userEvent.click(res.getByTestId('description-edit-icon'));

userEvent.clear(screen.getByTestId('euiMarkdownEditorTextArea'));
userEvent.type(screen.getByTestId('euiMarkdownEditorTextArea'), ' ');

await waitFor(() => {
expect(screen.getByText('A description is required.')).toBeInTheDocument();
expect(screen.getByTestId('editable-save-markdown')).toHaveAttribute('disabled');
});
});

Expand All @@ -145,6 +162,7 @@ describe('Description', () => {
'The length of the description is too long. The maximum length is 30000 characters.'
)
).toBeInTheDocument();
expect(screen.getByTestId('editable-save-markdown')).toHaveAttribute('disabled');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const EditableMarkdownFooterComponent: React.FC<EditableMarkdownFooterProps> = (
}) => {
const [{ content }] = useFormData<{ content: string }>({ watch: ['content'] });

const isDisabled = !content?.trim().length || content.trim().length > MAX_COMMENT_LENGTH;

return (
<EuiFlexGroup gutterSize="s" justifyContent="flexEnd" responsive={false}>
<EuiFlexItem grow={false}>
Expand All @@ -43,7 +45,7 @@ const EditableMarkdownFooterComponent: React.FC<EditableMarkdownFooterProps> = (
fill
iconType="save"
onClick={handleSaveAction}
disabled={!content || content.length > MAX_COMMENT_LENGTH}
disabled={isDisabled}
size="s"
>
{i18n.SAVE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,24 @@ describe('EditableMarkdown', () => {

userEvent.clear(screen.getByTestId('euiMarkdownEditorTextArea'));

userEvent.type(screen.getByTestId('euiMarkdownEditorTextArea'), ' ');
userEvent.type(screen.getByTestId('euiMarkdownEditorTextArea'), '');

await waitFor(() => {
expect(screen.getByText('Empty comments are not allowed.')).toBeInTheDocument();
expect(screen.getByTestId('editable-save-markdown')).toHaveProperty('disabled');
});
});

it('Shows error message and save button disabled if current text is of empty characters', async () => {
render(
<MockHookWrapperComponent>
<EditableMarkdown {...defaultProps} />
</MockHookWrapperComponent>
);

userEvent.clear(screen.getByTestId('euiMarkdownEditorTextArea'));

userEvent.type(screen.getByTestId('euiMarkdownEditorTextArea'), ' ');

await waitFor(() => {
expect(screen.getByText('Empty comments are not allowed.')).toBeInTheDocument();
Expand Down

0 comments on commit a12a48a

Please sign in to comment.