summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/gfm/index_spec.js
blob: b722315d63a971c9dbc77dd6b962d50ca4cde83e (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
import { render } from '~/lib/gfm';

describe('gfm', () => {
  const markdownToAST = async (markdown, skipRendering = []) => {
    let result;

    await render({
      markdown,
      skipRendering,
      renderer: (tree) => {
        result = tree;
      },
    });

    return result;
  };

  const expectInRoot = (result, ...nodes) => {
    expect(result).toEqual(
      expect.objectContaining({
        children: expect.arrayContaining(nodes),
      }),
    );
  };

  describe('render', () => {
    it('processes Commonmark and provides an ast to the renderer function', async () => {
      const result = await markdownToAST('This is text');

      expect(result.type).toBe('root');
    });

    it('transforms raw HTML into individual nodes in the AST', async () => {
      const result = await markdownToAST('<strong>This is bold text</strong>');

      expectInRoot(
        result,
        expect.objectContaining({
          children: expect.arrayContaining([
            expect.objectContaining({
              type: 'element',
              tagName: 'strong',
            }),
          ]),
        }),
      );
    });

    it('returns the result of executing the renderer function', async () => {
      const rendered = { value: 'rendered tree' };

      const result = await render({
        markdown: '<strong>This is bold text</strong>',
        renderer: () => {
          return rendered;
        },
      });

      expect(result).toEqual(rendered);
    });

    describe('when skipping the rendering of footnote reference and definition nodes', () => {
      it('transforms footnotes into footnotedefinition and footnotereference tags', async () => {
        const result = await markdownToAST(
          `footnote reference [^footnote]

[^footnote]: Footnote definition`,
          ['footnoteReference', 'footnoteDefinition'],
        );

        expectInRoot(
          result,
          expect.objectContaining({
            children: expect.arrayContaining([
              expect.objectContaining({
                type: 'element',
                tagName: 'footnotereference',
                properties: {
                  identifier: 'footnote',
                  label: 'footnote',
                },
              }),
            ]),
          }),
        );

        expectInRoot(
          result,
          expect.objectContaining({
            tagName: 'footnotedefinition',
            properties: {
              identifier: 'footnote',
              label: 'footnote',
            },
          }),
        );
      });
    });
  });

  describe('when skipping the rendering of code blocks', () => {
    it('transforms code nodes into codeblock html tags', async () => {
      const result = await markdownToAST(
        `
\`\`\`javascript
console.log('Hola');
\`\`\`\
          `,
        ['code'],
      );

      expectInRoot(
        result,
        expect.objectContaining({
          tagName: 'codeblock',
          properties: {
            language: 'javascript',
          },
        }),
      );
    });
  });
});