From 1d04e9379f5189522657bf2c6d2bef185ce49583 Mon Sep 17 00:00:00 2001 From: Danish Ahmed Mirza <77742477+try-catch-stack@users.noreply.github.com> Date: Wed, 16 Mar 2022 01:03:43 +0530 Subject: [PATCH] [FIX] Mention from suggestions concatenates to query string on autocomplete (#3696) * [FIX] Mention suggestion concatenate to query string * Add function to get regexp and its tests in separate files * Update getRegexp.ts * Update file names * Try new regex * One regex for all mention types Co-authored-by: Gleidson Daniel Silva --- .../MessageBox/getMentionRegexp.test.js | 56 +++++++++++++++++++ app/containers/MessageBox/getMentionRegexp.ts | 4 ++ app/containers/MessageBox/index.tsx | 3 +- 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 app/containers/MessageBox/getMentionRegexp.test.js create mode 100644 app/containers/MessageBox/getMentionRegexp.ts diff --git a/app/containers/MessageBox/getMentionRegexp.test.js b/app/containers/MessageBox/getMentionRegexp.test.js new file mode 100644 index 000000000..8923ae100 --- /dev/null +++ b/app/containers/MessageBox/getMentionRegexp.test.js @@ -0,0 +1,56 @@ +import getMentionRegexp from './getMentionRegexp'; + +const regexp = getMentionRegexp(); + +describe('getMentionRegexpUser', function () { + test('removing query text on user suggestion autocomplete (latin)', () => { + const message = 'Hey @test123'; + expect(message.replace(regexp, '')).toBe('Hey @'); + }); + + test('removing query text on user suggestion autocomplete (arabic)', () => { + const message = 'Hey @اختبار123'; + expect(message.replace(regexp, '')).toBe('Hey @'); + }); + + test('removing query text on user suggestion autocomplete (russian)', () => { + const message = 'Hey @тест123'; + expect(message.replace(regexp, '')).toBe('Hey @'); + }); + + test('removing query text on user suggestion autocomplete (chinese trad)', () => { + const message = 'Hey @測試123'; + expect(message.replace(regexp, '')).toBe('Hey @'); + }); + + test('removing query text on user suggestion autocomplete (japanese)', () => { + const message = 'Hey @テスト123'; + expect(message.replace(regexp, '')).toBe('Hey @'); + }); + + test('removing query text on user suggestion autocomplete (special characters in query)', () => { + const message = "Hey @'=test123"; + expect(message.replace(regexp, '')).toBe('Hey @'); + }); +}); + +describe('getMentionRegexpEmoji', function () { + test('removing query text on emoji suggestion autocomplete ', () => { + const message = 'Hey :smiley'; + expect(message.replace(regexp, '')).toBe('Hey :'); + }); +}); + +describe('getMentionRegexpCommand', function () { + test('removing query text on emoji suggestion autocomplete ', () => { + const message = '/archive'; + expect(message.replace(regexp, '')).toBe('/'); + }); +}); + +describe('getMentionRegexpRoom', function () { + test('removing query text on emoji suggestion autocomplete ', () => { + const message = 'Check #general'; + expect(message.replace(regexp, '')).toBe('Check #'); + }); +}); diff --git a/app/containers/MessageBox/getMentionRegexp.ts b/app/containers/MessageBox/getMentionRegexp.ts new file mode 100644 index 000000000..ec8ea5d14 --- /dev/null +++ b/app/containers/MessageBox/getMentionRegexp.ts @@ -0,0 +1,4 @@ +// Match query string from the message to replace it with the suggestion +const getMentionRegexp = (): any => /[^@:#/!]*$/; + +export default getMentionRegexp; diff --git a/app/containers/MessageBox/index.tsx b/app/containers/MessageBox/index.tsx index 073d04b74..1b94e80d9 100644 --- a/app/containers/MessageBox/index.tsx +++ b/app/containers/MessageBox/index.tsx @@ -31,6 +31,7 @@ import { isAndroid, isTablet } from '../../utils/deviceInfo'; import { canUploadFile } from '../../utils/media'; import EventEmiter from '../../utils/events'; import { KEY_COMMAND, handleCommandShowUpload, handleCommandSubmit, handleCommandTyping } from '../../commands'; +import getMentionRegexp from './getMentionRegexp'; import Mentions from './Mentions'; import MessageboxContext from './Context'; import { @@ -489,7 +490,7 @@ class MessageBox extends Component { const msg = this.text; const { start, end } = this.selection; const cursor = Math.max(start, end); - const regexp = /([a-z0-9._-]+)$/im; + const regexp = getMentionRegexp(); let result = msg.substr(0, cursor).replace(regexp, ''); // Remove the ! after select the canned response if (trackingType === MENTIONS_TRACKING_TYPE_CANNED) {