describe('Filters', function() { let $filter; // Ignoring page reload request beforeAll(() => window.onbeforeunload = () => null); let webClientServiceMock = { contacts: { get: function(id) { if (id === 'AAAAAAAA') { return { displayName: 'ContactA' } } else if (id === 'XXXXXXXX') { return { displayName: 'ContactX' } } else if (id === '*AAAAAAA') { return { displayName: 'GWContactA' } } return null; } } }; let translationMock = { instant: function(label) { return label; } }; beforeEach(function() { // Load 3ema.filters module module('3ema.filters'); module(function ($provide) { $provide.value('WebClientService', webClientServiceMock); $provide.value('$translate', translationMock); }); // Inject the $filter function inject(function(_$filter_) { $filter = _$filter_; }); }); function testPatterns(filterName, cases) { const filter = $filter(filterName); for (let testcase of cases) { const input = testcase[0]; const expected = testcase[1]; expect(filter(input)).toEqual(expected); }; }; describe('markify', function() { this.testPatterns = (cases) => testPatterns('markify', cases); it('detects bold text', () => { this.testPatterns([ ['*bold text (not italic)*', 'bold text (not italic)'], ]); }); it('detects italic text', () => { this.testPatterns([ ['This text is not italic.', 'This text is not italic.'], ['_This text is italic._', 'This text is italic.'], ['This text is _partially_ italic', 'This text is partially italic'], ['This text has _two_ _italic_ bits', 'This text has two italic bits'], ]); }); it('detects strikethrough text', () => { this.testPatterns([ ['so ~strikethrough~', 'so strikethrough'], ]); }); it('detects mixed markup', () => { this.testPatterns([ ['*bold text with _italic_ *', 'bold text with italic '], ['*part bold,* _part italic_', 'part bold, part italic'], ['_italic text with *bold* _', 'italic text with bold '], ]); }); it('is only applied on word boundaries', () => { this.testPatterns([ ['so not_really_italic', 'so not_really_italic'], ['invalid*bold*stuff', 'invalid*bold*stuff'], ['no~strike~through', 'no~strike~through'], ['*bold_but_no~strike~through*', 'bold_but_no~strike~through'], ]); }); it('does not break URLs', () => { this.testPatterns([ ['https://en.wikipedia.org/wiki/Java_class_file *nice*', 'https://en.wikipedia.org/wiki/Java_class_file nice'], ['Threema'], ]); }); it('ignores invalid markup', () => { this.testPatterns([ ['*invalid markup (do not parse)_', '*invalid markup (do not parse)_'], ['random *asterisk', 'random *asterisk'], ]); }); it('ignores markup with \\n (newline)', () => { this.testPatterns([ ['*First line\n and a new one. (do not parse)*', '*First line\n and a new one. (do not parse)*'], ['*\nbegins with linebreak. (do not parse)*', '*\nbegins with linebreak. (do not parse)*'], ['*Just some text. But it ends with newline (do not parse)\n*', '*Just some text. But it ends with newline (do not parse)\n*'], ]); }); }); describe('escapeHtml', function() { this.testPatterns = (cases) => testPatterns('escapeHtml', cases); it('escapes html tags', () => { this.testPatterns([ ['

heading

', '<h1>heading</h1>'], ['< script >foo–< script>', '<b>< script >foo&ndash;</b>< script>'], ['a', '<a href="/">a</a>'], ]); }); }); describe('mentionify', function() { this.testPatterns = (cases) => testPatterns('mentionify', cases); it('no mentions', () => { this.testPatterns([ ['', ''], ['hello my friend', 'hello my friend'], ['@[AAAAAAA]', '@[AAAAAAA]'], ['this is not a valid @[AAAAAAA]', 'this is not a valid @[AAAAAAA]'], ['@[@@@@@@@]', '@[@@@@@@@]'], ['this is not a valid @[@@@@@@@]', 'this is not a valid @[@@@@@@@]'], ]); }); it('mention - no contacts', () => { this.testPatterns([ ['@[BBBBBBBB]', '@[BBBBBBBB]'], ['@[*BBBBBBB]', '@[*BBBBBBB]'], ]); }); it('mention - contact', () => { this.testPatterns([ ['@[AAAAAAAA]', 'ContactA'], ['hello @[AAAAAAAA]. @[AAAAAAAA] you are my friend', 'hello ContactA. ContactA you are my friend'], ['@[AAAAAAAA] @[AAAAAAAA] @[AAAAAAAA]', 'ContactA ContactA ContactA'] ]); }); it('mention - all', () => { this.testPatterns([ ['@[@@@@@@@@]', 'messenger.ALL'], ['@[@@@@@@@@] your base are belong to us', 'messenger.ALL your base are belong to us'], ['@[@@@@@@@@] @[@@@@@@@@] @[@@@@@@@@]', 'messenger.ALL messenger.ALL messenger.ALL'] ]); }); it('mention - mixed', () => { this.testPatterns([ ['@[@@@@@@@@] @[AAAAAAAA] @[BBBBBBBB]', 'messenger.ALL ContactA @[BBBBBBBB]'], ]); }); }); });