From d9072f2fd49247eb5a6f6fde403a334dc09b9bad Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Tue, 6 Oct 2020 02:58:59 +0200 Subject: [PATCH] Reformat test file --- __test__/test.js | 273 ++++++++++++++++++++++++++--------------------- package.json | 2 +- 2 files changed, 152 insertions(+), 123 deletions(-) diff --git a/__test__/test.js b/__test__/test.js index 90b5fe2..eb1922b 100644 --- a/__test__/test.js +++ b/__test__/test.js @@ -1,103 +1,67 @@ -const md = require("markdown-it")({ - html: false, - xhtmlOut: true, - typographer: true -}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§' } ) - .use( require('../dist/markdownItTocDoneRight.js') ); - - - - - -const uslug = require("uslug"); -function uslugify(x) { - return uslug(x); -} -function custom_format(x, h) { - return `${h(x)}`; +/* eslint-env jest */ +/* eslint-disable no-template-curly-in-string */ + +const markdownIt = require('markdown-it') +const markdownItAnchor = require('markdown-it-anchor') +const markdownItTocDoneRight = require('../dist/markdownItTocDoneRight') +const uslug = require('uslug') + +function getMarkdownIt () { + return markdownIt({ + html: false, + xhtmlOut: true, + typographer: true + }) } -const umd = require("markdown-it")({ - html: false, - xhtmlOut: true, - typographer: true -}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§', slugify: uslugify } ) - .use( require('../dist/markdownItTocDoneRight.js'), { - placeholder: "\\@\\[\\[TOC\\]\\]", - slugify: uslugify, - containerClass: "user-content-toc", - listClass: "my-list", - itemClass: "my-item", - linkClass: "my-link", - listType: "ul", - format: custom_format, - callback: function (html, ast) { - console.log(ast) - } -} ); - - - - - -const level_md = require("markdown-it")({ - html: false, - xhtmlOut: true, - typographer: true -}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§', level: 2 } ) - .use( require('../dist/markdownItTocDoneRight.js'), { level: 2 } ); - - - - - -const level_md_array = require("markdown-it")({ - html: false, - xhtmlOut: true, - typographer: true -}).use( require("markdown-it-anchor"), { permalink: true, permalinkBefore: true, permalinkSymbol: '§', level: [1, 2] } ) - .use( require('../dist/markdownItTocDoneRight.js'), { level: [1, 2] } ); - - - - - -const containerId_md = require("markdown-it")({ - html: false, - xhtmlOut: true, - typographer: true -}).use( require('../dist/markdownItTocDoneRight.js'), { containerId: "toc" } ); - - - +function uslugify (text) { + return uslug(text) +} -test("empty set", () => { - expect( md.render("${toc}") ).toBe(''); -}); +function customFormat (content, htmlEncoder) { + return `${htmlEncoder(content)}` +} -test("containerId is set correctly", () => { - expect( containerId_md.render("${toc}") ).toBe(''); -}); +const commonMd = getMarkdownIt() + .use(markdownItAnchor, { + permalink: true, + permalinkBefore: true, + permalinkSymbol: '§' + }) + .use(markdownItTocDoneRight) + +test('empty set', () => { + const mdContent = '${toc}' + const htmlContent = '' + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) -test("single element", () => { - expect( md.render("${toc}\n\n# Title") ).toBe('

§ Title

\n'); -}); +test('single element', () => { + const mdContent = '${toc}\n\n# Title' + const htmlContent = '

§ Title

\n' + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) test("single element doesn't need to be h1", () => { - expect( md.render("${toc}\n\n## Section") ).toBe('

§ Section

\n'); -}); + const mdContent = '${toc}\n\n## Section' + const htmlContent = '

§ Section

\n' + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) -test("main heading usually comes first", () => { - expect( md.render("${toc}\n\n# Title\n\n## Section 1\n\n### Sub Section 1\n\n### Sub Section 2\n\n## Section 2") ).toBe(`

§ Title

+test('main heading usually comes first', () => { + const mdContent = '${toc}\n\n# Title\n\n## Section 1\n\n### Sub Section 1\n\n### Sub Section 2\n\n## Section 2' + const htmlContent = `

§ Title

§ Section 1

§ Sub Section 1

§ Sub Section 2

§ Section 2

-`); -}); +` + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) -test("but sometimes it comes after navigation", () => { - expect( md.render("${toc}\n\n## Navigation Menu\n\n## Sidebar\n\n### More news\n\n### What our clients say\n\n### Ratings\n\n# Title\n\n## Section 1\n\n### Sub Section 1\n\n### Sub Section 2\n\n## Section 2") ).toBe(` +test('but sometimes it comes after navigation', () => { + const mdContent = '${toc}\n\n## Navigation Menu\n\n## Sidebar\n\n### More news\n\n### What our clients say\n\n### Ratings\n\n# Title\n\n## Section 1\n\n### Sub Section 1\n\n### Sub Section 2\n\n## Section 2' + const htmlContent = `

§ More news

§ What our clients say

@@ -107,42 +71,52 @@ test("but sometimes it comes after navigation", () => {

§ Sub Section 1

§ Sub Section 2

§ Section 2

-`); -}); +` + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) -test("markup inside the heading should be treated", () => { - expect( md.render("${toc}\n\n# A **heading** with *markup* `code` [link](https://github.com)") ).toBe('

§ A heading with markup code link

\n'); -}); +test('markup inside the heading should be treated', () => { + const mdContent = '${toc}\n\n# A **heading** with *markup* `code` [link](https://github.com)' + const htmlContent = '

§ A heading with markup code link

\n' + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) -test("especially < and >", () => { - expect( md.render("${toc}\n\n# > Title\n\n## < Section\n\n## > Another Section") ).toBe(`

