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)*',
                 '<span class="text-bold">bold text (not italic)</span>'],
            ]);
        });

        it('detects italic text', () => {
            this.testPatterns([
                ['This text is not italic.',
                 'This text is not italic.'],
                ['_This text is italic._',
                 '<span class="text-italic">This text is italic.</span>'],
                ['This text is _partially_ italic',
                 'This text is <span class="text-italic">partially</span> italic'],
                ['This text has _two_ _italic_ bits',
                 'This text has <span class="text-italic">two</span> <span class="text-italic">italic</span> bits'],
            ]);

        });

        it('detects strikethrough text', () => {
            this.testPatterns([
                ['so ~strikethrough~', 'so <span class="text-strike">strikethrough</span>'],
            ]);
        });

        it('detects mixed markup', () => {
            this.testPatterns([
                ['*bold text with _italic_ *',
                 '<span class="text-bold">bold text with <span class="text-italic">italic</span> </span>'],
                ['*part bold,* _part italic_',
                 '<span class="text-bold">part bold,</span> <span class="text-italic">part italic</span>'],
                ['_italic text with *bold* _',
                 '<span class="text-italic">italic text with <span class="text-bold">bold</span> </span>'],
            ]);
        });

        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*',
                 '<span class="text-bold">bold_but_no~strike~through</span>'],
            ]);
        });

        it('does not break URLs', () => {
            this.testPatterns([
                ['https://en.wikipedia.org/wiki/Java_class_file *nice*',
                 'https://en.wikipedia.org/wiki/Java_class_file <span class="text-bold">nice</span>'],
                ['<a href="https://threema.ch/>_Threema_</a>',
                 '<a href="https://threema.ch/><span class="text-italic">Threema</span></a>'],
            ]);
        });

        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([
                ['<b>bold</b>', '*bold*'],
                ['< 	b >bold</b>', '*bold*'],
                ['<B>bold</b>', '*bold*'],
                ['<b class="asdf">bold</b>', '*bold*'],
                ['<strong>bold</strong>', '*bold*'],
                ['<b><b>bold</b></b>', '**bold**'],
                ['<b><strong>bold</strong></b>', '**bold**'],
            ]);
        });

        it('converts italic text', () => {
            this.testPatterns([
                ['<i>italic</i>', '_italic_'],
                ['<i onclick="alert(1)">italic</i>', '_italic_'],
                ['<em>italic</em>', '_italic_'],
                ['<i><em>italic</em></i>', '__italic__'],
            ]);
        });

        it('converts strikethrough text', () => {
            this.testPatterns([
                ['<strike>strikethrough</strike>', '~strikethrough~'],
                ['<del>strikethrough</del>', '~strikethrough~'],
                ['<del href="/">strikethrough</del>', '~strikethrough~'],
                ['<s>strikethrough</s>', '~strikethrough~'],
                ['<strike><del><s>strikethrough</s></del></strike>', '~~~strikethrough~~~'],
            ]);
        });

        it('does not affect other tags', () => {
            this.testPatterns([
                ['<script>alert("pho soup time")</script>', '<script>alert("pho soup time")</script>'],
            ]);
        });

        it('combination of all', () => {
            this.testPatterns([
                ['<b><em><del>foo</del></em></b>', '*_~foo~_*'],
            ]);
        });

    });
});

@@ -366,87 +366,51 @@
     
-describe('markify', function() %7B%0A%0A        this.testPatterns = (cases) =%3E %7B%0A    
+function testPatterns(filterName, cases) %7B%0A
     
@@ -440,24 +440,21 @@
 ter(
-'markify');%0A    
+filterName);%0A
     
@@ -491,36 +491,32 @@
 ) %7B%0A            
-    
 const input = te
@@ -530,36 +530,32 @@
 0%5D;%0A            
-  
-  
 const expected =
@@ -560,36 +560,32 @@
  = testcase%5B1%5D;%0A
-    
             expe
@@ -629,32 +629,134 @@
         
+%7D;%0A
     %7D;%0A
+%0A
     
-    %7D
+describe('markify', function() %7B%0A%0A        this.testPatterns = (cases) =%3E testPatterns('markify', cases)
 ;%0A%0A     
@@ -3637,44 +3637,20 @@
  =%3E 
-%7B%0A            const filter = $fil
+testPat
 ter
+ns
 ('ht
@@ -3669,223 +3669,16 @@
 kup'
-);%0A            for (let testcase of cases) %7B%0A                const input = testcase%5B0%5D;%0A                const expected = testcase%5B1%5D;%0A                expect(filter(input)).toEqual(expected);%0A            %7D;%0A        %7D
+, cases)
 ;%0A%0A 

Simplify filter tests