diff --git a/lib/shared/markdown.test.js b/lib/shared/markdown.test.js --- a/lib/shared/markdown.test.js +++ b/lib/shared/markdown.test.js @@ -1,6 +1,7 @@ // @flow import { spoilerRegex } from './markdown.js'; +import { chatMentionRegex } from './mention-utils.js'; // Each of the below tests is subject to the following RegEx pattern: // Spoiler RegEx: /^\|\|([^\n]+?)\|\|/g @@ -86,3 +87,58 @@ expect(extracted ? extracted[0] : null).toMatch(' \\|\\| '); }); }); + +// Each of the below tests is subject to the following RegEx pattern: +// Chat mention RegEx: +// (? { + it('We do not expect an empty raw chat mention to match.', () => { + expect('@[[:]]').not.toMatch(chatMentionRegex); + expect('@[[]]').not.toMatch(chatMentionRegex); + }); + + it('We do not expect a raw chat mention with invalid id to match.', () => { + expect('@[[|:]]').not.toMatch(chatMentionRegex); + expect('@[[256|:hello]]').not.toMatch(chatMentionRegex); + expect('@[[foo:]]').not.toMatch(chatMentionRegex); + expect('@[[:bar]]').not.toMatch(chatMentionRegex); + expect('@[[|:bar]]').not.toMatch(chatMentionRegex); + expect('@[[foo:bar]]').not.toMatch(chatMentionRegex); + }); + + it('We do not expect to match raw chat mention with valid id and without text.', () => { + expect('@[[1:]]').not.toMatch(chatMentionRegex); + expect('@[[256|1:]]').not.toMatch(chatMentionRegex); + }); + + it('We do expect to match raw chat mention with valid id and text.', () => { + expect('@[[1:test]]').toMatch(chatMentionRegex); + expect('@[[256|1:test]]').toMatch(chatMentionRegex); + }); + + it('We do expect to properly match closed raw chat mention.', () => { + expect('@[[256|1:bar \\] \\]]]').toMatch(chatMentionRegex); + }); + + it('We do not expect to improperly match closed raw chat mention.', () => { + expect('@[[256|1:bar \\]]').not.toMatch(chatMentionRegex); + expect('@[[256|1:bar \\] \\]]').not.toMatch(chatMentionRegex); + expect('@[[256|1:bar] ]').not.toMatch(chatMentionRegex); + }); + + it('We do not expect to match raw chat mention with escaped @ char.', () => { + expect('\\@[[1:bar]]').not.toMatch(chatMentionRegex); + }); + + it('We do expect to match raw chat mention with escaped chars in default text field.', () => { + expect('@[[256|1:\\@\\[\\[1:test\\]\\] ]]').toMatch(chatMentionRegex); + }); + + it('We do not expect an invalid raw chat mention format to match.', () => { + expect('@[256|1:test]').not.toMatch(chatMentionRegex); + expect('@test').not.toMatch(chatMentionRegex); + expect('@[256|1:test]]').not.toMatch(chatMentionRegex); + expect('@[[256|1:test]').not.toMatch(chatMentionRegex); + }); +});