summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/extensions/link_spec.js
blob: 026b2a06df3e6f9baa0dd0c18a1a1260862a7b6f (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
import {
  markdownLinkSyntaxInputRuleRegExp,
  urlSyntaxRegExp,
  extractHrefFromMarkdownLink,
} from '~/content_editor/extensions/link';

describe('content_editor/extensions/link', () => {
  describe.each`
    input                             | matches
    ${'[gitlab](https://gitlab.com)'} | ${true}
    ${'[documentation](readme.md)'}   | ${true}
    ${'[link 123](readme.md)'}        | ${true}
    ${'[link 123](read me.md)'}       | ${true}
    ${'text'}                         | ${false}
    ${'documentation](readme.md'}     | ${false}
    ${'https://www.google.com'}       | ${false}
  `('markdownLinkSyntaxInputRuleRegExp', ({ input, matches }) => {
    it(`${matches ? 'matches' : 'does not match'} ${input}`, () => {
      const match = new RegExp(markdownLinkSyntaxInputRuleRegExp).exec(input);

      expect(Boolean(match?.groups.href)).toBe(matches);
    });
  });

  describe.each`
    input                        | matches
    ${'http://example.com '}     | ${true}
    ${'https://example.com '}    | ${true}
    ${'www.example.com '}        | ${true}
    ${'example.com/ab.html '}    | ${false}
    ${'text'}                    | ${false}
    ${' http://example.com '}    | ${true}
    ${'https://www.google.com '} | ${true}
  `('urlSyntaxRegExp', ({ input, matches }) => {
    it(`${matches ? 'matches' : 'does not match'} ${input}`, () => {
      const match = new RegExp(urlSyntaxRegExp).exec(input);

      expect(Boolean(match?.groups.href)).toBe(matches);
    });
  });

  describe('extractHrefFromMarkdownLink', () => {
    const input = '[gitlab](https://gitlab.com)';
    const href = 'https://gitlab.com';
    let match;
    let result;

    beforeEach(() => {
      match = new RegExp(markdownLinkSyntaxInputRuleRegExp).exec(input);
      result = extractHrefFromMarkdownLink(match);
    });

    it('extracts the url from a markdown link captured by markdownLinkSyntaxInputRuleRegExp', () => {
      expect(result).toEqual({ href });
    });

    it('makes sure that url text is the last capture group', () => {
      expect(match[match.length - 1]).toEqual('gitlab');
    });
  });
});