summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/test/unit/html-formatter.spec.ts
blob: dd954bfc94bc35474f8a947bd6b2a30b9d575a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { describe, it } from 'mocha';
import React from 'react';

import { formatHtml } from '../../src/renderer/lib/html-formatter';
import { createFragment, expectChildrenToMatch, expectChildrenToMatchElements } from './utils';

describe('Format html', () => {
  it('should format middle bold tag', () => {
    expectChildrenToMatch(formatHtml('Some <b>bold</b> text'), ['Some ', 'bold', ' text']);
  });
  it('should format starting bold tag', () => {
    expectChildrenToMatch(formatHtml('<b>Some</b> bold text'), ['Some', ' bold text']);
  });
  it('should format ending bold tag', () => {
    expectChildrenToMatch(formatHtml('Some bold <b>text</b>'), ['Some bold ', 'text']);
  });
  it('should format multiple bold tags', () => {
    expectChildrenToMatch(formatHtml('Some <b>bold</b> and <b>more bold</b> text'), [
      'Some ',
      'bold',
      ' and ',
      'more bold',
      ' text',
    ]);
  });
  it('should produce reliable output on each call', () => {
    expectChildrenToMatch(formatHtml('<b>Some</b> bold text'), ['Some', ' bold text']);
    expectChildrenToMatch(formatHtml('Some non bold text'), ['Some non bold text']);
    // Same string used as in first expectChildrenToMatch call
    expectChildrenToMatch(formatHtml('<b>Some</b> bold text'), ['Some', ' bold text']);
  });
  it('should format middle emphasis tag', () => {
    expectChildrenToMatch(formatHtml('Some <em>emphasized</em> text'), [
      'Some ',
      'emphasized',
      ' text',
    ]);
  });
  it('should format starting emphasis tag', () => {
    expectChildrenToMatch(formatHtml('<em>Some</em> emphasized text'), [
      'Some',
      ' emphasized text',
    ]);
  });
  it('should format ending emphasis tag', () => {
    expectChildrenToMatch(formatHtml('Some emphasized <em>text</em>'), [
      'Some emphasized ',
      'text',
    ]);
  });
  it('should format multiple emphasis tags', () => {
    expectChildrenToMatch(
      formatHtml('Some <em>emphasized</em> and <em>more emphasized</em> text'),
      ['Some ', 'emphasized', ' and ', 'more emphasized', ' text'],
    );
  });
  it('should format both bold and emphasis tags', () => {
    expectChildrenToMatch(formatHtml('Some <b>bold</b> and <em>emphasized</em> text'), [
      'Some ',
      'bold',
      ' and ',
      'emphasized',
      ' text',
    ]);
  });
  it('should format multiple bold and emphasis tags', () => {
    expectChildrenToMatch(
      formatHtml(
        'Some <b>bold</b> and <em>emphasized</em> text. Then another <b>bold text</b> and one more <em>text</em> which was emphasized.',
      ),
      [
        'Some ',
        'bold',
        ' and ',
        'emphasized',
        ' text. Then another ',
        'bold text',
        ' and one more ',
        'text',
        ' which was emphasized.',
      ],
    );
  });
  it('should format using custom transformer', () => {
    expectChildrenToMatch(
      formatHtml('Some <b>bold</b> text', {
        b: () => createFragment('override value'),
      }),
      ['Some ', 'override value', ' text'],
    );
  });
  it('should format using custom transformer and produce correct output', () => {
    expectChildrenToMatchElements(
      formatHtml('Some <b>bold</b> text', {
        b: (value) => React.createElement('div', null, value),
      }),
      [createFragment('Some '), React.createElement('div', null, 'bold'), createFragment(' text')],
    );
  });
});