diff --git a/web/utils/typeahead-utils.test.js b/web/utils/typeahead-utils.test.js --- a/web/utils/typeahead-utils.test.js +++ b/web/utils/typeahead-utils.test.js @@ -1,6 +1,9 @@ // @flow -import { getTypeaheadOverlayScroll } from './typeahead-utils.js'; +import { + getTypeaheadOverlayScroll, + webMentionTypeaheadRegex, +} from './typeahead-utils.js'; import { typeaheadStyle } from '../chat/chat-constants.js'; describe('getTypeaheadOverlayScroll', () => { @@ -30,3 +33,75 @@ ), ); }); + +describe('webMentionTypeaheadRegex', () => { + const validMatches = [ + { + text: '@mention123', + textPrefix: '', + mentionText: 'mention123', + }, + { + text: '@mention with space', + textPrefix: '', + mentionText: 'mention with space', + }, + { + text: 'text prefix @mention with space', + textPrefix: 'text prefix ', + mentionText: 'mention with space', + }, + { + text: 'This is a test @mention', + textPrefix: 'This is a test ', + mentionText: 'mention', + }, + { + text: '@', + textPrefix: '', + mentionText: '', + }, + { + text: 'A multi-line\nmention with\nnewlines @mention', + textPrefix: 'A multi-line\nmention with\nnewlines ', + mentionText: 'mention', + }, + { + text: 'text @first mention @second mention', + textPrefix: 'text @first mention ', + mentionText: 'second mention', + }, + { + text: 'text text \n@first \n@second mention', + textPrefix: 'text text \n@first \n', + mentionText: 'second mention', + }, + ]; + + it('should match webMentionTypeaheadRegex', () => + validMatches.forEach(validMatchObject => { + const match = validMatchObject.text.match(webMentionTypeaheadRegex); + expect(match).toBeTruthy(); + expect(match?.groups?.textPrefix ?? '').toEqual( + validMatchObject.textPrefix, + ); + expect(match?.groups?.mentionText ?? '').toEqual( + validMatchObject.mentionText, + ); + })); + + const invalidMatches = [ + 'This is not a valid text with mention', + `@${'a'.repeat(200)} `, + 'text prefix\\@mention', + '@mention\nnewline \\@mention with space', + '\\@', + '\\@username', + '\\@thread with spaces', + ]; + + it('should not match webMentionTypeaheadRegex', () => + invalidMatches.forEach(text => + expect(text.match(webMentionTypeaheadRegex)).toBeFalsy(), + )); +});