Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CP Staging] input of '[' and other special characters fixed #30559

Merged
merged 10 commits into from
Oct 30, 2023
8 changes: 3 additions & 5 deletions src/libs/convertToLTRForComposer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/ConvertToLTRForComposerTest.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Loading