2022-06-21 00:08:43 +00:00
// See https://docs.gitlab.com/ee/development/gitlab_flavored_markdown/specification_guide/#markdown-snapshot-testing
// for documentation on this spec.
import fs from 'fs' ;
import path from 'path' ;
import jsYaml from 'js-yaml' ;
import { pick } from 'lodash' ;
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 = ( dir , fileName ) => {
const yaml = fs . readFileSync ( path . join ( dir , fileName ) ) ;
const examples = jsYaml . safeLoad ( yaml , { } ) ;
return filterExamples ( examples ) ;
} ;
// eslint-disable-next-line jest/no-export
2022-06-22 21:08:26 +00:00
export const describeMarkdownSnapshots = ( description , glfmSpecificationDir ) => {
2022-06-21 00:08:43 +00:00
let actualHtmlAndJsonExamples ;
let skipRunningSnapshotWysiwygHtmlTests ;
let skipRunningSnapshotProsemirrorJsonTests ;
const exampleStatuses = loadExamples (
path . join ( glfmSpecificationDir , 'input' , 'gitlab_flavored_markdown' ) ,
'glfm_example_status.yml' ,
) ;
2022-06-22 21:08:26 +00:00
const glfmExampleSnapshotsDir = path . join ( glfmSpecificationDir , 'example_snapshots' ) ;
2022-06-21 00:08:43 +00:00
const markdownExamples = loadExamples ( glfmExampleSnapshotsDir , 'markdown.yml' ) ;
const expectedHtmlExamples = loadExamples ( glfmExampleSnapshotsDir , 'html.yml' ) ;
const expectedProseMirrorJsonExamples = loadExamples (
glfmExampleSnapshotsDir ,
'prosemirror_json.yml' ,
) ;
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 ,
) ;
} ) ;
}
} ) ;
} ) ;
} ;