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,68 @@ ), ); }); + +describe('webMentionTypeaheadRegex', () => { + const validMatches = [ + { + textPrefix: '', + mentionText: 'mention123', + }, + { + textPrefix: '', + mentionText: 'mention with space', + }, + { + textPrefix: 'text prefix ', + mentionText: 'mention with space', + }, + { + textPrefix: 'This is a test ', + mentionText: 'mention', + }, + { + textPrefix: '', + mentionText: '', + }, + { + textPrefix: 'A multi-line\nmention with\nnewlines ', + mentionText: 'mention', + }, + { + textPrefix: 'text @first mention ', + mentionText: 'second mention', + }, + { + textPrefix: 'text text \n@first \n', + mentionText: 'second mention', + }, + ]; + + it('should match webMentionTypeaheadRegex', () => + validMatches.forEach(validMatchObject => { + const text = `${validMatchObject.textPrefix}@${validMatchObject.mentionText}`; + const match = 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(), + )); +});