import { expect } from 'chai'; import { MentionsParser } from '../../../../app/mentions/lib/MentionsParser'; let mentionsParser; beforeEach(() => { mentionsParser = new MentionsParser({ pattern: () => '[0-9a-zA-Z-_.]+', me: () => 'me', }); }); describe('Mention', () => { describe('getUserMentions', () => { describe('for simple text, no mentions', () => { const result = []; ['#rocket.cat', 'hello rocket.cat how are you?'].forEach((text) => { it(`should return "${JSON.stringify(result)}" from "${text}"`, () => { expect(result).to.be.deep.equal(mentionsParser.getUserMentions(text)); }); }); }); describe('for one user', () => { const result = ['@rocket.cat']; [ '@rocket.cat', ' @rocket.cat ', 'hello @rocket.cat', // 'hello,@rocket.cat', // this test case is ignored since is not compatible with the message box behavior '@rocket.cat, hello', '@rocket.cat,hello', 'hello @rocket.cat how are you?', ].forEach((text) => { it(`should return "${JSON.stringify(result)}" from "${text}"`, () => { expect(result).to.be.deep.equal(mentionsParser.getUserMentions(text)); }); }); it.skip('should return without the "." from "@rocket.cat."', () => { expect(result).to.be.deep.equal(mentionsParser.getUserMentions('@rocket.cat.')); }); it.skip('should return without the "_" from "@rocket.cat_"', () => { expect(result).to.be.deep.equal(mentionsParser.getUserMentions('@rocket.cat_')); }); it.skip('should return without the "-" from "@rocket.cat-"', () => { expect(result).to.be.deep.equal(mentionsParser.getUserMentions('@rocket.cat-')); }); }); describe('for two users', () => { const result = ['@rocket.cat', '@all']; [ '@rocket.cat @all', ' @rocket.cat @all ', 'hello @rocket.cat and @all', '@rocket.cat, hello @all', 'hello @rocket.cat and @all how are you?', ].forEach((text) => { it(`should return "${JSON.stringify(result)}" from "${text}"`, () => { expect(result).to.be.deep.equal(mentionsParser.getUserMentions(text)); }); }); }); }); describe('getChannelMentions', () => { describe('for simple text, no mentions', () => { const result = []; ['@rocket.cat', 'hello rocket.cat how are you?'].forEach((text) => { it(`should return "${JSON.stringify(result)}" from "${text}"`, () => { expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text)); }); }); }); describe('for one channel', () => { const result = ['#general']; ['#general', ' #general ', 'hello #general', '#general, hello', 'hello #general, how are you?'].forEach((text) => { it(`should return "${JSON.stringify(result)}" from "${text}"`, () => { expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text)); }); }); it.skip('should return without the "." from "#general."', () => { expect(result).to.be.deep.equal(mentionsParser.getUserMentions('#general.')); }); it.skip('should return without the "_" from "#general_"', () => { expect(result).to.be.deep.equal(mentionsParser.getUserMentions('#general_')); }); it.skip('should return without the "-" from "#general."', () => { expect(result).to.be.deep.equal(mentionsParser.getUserMentions('#general-')); }); }); describe('for two channels', () => { const result = ['#general', '#other']; [ '#general #other', ' #general #other', 'hello #general and #other', '#general, hello #other', 'hello #general #other, how are you?', ].forEach((text) => { it(`should return "${JSON.stringify(result)}" from "${text}"`, () => { expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text)); }); }); }); describe('for url with fragments', () => { const result = []; ['http://localhost/#general'].forEach((text) => { it(`should return nothing from "${text}"`, () => { expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text)); }); }); }); describe('for messages with url and channels', () => { const result = ['#general']; ['http://localhost/#general #general'].forEach((text) => { it(`should return "${JSON.stringify(result)}" from "${text}"`, () => { expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text)); }); }); }); }); }); const message = { mentions: [ { username: 'rocket.cat', name: 'Rocket.Cat' }, { username: 'admin', name: 'Admin' }, { username: 'me', name: 'Me' }, { username: 'specialchars', name: '' }, ], channels: [ { name: 'general', _id: '42' }, { name: 'rocket.cat', _id: '169' }, ], }; describe('replace methods', () => { describe('replaceUsers', () => { it('should render for @all', () => { const result = mentionsParser.replaceUsers('@all', message, 'me'); expect(result).to.be.equal('all'); }); const str2 = 'rocket.cat'; it(`should render for "@${str2}"`, () => { const result = mentionsParser.replaceUsers(`@${str2}`, message, 'me'); expect(result).to.be.equal(`${str2}`); }); it(`should render for "hello ${str2}"`, () => { const result = mentionsParser.replaceUsers(`hello @${str2}`, message, 'me'); expect(result).to.be.equal(`hello ${str2}`); }); it('should render for unknow/private user "hello @unknow"', () => { const result = mentionsParser.replaceUsers('hello @unknow', message, 'me'); expect(result).to.be.equal('hello @unknow'); }); it('should render for me', () => { const result = mentionsParser.replaceUsers('hello @me', message, 'me'); expect(result).to.be.equal('hello me'); }); }); describe('replaceUsers (RealNames)', () => { beforeEach(() => { mentionsParser.useRealName = () => true; }); it('should render for @all', () => { const result = mentionsParser.replaceUsers('@all', message, 'me'); expect(result).to.be.equal('all'); }); const str2 = 'rocket.cat'; const str2Name = 'Rocket.Cat'; it(`should render for "@${str2}"`, () => { const result = mentionsParser.replaceUsers(`@${str2}`, message, 'me'); expect(result).to.be.equal(`${str2Name}`); }); it(`should render for "hello @${str2}"`, () => { const result = mentionsParser.replaceUsers(`hello @${str2}`, message, 'me'); expect(result).to.be.equal( `hello ${str2Name}`, ); }); const specialchars = 'specialchars'; const specialcharsName = '<img onerror=alert(hello)>'; it(`should escape special characters in "hello @${specialchars}"`, () => { const result = mentionsParser.replaceUsers(`hello @${specialchars}`, message, 'me'); expect(result).to.be.equal( `hello ${specialcharsName}`, ); }); it(`should render for "hello
@${str2}
"`, () => { const result = mentionsParser.replaceUsers(`hello
@${str2}
`, message, 'me'); expect(result).to.be.equal( `hello
${str2Name}
`, ); }); it('should render for unknow/private user "hello @unknow"', () => { const result = mentionsParser.replaceUsers('hello @unknow', message, 'me'); expect(result).to.be.equal('hello @unknow'); }); it('should render for me', () => { const result = mentionsParser.replaceUsers('hello @me', message, 'me'); expect(result).to.be.equal('hello Me'); }); }); describe('replaceChannels', () => { it('should render for #general', () => { const result = mentionsParser.replaceChannels('#general', message); expect('<).to.be.equal(class="mention-link mention-link--room" data-channel="42">#general', result); }); const str2 = '#rocket.cat'; it(`should render for ${str2}`, () => { const result = mentionsParser.replaceChannels(str2, message); expect(result).to.be.equal(`${str2}`); }); it(`should render for "hello ${str2}"`, () => { const result = mentionsParser.replaceChannels(`hello ${str2}`, message); expect(result).to.be.equal(`hello ${str2}`); }); it('should render for unknow/private channel "hello #unknow"', () => { const result = mentionsParser.replaceChannels('hello #unknow', message); expect(result).to.be.equal('hello #unknow'); }); }); describe('parse all', () => { it('should render for #general', () => { message.html = '#general'; const result = mentionsParser.parse(message, 'me'); expect(result.html).to.be.equal('#general'); }); it('should render for "#general and @rocket.cat', () => { message.html = '#general and @rocket.cat'; const result = mentionsParser.parse(message, 'me'); expect(result.html).to.be.equal( '#general and rocket.cat', ); }); it('should render for "', () => { message.html = ''; const result = mentionsParser.parse(message, 'me'); expect(result.html).to.be.equal(''); }); it('should render for "simple text', () => { message.html = 'simple text'; const result = mentionsParser.parse(message, 'me'); expect(result.html).to.be.equal('simple text'); }); }); describe('parse all (RealNames)', () => { beforeEach(() => { mentionsParser.useRealName = () => true; }); it('should render for #general', () => { message.html = '#general'; const result = mentionsParser.parse(message, 'me'); expect(result.html).to.be.equal('#general'); }); it('should render for "#general and @rocket.cat', () => { message.html = '#general and @rocket.cat'; const result = mentionsParser.parse(message, 'me'); expect(result.html).to.be.equal( '#general and Rocket.Cat', ); }); it('should render for "', () => { message.html = ''; const result = mentionsParser.parse(message, 'me'); expect(result.html).to.be.equal(''); }); it('should render for "simple text', () => { message.html = 'simple text'; const result = mentionsParser.parse(message, 'me'); expect(result.html).to.be.equal('simple text'); }); }); });