diff --git a/src/libs/convertToLTRForComposer/index.ts b/src/libs/convertToLTRForComposer/index.ts index 37015a755869..dd6ee50d862e 100644 --- a/src/libs/convertToLTRForComposer/index.ts +++ b/src/libs/convertToLTRForComposer/index.ts @@ -9,13 +9,11 @@ function hasRTLCharacters(text: string): boolean { // Converts a given text to ensure it starts with the LTR (Left-to-Right) marker. const convertToLTRForComposer: ConvertToLTRForComposer = (text) => { - // Ensure that the text starts with RTL characters if not we return the same text to avoid concatination with special character at the start which leads to unexpected behaviour for Emoji/Mention suggestions. + // Ensure that the text starts with RTL characters if not we return the same text to avoid concatination with special + // character at the start which leads to unexpected behaviour for Emoji/Mention suggestions. if (!hasRTLCharacters(text)) { // If text is empty string return empty string to avoid an empty draft due to special character. - if (text === '' || CONST.UNICODE.LTR.match(text)) { - return ''; - } - return text; + return text.replace(CONST.UNICODE.LTR, ''); } // Check if the text contains only spaces. If it does, we do not concatenate it with CONST.UNICODE.LTR, diff --git a/tests/unit/ConvertToLTRForComposerTest.js b/tests/unit/ConvertToLTRForComposerTest.js new file mode 100644 index 000000000000..86246a9f1302 --- /dev/null +++ b/tests/unit/ConvertToLTRForComposerTest.js @@ -0,0 +1,62 @@ +import convertToLTRForComposer from '@libs/convertToLTRForComposer'; +import CONST from '@src/CONST'; + +describe('convertToLTRForComposer', () => { + test('Input without RTL characters remains unchanged', () => { + // Test when input contains no RTL characters + const inputText = 'Hello, world!'; + const result = convertToLTRForComposer(inputText); + expect(result).toBe(inputText); + }); + + test('Input with RTL characters is prefixed with LTR marker', () => { + // Test when input contains RTL characters + const inputText = 'مثال'; + const result = convertToLTRForComposer(inputText); + expect(result).toBe(`${CONST.UNICODE.LTR}${inputText}`); + }); + + test('Input with mixed RTL and LTR characters is prefixed with LTR marker', () => { + // Test when input contains mix of RTL and LTR characters + const inputText = 'مثال test '; + const result = convertToLTRForComposer(inputText); + expect(result).toBe(`${CONST.UNICODE.LTR}${inputText}`); + }); + + test('Input with only space remains unchanged', () => { + // Test when input contains only spaces + const inputText = ' '; + const result = convertToLTRForComposer(inputText); + expect(result).toBe(inputText); + }); + + test('Input with existing LTR marker remains unchanged', () => { + // Test when input already starts with the LTR marker + const inputText = `${CONST.UNICODE.LTR}مثال`; + const result = convertToLTRForComposer(inputText); + expect(result).toBe(inputText); + }); + + test('Input starting with native emojis remains unchanged', () => { + // Test when input starts with the native emojis + const inputText = '🧶'; + const result = convertToLTRForComposer(inputText); + expect(result).toBe(inputText); + }); + + test('Input is empty', () => { + // Test when input is empty to check for draft comments + const inputText = ''; + const result = convertToLTRForComposer(inputText); + expect(result.length).toBe(0); + }); + + test('input with special characters remains unchanged', () => { + // Test when input contains special characters + const specialCharacters = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '{', '}', '[', ']', '"', ':', ';', '<', '>', '?', '`', '~']; + specialCharacters.forEach((character) => { + const result = convertToLTRForComposer(character); + expect(result).toBe(character); + }); + }); +});