Skip to content

Commit 784a099

Browse files
committed
coderabbit suggestions
1 parent 41be479 commit 784a099

2 files changed

Lines changed: 101 additions & 5 deletions

File tree

app/containers/MessageComposer/MessageComposer.test.tsx

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { render, screen, fireEvent, waitFor, userEvent } from '@testing-library/
33
import { Provider } from 'react-redux';
44

55
import { MessageComposerContainer } from './MessageComposerContainer';
6+
import { calculateLength, getSeparator } from './components/ComposerInput';
67
import { setPermissions } from '../../actions/permissions';
78
import { addSettings } from '../../actions/settings';
89
import { selectServerRequest } from '../../actions/server';
@@ -860,3 +861,100 @@ describe('MessageComposer', () => {
860861
});
861862
});
862863
});
864+
865+
describe('calculateLength', () => {
866+
describe('non-code-block (inline markdown)', () => {
867+
test('empty text - no separator needed', () => {
868+
const result = calculateLength('', '*', false, '');
869+
expect(result).toBe(1);
870+
});
871+
872+
test('text without trailing space, no selection - separator is empty, cursor between delimiters', () => {
873+
const separator = getSeparator('hello', false, false);
874+
expect(separator).toBe('');
875+
const result = calculateLength('hello', '*', false, separator);
876+
expect(result).toBe(1);
877+
});
878+
879+
test('text with trailing space, no selection - separator is empty', () => {
880+
const separator = getSeparator('hello ', false, false);
881+
expect(separator).toBe('');
882+
const result = calculateLength('hello ', '*', false, separator);
883+
expect(result).toBe(1);
884+
});
885+
886+
test('text without trailing space, with selection - separator is space', () => {
887+
const separator = getSeparator('hello', false, true);
888+
expect(separator).toBe(' ');
889+
const result = calculateLength('hello', '*', false, separator);
890+
expect(result).toBe(2);
891+
});
892+
893+
test('text with trailing space, with selection - separator is empty', () => {
894+
const separator = getSeparator('hello ', false, true);
895+
expect(separator).toBe('');
896+
const result = calculateLength('hello ', '*', false, separator);
897+
expect(result).toBe(1);
898+
});
899+
});
900+
901+
describe('code-block', () => {
902+
test('empty text - no separator needed', () => {
903+
const result = calculateLength('', '```', true, '');
904+
expect(result).toBe(4);
905+
});
906+
907+
test('text without trailing backticks - separator is newline', () => {
908+
const separator = getSeparator('some code', true, false);
909+
expect(separator).toBe('\n');
910+
const result = calculateLength('some code', '```', true, separator);
911+
expect(result).toBe(5);
912+
});
913+
914+
test('text ending with backticks - separator is empty', () => {
915+
const separator = getSeparator('code ```', true, false);
916+
expect(separator).toBe('');
917+
const result = calculateLength('code ```', '```', true, separator);
918+
expect(result).toBe(4);
919+
});
920+
});
921+
});
922+
923+
describe('getSeparator', () => {
924+
describe('non-code-block', () => {
925+
test('empty text returns empty string', () => {
926+
expect(getSeparator('', false, false)).toBe('');
927+
expect(getSeparator('', false, true)).toBe('');
928+
});
929+
930+
test('no selection returns empty string', () => {
931+
expect(getSeparator('hello', false, false)).toBe('');
932+
});
933+
934+
test('with selection, no trailing space returns space', () => {
935+
expect(getSeparator('hello', false, true)).toBe(' ');
936+
});
937+
938+
test('with selection, trailing space returns empty string', () => {
939+
expect(getSeparator('hello ', false, true)).toBe('');
940+
});
941+
});
942+
943+
describe('code-block', () => {
944+
test('empty text returns empty string', () => {
945+
expect(getSeparator('', true, false)).toBe('');
946+
});
947+
948+
test('text not ending with backticks returns newline', () => {
949+
expect(getSeparator('some code', true, false)).toBe('\n');
950+
});
951+
952+
test('text ending with ``` returns empty string', () => {
953+
expect(getSeparator('code ```', true, false)).toBe('');
954+
});
955+
956+
test('text ending with ``` and spaces returns empty string', () => {
957+
expect(getSeparator('code ``` ', true, false)).toBe('');
958+
});
959+
});
960+
});

app/containers/MessageComposer/components/ComposerInput.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import useIOSBackSwipeHandler from '../hooks/useIOSBackSwipeHandler';
4545

4646
const defaultSelection: IInputSelection = { start: 0, end: 0 };
4747

48-
function calculateLength(startingText: string, markdown: string, isCodeBlock: boolean, separator: string) {
48+
export function calculateLength(startingText: string, markdown: string, isCodeBlock: boolean, separator: string) {
4949
if (isCodeBlock) {
5050
if (startingText.length > 0) {
5151
return markdown.length + separator.length + 1;
@@ -54,12 +54,10 @@ function calculateLength(startingText: string, markdown: string, isCodeBlock: bo
5454
return markdown.length + 1;
5555
}
5656

57-
const endWithSpace = startingText.endsWith(' ');
58-
59-
return markdown.length + (startingText.length > 0 ? 1 : 0) + (endWithSpace ? -1 : 0);
57+
return separator.length + markdown.length;
6058
}
6159

62-
function getSeparator(startingText: string, isCodeBlock: boolean, hasSelection: boolean) {
60+
export function getSeparator(startingText: string, isCodeBlock: boolean, hasSelection: boolean) {
6361
if (startingText.length === 0) {
6462
return '';
6563
}

0 commit comments

Comments
 (0)