describe('Filters', function() { let $filter; beforeAll(() => window.onbeforeunload = () => 'Ignoring page reload request'); beforeEach(function() { // Load 3ema.filters module module('3ema.filters'); // Inject the $filter function inject(function(_$filter_) { $filter = _$filter_; }); }); describe('markify', function() { this.testPatterns = (cases) => { const filter = $filter('markify'); for (let testcase of cases) { const input = testcase[0]; const expected = testcase[1]; expect(filter(input)).toEqual(expected); }; }; 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'], ]); }); }); describe('htmlToAsciiMarkup', function() { this.testPatterns = (cases) => { const filter = $filter('htmlToAsciiMarkup'); for (let testcase of cases) { const input = testcase[0]; const expected = testcase[1]; expect(filter(input)).toEqual(expected); }; }; it('converts bold text', () => { this.testPatterns([ ['bold', '*bold*'], ['< b >bold', '*bold*'], ['bold', '*bold*'], ['bold', '*bold*'], ['bold', '*bold*'], ['bold', '**bold**'], ['bold', '**bold**'], ]); }); it('converts italic text', () => { this.testPatterns([ ['italic', '_italic_'], ['italic', '_italic_'], ['italic', '_italic_'], ['italic', '__italic__'], ]); }); it('converts strikethrough text', () => { this.testPatterns([ ['strikethrough', '~strikethrough~'], ['strikethrough', '~strikethrough~'], ['strikethrough', '~strikethrough~'], ['strikethrough', '~strikethrough~'], ['strikethrough', '~~~strikethrough~~~'], ]); }); it('does not affect other tags', () => { this.testPatterns([ ['', ''], ]); }); it('combination of all', () => { this.testPatterns([ ['foo', '*_~foo~_*'], ]); }); }); });