Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions apps/web/src/common/components/CommentInputNewline.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import LabCommentInput from '@modules/lab/model/Analyze/CommentDrawer/CommentInput';
import SandboxCommentInput from '@modules/oh/DataView/HatchingTreatment/Sandbox/EvaluationInfo/MetricDrawer/CommentInput';
import HatchCommentInput from '@modules/oh/DataView/HatchingTreatment/Hatch/EvaluationInfo/MetricDrawer/CommentInput';
import GraduateCommentInput from '@modules/oh/DataView/HatchingTreatment/Graduate/EvaluationInfo/MetricDrawer/CommentInput';

jest.mock('next-i18next', () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));

jest.mock('@common/hooks/useImagePreview', () => ({
__esModule: true,
default: () => ({ ref: jest.fn(), open: jest.fn(), close: jest.fn() }),
}));

describe.each([
{ name: 'Lab', Component: LabCommentInput },
{ name: 'Sandbox', Component: SandboxCommentInput },
{ name: 'Hatch', Component: HatchCommentInput },
{ name: 'Graduate', Component: GraduateCommentInput },
])('$name comment newlines', ({ Component }) => {
describe.each([
{ name: 'Shift+Enter', modifiers: { shiftKey: true } },
{ name: 'Ctrl+Enter', modifiers: { ctrlKey: true } },
])('$name', ({ modifiers }) => {
it.each([
{ position: 'start', start: 0, end: 0, expected: '\nabcdef' },
{ position: 'middle', start: 3, end: 3, expected: 'abc\ndef' },
{ position: 'selection', start: 2, end: 4, expected: 'ab\nef' },
{ position: 'end', start: 6, end: 6, expected: 'abcdef\n' },
])('inserts a newline at the $position', ({ start, end, expected }) => {
const onSubmit = jest.fn();
render(<Component loading={false} onSubmit={onSubmit} />);
const input = screen.getByRole('textbox') as HTMLTextAreaElement;
fireEvent.change(input, { target: { value: 'abcdef' } });
input.focus();
input.setSelectionRange(start, end);

const accepted = fireEvent.keyDown(input, {
key: 'Enter',
code: 'Enter',
...modifiers,
});

expect(input.value).toBe(expected);
expect(input.selectionStart).toBe(start + 1);
expect(input.selectionEnd).toBe(start + 1);
expect(onSubmit).not.toHaveBeenCalled();
expect(accepted).toBe(false);
});
});

it('continues submitting plain Enter', () => {
const onSubmit = jest.fn();
render(<Component loading={false} onSubmit={onSubmit} />);
const input = screen.getByRole('textbox');
fireEvent.change(input, { target: { value: 'abcdef' } });

fireEvent.keyDown(input, { key: 'Enter', code: 'Enter' });

expect(onSubmit).toHaveBeenCalledTimes(1);
expect(onSubmit).toHaveBeenCalledWith('abcdef', []);
});

it('does not insert a newline or submit while loading', () => {
const onSubmit = jest.fn();
render(<Component loading onSubmit={onSubmit} />);
const input = screen.getByRole('textbox') as HTMLTextAreaElement;
fireEvent.change(input, { target: { value: 'abcdef' } });
input.setSelectionRange(2, 2);

fireEvent.keyDown(input, { key: 'Enter', shiftKey: true });
fireEvent.keyDown(input, { key: 'Enter', ctrlKey: true });

expect(input.value).toBe('abcdef');
expect(onSubmit).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {
forwardRef,
useImperativeHandle,
} from 'react';
import { flushSync } from 'react-dom';
import { FiImage } from 'react-icons/fi';
import { BiLoaderAlt } from 'react-icons/bi';
import { toast } from 'react-hot-toast';
Expand Down Expand Up @@ -116,7 +117,19 @@ const CommentInput = forwardRef<InputRefProps, Props>(
if (loading) return;
if (event.key == 'Enter' || event.code == 'Enter') {
if (event.shiftKey || event.ctrlKey) {
setValue((p) => p + '\n');
const input = event.currentTarget;
const { selectionStart, selectionEnd, value } = input;
flushSync(() => {
setValue(
value.slice(0, selectionStart) +
'\n' +
value.slice(selectionEnd)
);
});
input.setSelectionRange(
selectionStart + 1,
selectionStart + 1
);
} else {
if (value) {
onSubmit(value, images);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
forwardRef,
useImperativeHandle,
} from 'react';
import { flushSync } from 'react-dom';
import { BiLoaderAlt } from 'react-icons/bi';
import { toast } from 'react-hot-toast';
import { useTranslation } from 'next-i18next';
Expand Down Expand Up @@ -124,7 +125,19 @@ const CommentInput = forwardRef<InputRefProps, Props>(
if (loading) return;
if (event.key == 'Enter' || event.code == 'Enter') {
if (event.shiftKey || event.ctrlKey) {
setValue((p) => p + '\n');
const input = event.currentTarget;
const { selectionStart, selectionEnd, value } = input;
flushSync(() => {
setValue(
value.slice(0, selectionStart) +
'\n' +
value.slice(selectionEnd)
);
});
input.setSelectionRange(
selectionStart + 1,
selectionStart + 1
);
} else {
if (value) {
onSubmit(value, images);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {
forwardRef,
useImperativeHandle,
} from 'react';
import { flushSync } from 'react-dom';
import { FiImage } from 'react-icons/fi';
import { BiLoaderAlt } from 'react-icons/bi';
import { toast } from 'react-hot-toast';
Expand Down Expand Up @@ -128,7 +129,19 @@ const CommentInput = forwardRef<InputRefProps, Props>(
if (loading) return;
if (event.key == 'Enter' || event.code == 'Enter') {
if (event.shiftKey || event.ctrlKey) {
setValue((p) => p + '\n');
const input = event.currentTarget;
const { selectionStart, selectionEnd, value } = input;
flushSync(() => {
setValue(
value.slice(0, selectionStart) +
'\n' +
value.slice(selectionEnd)
);
});
input.setSelectionRange(
selectionStart + 1,
selectionStart + 1
);
} else {
if (value) {
onSubmit(value, images);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
forwardRef,
useImperativeHandle,
} from 'react';
import { flushSync } from 'react-dom';
import { BiLoaderAlt } from 'react-icons/bi';
import { toast } from 'react-hot-toast';
import { useTranslation } from 'next-i18next';
Expand Down Expand Up @@ -86,7 +87,19 @@ const CommentInput = forwardRef<InputRefProps, Props>(
if (loading) return;
if (event.key == 'Enter' || event.code == 'Enter') {
if (event.shiftKey || event.ctrlKey) {
setValue((p) => p + '\n');
const input = event.currentTarget;
const { selectionStart, selectionEnd, value } = input;
flushSync(() => {
setValue(
value.slice(0, selectionStart) +
'\n' +
value.slice(selectionEnd)
);
});
input.setSelectionRange(
selectionStart + 1,
selectionStart + 1
);
} else {
if (value) {
onSubmit(value, images);
Expand Down