123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- 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)*',
- '<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'],
- ]);
- });
- 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([
- ['<h1>heading</h1>', '<h1>heading</h1>'],
- ['<b>< script >foo–</b>< script>', '<b>< script >foo&ndash;</b>< script>'],
- ['<a href="/">a</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]', '<span class="mention contact">ContactA</span>'],
- ['hello @[AAAAAAAA]. @[AAAAAAAA] you are my friend', 'hello <span class="mention contact">ContactA</span>. <span class="mention contact">ContactA</span> you are my friend'],
- ['@[AAAAAAAA] @[AAAAAAAA] @[AAAAAAAA]', '<span class="mention contact">ContactA</span> <span class="mention contact">ContactA</span> <span class="mention contact">ContactA</span>']
- ]);
- });
- it('mention - all', () => {
- this.testPatterns([
- ['@[@@@@@@@@]', '<span class="mention all">messenger.ALL</span>'],
- ['@[@@@@@@@@] your base are belong to us', '<span class="mention all">messenger.ALL</span> your base are belong to us'],
- ['@[@@@@@@@@] @[@@@@@@@@] @[@@@@@@@@]', '<span class="mention all">messenger.ALL</span> <span class="mention all">messenger.ALL</span> <span class="mention all">messenger.ALL</span>']
- ]);
- });
- it('mention - mixed', () => {
- this.testPatterns([
- ['@[@@@@@@@@] @[AAAAAAAA] @[BBBBBBBB]', '<span class="mention all">messenger.ALL</span> <span class="mention contact">ContactA</span> @[BBBBBBBB]'],
- ]);
- });
- });
- });
|