§ > Title

+test('especially < and >', () => { + const mdContent = '${toc}\n\n# > Title\n\n## < Section\n\n## > Another Section' + const htmlContent = `

§ > Title

§ < Section

§ > Another Section

-`); -}); +` + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) -test("skipping heading ranks should work", () => { - expect( md.render("${toc}\n\n## Foo\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tempus porta nulla id porttitor. Vivamus sagittis, leo eget gravida euismod, justo ex dignissim sem, in tempus turpis libero quis velit. Aliquam sit amet ultricies quam.\n\n#### Bar\nSuspendisse ornare pellentesque nibh non tristique. Suspendisse potenti. Nunc id dui non diam luctus imperdiet.\n\n## Grob\nQuisque fringilla urna sit amet elit ultrices tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. In posuere tellus suscipit sapien rhoncus euismod. Phasellus eu ligula mollis, finibus tortor ut, consectetur metus. Vestibulum eget leo felis.") ).toBe(`

§ Foo

+test('skipping heading ranks should work', () => { + const mdContent = '${toc}\n\n## Foo\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tempus porta nulla id porttitor. Vivamus sagittis, leo eget gravida euismod, justo ex dignissim sem, in tempus turpis libero quis velit. Aliquam sit amet ultricies quam.\n\n#### Bar\nSuspendisse ornare pellentesque nibh non tristique. Suspendisse potenti. Nunc id dui non diam luctus imperdiet.\n\n## Grob\nQuisque fringilla urna sit amet elit ultrices tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. In posuere tellus suscipit sapien rhoncus euismod. Phasellus eu ligula mollis, finibus tortor ut, consectetur metus. Vestibulum eget leo felis.' + const htmlContent = `

§ Foo

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tempus porta nulla id porttitor. Vivamus sagittis, leo eget gravida euismod, justo ex dignissim sem, in tempus turpis libero quis velit. Aliquam sit amet ultricies quam.

§ Bar

Suspendisse ornare pellentesque nibh non tristique. Suspendisse potenti. Nunc id dui non diam luctus imperdiet.

§ Grob

Quisque fringilla urna sit amet elit ultrices tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. In posuere tellus suscipit sapien rhoncus euismod. Phasellus eu ligula mollis, finibus tortor ut, consectetur metus. Vestibulum eget leo felis.

-`); +` + expect(commonMd.render(mdContent)).toBe(htmlContent) }) -test("code blocks should not be confused as headings", () => { - expect( md.render("[[_toc_]]\n\n## Foo\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tempus porta nulla id porttitor. Vivamus sagittis, leo eget gravida euismod, justo ex dignissim sem, in tempus turpis libero quis velit. Aliquam sit amet ultricies quam.\n\n#### Bar\nSuspendisse ornare pellentesque nibh non tristique. Suspendisse potenti. Nunc id dui non diam luctus imperdiet.\n\n ## Grob\n Quisque fringilla urna sit amet elit ultrices tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. In posuere tellus suscipit sapien rhoncus euismod. Phasellus eu ligula mollis, finibus tortor ut, consectetur metus. Vestibulum eget leo felis.") ).toBe(`

§ Foo

+test('code blocks should not be confused as headings', () => { + const mdContent = '[[_toc_]]\n\n## Foo\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tempus porta nulla id porttitor. Vivamus sagittis, leo eget gravida euismod, justo ex dignissim sem, in tempus turpis libero quis velit. Aliquam sit amet ultricies quam.\n\n#### Bar\nSuspendisse ornare pellentesque nibh non tristique. Suspendisse potenti. Nunc id dui non diam luctus imperdiet.\n\n ## Grob\n Quisque fringilla urna sit amet elit ultrices tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. In posuere tellus suscipit sapien rhoncus euismod. Phasellus eu ligula mollis, finibus tortor ut, consectetur metus. Vestibulum eget leo felis.' + const htmlContent = `

§ Foo

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tempus porta nulla id porttitor. Vivamus sagittis, leo eget gravida euismod, justo ex dignissim sem, in tempus turpis libero quis velit. Aliquam sit amet ultricies quam.

§ Bar

Suspendisse ornare pellentesque nibh non tristique. Suspendisse potenti. Nunc id dui non diam luctus imperdiet.

## Grob
 Quisque fringilla urna sit amet elit ultrices tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. In posuere tellus suscipit sapien rhoncus euismod. Phasellus eu ligula mollis, finibus tortor ut, consectetur metus. Vestibulum eget leo felis.
-`); -}); +` + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) -test("headers innerText may happen more than once", () => { - expect( md.render("[toc]\n\n# Title\n\n## Section 1\n\n### Subsection 1\n\n### Subsection 2\n\n## Section 2\n\n### Subsection 1\n\n### Subsection 2\n\n## Section 3\n\n### Subsection 1\n\n### Subsection 2") ).toBe(`

