marktext/test/unit/specs/markdown-basic.spec.js
Felix Häusler 2ce05829d7 Fix tests and improve CI (#590)
* Fix tests and improve CI

* Add xvfb to Linux - still problems with Mocha e2e tests

* Disable Webpack Bundle Analyzer for testing

* Use preinstalled yarn application on AppVeyor

* Fix build failure with latest vue version

* Remove "markdown-toc"

* Fix application title unit test

* Hide electron window during unit tests

* Add basic muya unit tests

* Dirty markdown-toc replacement

* Update dependencies

* Update eslint packages and configuration
2018-12-17 10:29:37 +08:00

105 lines
3.5 KiB
JavaScript

import ContentState from '../../../src/muya/lib/contentState'
import EventCenter from '../../../src/muya/lib/eventHandler/event'
import ExportMarkdown from '../../../src/muya/lib/utils/exportMarkdown'
import { MUYA_DEFAULT_OPTION } from '../../../src/muya/lib/config'
import * as templates from '../markdown'
const defaultOptions = { 'endOfLine': 'lf' }
const defaultOptionsCrlf = Object.assign({}, defaultOptions, { 'endOfLine': 'crlf' })
const createMuyaContext = options => {
const ctx = {}
ctx.options = Object.assign({}, MUYA_DEFAULT_OPTION, options)
ctx.eventCenter = new EventCenter()
ctx.contentState = new ContentState(ctx, ctx.options)
return ctx
}
// ----------------------------------------------------------------------------
// Muya parser (Markdown to HTML to Markdown)
//
const verifyMarkdown = (markdown, options) => {
const ctx = createMuyaContext(options)
ctx.contentState.importMarkdown(markdown)
const blocks = ctx.contentState.getBlocks()
const exportedMarkdown = new ExportMarkdown(blocks).generate()
// FIXME: We always need to add a new line at the end of the document. Add a option to disable the new line.
// Muya always use LF line endings.
expect(exportedMarkdown).to.equal(markdown)
}
describe('Muya parser', () => {
it('Basic Text Formatting', () => {
verifyMarkdown(templates.BasicTextFormattingTemplate(), defaultOptions)
})
it('Blockquotes', () => {
verifyMarkdown(templates.BlockquotesTemplate(), defaultOptions)
})
it('Code Blocks', () => {
verifyMarkdown(templates.CodeBlocksTemplate(), defaultOptions)
})
it('Escapes', () => {
verifyMarkdown(templates.EscapesTemplate(), defaultOptions)
})
it('Headings', () => {
verifyMarkdown(templates.HeadingsTemplate(), defaultOptions)
})
it('Images', () => {
verifyMarkdown(templates.ImagesTemplate(), defaultOptions)
})
it('Links', () => {
verifyMarkdown(templates.LinksTemplate(), defaultOptions)
})
it('Lists', () => {
verifyMarkdown(templates.ListsTemplate(), defaultOptions)
})
it('GFM - Basic Text Formatting', () => {
verifyMarkdown(templates.GfmBasicTextFormattingTemplate(), defaultOptions)
})
it('GFM - Lists', () => {
verifyMarkdown(templates.GfmListsTemplate(), defaultOptions)
})
it('GFM - Tables', () => {
verifyMarkdown(templates.GfmTablesTemplate(), defaultOptions)
})
})
describe('Muya parser (CRLF)', () => {
it('Basic Text Formatting', () => {
verifyMarkdown(templates.BasicTextFormattingTemplate(), defaultOptionsCrlf)
})
it('Blockquotes', () => {
verifyMarkdown(templates.BlockquotesTemplate(), defaultOptionsCrlf)
})
it('Code Blocks', () => {
verifyMarkdown(templates.CodeBlocksTemplate(), defaultOptionsCrlf)
})
it('Escapes', () => {
verifyMarkdown(templates.EscapesTemplate(), defaultOptionsCrlf)
})
it('Headings', () => {
verifyMarkdown(templates.HeadingsTemplate(), defaultOptionsCrlf)
})
it('Images', () => {
verifyMarkdown(templates.ImagesTemplate(), defaultOptionsCrlf)
})
it('Links', () => {
verifyMarkdown(templates.LinksTemplate(), defaultOptionsCrlf)
})
it('Lists', () => {
verifyMarkdown(templates.ListsTemplate(), defaultOptionsCrlf)
})
it('GFM - Basic Text Formatting', () => {
verifyMarkdown(templates.GfmBasicTextFormattingTemplate(), defaultOptionsCrlf)
})
it('GFM - Lists', () => {
verifyMarkdown(templates.GfmListsTemplate(), defaultOptionsCrlf)
})
it('GFM - Tables', () => {
verifyMarkdown(templates.GfmTablesTemplate(), defaultOptionsCrlf)
})
})