Welcome to mirror list, hosted at ThFree Co, Russian Federation.

markdown_snapshot_spec_helper.js « content_editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 64988c5b71754c5e157b009d482b8f7af1001774 (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
// See https://docs.gitlab.com/ee/development/gitlab_flavored_markdown/specification_guide/#markdown-snapshot-testing
// for documentation on this spec.

import jsYaml from 'js-yaml';
import { pick } from 'lodash';
import glfmExampleStatusYml from '../../../glfm_specification/input/gitlab_flavored_markdown/glfm_example_status.yml';
import markdownYml from '../../../glfm_specification/output_example_snapshots/markdown.yml';
import htmlYml from '../../../glfm_specification/output_example_snapshots/html.yml';
import prosemirrorJsonYml from '../../../glfm_specification/output_example_snapshots/prosemirror_json.yml';
import {
  IMPLEMENTATION_ERROR_MSG,
  renderHtmlAndJsonForAllExamples,
} from './render_html_and_json_for_all_examples';

const filterExamples = (examples) => {
  const focusedMarkdownExamples = process.env.FOCUSED_MARKDOWN_EXAMPLES?.split(',') || [];
  if (!focusedMarkdownExamples.length) {
    return examples;
  }
  return pick(examples, focusedMarkdownExamples);
};

const loadExamples = (yaml) => {
  const examples = jsYaml.safeLoad(yaml, {});
  return filterExamples(examples);
};

// eslint-disable-next-line jest/no-export
export const describeMarkdownSnapshots = (description) => {
  let actualHtmlAndJsonExamples;
  let skipRunningSnapshotWysiwygHtmlTests;
  let skipRunningSnapshotProsemirrorJsonTests;

  const exampleStatuses = loadExamples(glfmExampleStatusYml);
  const markdownExamples = loadExamples(markdownYml);
  const expectedHtmlExamples = loadExamples(htmlYml);
  const expectedProseMirrorJsonExamples = loadExamples(prosemirrorJsonYml);

  beforeAll(async () => {
    return renderHtmlAndJsonForAllExamples(markdownExamples).then((examples) => {
      actualHtmlAndJsonExamples = examples;
    });
  });

  describe(description, () => {
    const exampleNames = Object.keys(markdownExamples);

    describe.each(exampleNames)('%s', (name) => {
      const exampleNamePrefix = 'verifies conversion of GLFM to';
      skipRunningSnapshotWysiwygHtmlTests =
        exampleStatuses[name]?.skip_running_snapshot_wysiwyg_html_tests;
      skipRunningSnapshotProsemirrorJsonTests =
        exampleStatuses[name]?.skip_running_snapshot_prosemirror_json_tests;

      const markdown = markdownExamples[name];

      if (skipRunningSnapshotWysiwygHtmlTests) {
        it.todo(`${exampleNamePrefix} HTML: ${skipRunningSnapshotWysiwygHtmlTests}`);
      } else {
        it(`${exampleNamePrefix} HTML`, async () => {
          const expectedHtml = expectedHtmlExamples[name].wysiwyg;
          const { html: actualHtml } = actualHtmlAndJsonExamples[name];

          // noinspection JSUnresolvedFunction (required to avoid RubyMine type inspection warning, because custom matchers auto-imported via Jest test setup are not automatically resolved - see https://youtrack.jetbrains.com/issue/WEB-42350/matcher-for-jest-is-not-recognized-but-it-is-runable)
          expect(actualHtml).toMatchExpectedForMarkdown(
            'HTML',
            name,
            markdown,
            IMPLEMENTATION_ERROR_MSG,
            expectedHtml,
          );
        });
      }

      if (skipRunningSnapshotProsemirrorJsonTests) {
        it.todo(
          `${exampleNamePrefix} ProseMirror JSON: ${skipRunningSnapshotProsemirrorJsonTests}`,
        );
      } else {
        it(`${exampleNamePrefix} ProseMirror JSON`, async () => {
          const expectedJson = expectedProseMirrorJsonExamples[name];
          const { json: actualJson } = actualHtmlAndJsonExamples[name];

          // noinspection JSUnresolvedFunction
          expect(actualJson).toMatchExpectedForMarkdown(
            'JSON',
            name,
            markdown,
            IMPLEMENTATION_ERROR_MSG,
            expectedJson,
          );
        });
      }
    });
  });
};