aboutsummaryrefslogtreecommitdiff
path: root/__tests__/index.js
blob: 3c86feae6d64f5f5a82c5077147e5e6a5d83db18 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {readFileSync as file} from 'fs';
import {join} from 'path';
import unified from 'unified';
import test from 'ava';
import raw from 'rehype-raw';
import reParse from 'remark-parse';
import stringify from 'rehype-stringify';
import remark2rehype from 'remark-rehype';
import parse5 from 'parse5';
import stream from 'stream';

import plugin from '..';

const Stream = stream.Readable;

const render = text => unified()
  .use(reParse)
  .use(plugin, {allowDangerousDOMEventHandlers: false, scope: 'permissive'})
  .use(remark2rehype)
  .use(stringify)
  .processSync(text);

const renderRaw = text => unified()
  .use(reParse)
  .use(plugin, {allowDangerousDOMEventHandlers: true, scope: 'permissive'})
  .use(remark2rehype, {allowDangerousHTML: true})
  .use(raw)
  .use(stringify)
  .processSync(text);

const mainTestString = `Inline *test*{style="em:4"} paragraphe. Use **multiple**{ style="color:pink"} inline ~~block~~ tag. Line \`tagCode\`{ style="color:yellow"}.`;

function string2stream(string) {
  const stream = new Stream();
  stream.push(string);
  stream.push(null);
  return stream;
}

function propEgal(prop, attrs) {
  if (Object.getOwnPropertyNames(prop).length !== attrs.length) {
    return false;
  }

  attrs.forEach(e => {
    if (prop[e.name] !== e.value) {
      return false;
    }
  });

  return true;
}
function every(obj, fct) {
  Object.getOwnPropertyNames(obj).forEach(name => {
    if (!fct(obj[name])) {
      return false;
    }
  });
  return true;
}

test('basic', t => {
  const {contents} = render(mainTestString);
  const parser = new parse5.SAXParser();

  const nbTag = {em: 1, s: 1, code: 1, strong: 1, errorTag: 0};
  parser.on('startTag', name => {
    if (name in nbTag) {
      nbTag[name] -= 1;
    }
  });
  string2stream(contents).pipe(parser);
  t.true(every(nbTag, x => x === 0));
});

test('basic-raw', t => {
  const {contents} = renderRaw(mainTestString);
  const parser = new parse5.SAXParser();

  const nbTag = {em: 1, s: 1, code: 1, strong: 1, errorTag: 0};
  parser.on('startTag', name => {
    if (name in nbTag) {
      nbTag[name] -= 1;
    }
  });
  string2stream(contents).pipe(parser);
  t.true(every(nbTag, x => x === 0));
});

test('em', async t => {
  const {contents} = render('textexampleno interest **Important**{style=4em} still no interest');
  const parser = new parse5.SAXParser();

  parser.on('startTag', (name, attrs) => {
    if (name === 'strong') {
      t.true(propEgal({style: '4em'}, attrs));
    }
  });

  await string2stream(contents).pipe(parser);
});

test('readme', async t => {
  const fileExample = file(join(__dirname, 'readMeTest.txt'));
  const {contents} = render(fileExample);
  const parser = new parse5.SAXParser();

  parser.on('startTag', (name, attrs) => {
    switch (name) {
      case 'img':
        t.true(propEgal({height: 50, alt: 'alt', src: 'img'}, attrs));
        break;
      case 'a':
        t.true(propEgal({ref: 'external', src: 'https://rms.sexy'}, attrs));
        break;
      case 'h3':
        t.true(propEgal({style: 'color:red;'}, attrs));
        break;
      case 'em':
        t.true(propEgal({style: 'color:yellow;'}, attrs));
        break;
      case 'strong':
        t.true(propEgal({awesome: ''}, attrs));
        break;
      case 'del':
        t.true(propEgal({style: 'color: grey;'}, attrs));
        break;
      case 'code':
        t.true(propEgal({lang: 'c'}, attrs));
        break;
      default:
    }
  });

  await string2stream(contents).pipe(parser);
});