From 0416a662c242c982fe0a861e36a8fa082c68293a Mon Sep 17 00:00:00 2001 From: Neil Marcellini Date: Mon, 30 Oct 2023 11:08:05 -0700 Subject: [PATCH] Merge pull request #30559 from HardikChoudhary24/fix/input-error input of '[' and other special characters fixed (cherry picked from commit d651cabec6cbec943c960bd61d25fb7e5169260e) --- src/libs/convertToLTRForComposer/index.ts | 8 ++- tests/unit/ConvertToLTRForComposerTest.js | 62 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 tests/unit/ConvertToLTRForComposerTest.js 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); + }); + }); +});