§ Title

+test('headers innerText may happen more than once', () => { + const mdContent = '[toc]\n\n# Title\n\n## Section 1\n\n### Subsection 1\n\n### Subsection 2\n\n## Section 2\n\n### Subsection 1\n\n### Subsection 2\n\n## Section 3\n\n### Subsection 1\n\n### Subsection 2' + const htmlContent = `

§ Title

§ Section 1

§ Subsection 1

§ Subsection 2

@@ -152,33 +126,88 @@ test("headers innerText may happen more than once", () => {

§ Section 3

§ Subsection 1

§ Subsection 2

-`); -}); +` + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) -test("and sometimes slugify with suffix may generate another existing header", () => { - expect( md.render("[[toc]]\n\n# header\n\n## header\n\n## header 2") ).toBe(`

§ header

+test('and sometimes slugify with suffix may generate another existing header', () => { + const mdContent = '[[toc]]\n\n# header\n\n## header\n\n## header 2' + const htmlContent = `

§ header

§ header

§ header 2

-`); -}); +` + expect(commonMd.render(mdContent)).toBe(htmlContent) +}) -test("level(Int Type) option should work as expected", () => { - expect( level_md.render("${toc}\n\n# header\n\n## header\n\n## header 2") ).toBe(`

header

+test('containerId is set correctly', () => { + const md = getMarkdownIt() + .use(markdownItTocDoneRight, { + containerId: 'toc' + }) + const mdContent = '${toc}' + const htmlContent = '' + expect(md.render(mdContent)).toBe(htmlContent) +}) + +test('level(Int Type) option should work as expected', () => { + const md = getMarkdownIt() + .use(markdownItAnchor, { + permalink: true, + permalinkBefore: true, + permalinkSymbol: '§', + level: 2 + }) + .use(markdownItTocDoneRight, { level: 2 }) + const mdContent = '${toc}\n\n# header\n\n## header\n\n## header 2' + const htmlContent = `

header

§ header 2

-`); -}); +` + expect(md.render(mdContent)).toBe(htmlContent) +}) -test("level(Array Type) option should work as expected", () => { - expect( level_md_array.render("${toc}\n\n# header\n\n## header\n\n## header 2\n\n# header 4\n\n## header 5\n\n## header 2") ).toBe(`

§ header

+test('level(Array Type) option should work as expected', () => { + const md = getMarkdownIt() + .use(markdownItAnchor, { + permalink: true, + permalinkBefore: true, + permalinkSymbol: '§', + level: [1, 2] + }) + .use(markdownItTocDoneRight, { level: [1, 2] }) + const mdContent = '${toc}\n\n# header\n\n## header\n\n## header 2\n\n# header 4\n\n## header 5\n\n## header 2' + const htmlContent = `

§ header

§ header

§ header 2

§ header 4

§ header 5

§ header 2

-`); -}); +` + expect(md.render(mdContent)).toBe(htmlContent) +}) -test("all other options should work as expected", () => { - expect( umd.render("@[[TOC]]\n\n# 日本語\n\n # should be considered as code") ).toBe('

§ 日本語

\n
# should be considered as code
\n'); -}); +test('all other options should work as expected', () => { + const md = getMarkdownIt() + .use(markdownItAnchor, { + permalink: true, + permalinkBefore: true, + permalinkSymbol: '§', + slugify: uslugify + }) + .use(markdownItTocDoneRight, { + placeholder: '\\@\\[\\[TOC\\]\\]', + slugify: uslugify, + containerClass: 'user-content-toc', + listClass: 'my-list', + itemClass: 'my-item', + linkClass: 'my-link', + listType: 'ul', + format: customFormat, + callback: function (html, ast) { + console.log(ast) + } + }) + const mdContent = '@[[TOC]]\n\n# 日本語\n\n # should be considered as code' + const htmlContent = '

§ 日本語

\n
# should be considered as code
\n' + expect(md.render(mdContent)).toBe(htmlContent) +}) diff --git a/package.json b/package.json index d912359..29bf234 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "build": "microbundle", "dev": "microbundle watch", - "lint": "standard --verbose index.js | snazzy", + "lint": "standard --verbose index.js __test__/test.js | snazzy", "test": "jest --coverage" }, "repository": {