Skip to content

Commit

Permalink
feat: Add test codes
Browse files Browse the repository at this point in the history
  • Loading branch information
spring-raining committed Jul 20, 2020
1 parent a27d0ab commit 34aa884
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/tests/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)'],
testMatch: ['**/?(*.)+(spec|test).ts?(x)'],
testPathIgnorePatterns: ['lib'],
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"devDependencies": {
"@release-it/conventional-changelog": "^1.1.4",
"@types/common-tags": "^1.8.0",
"@types/debug": "^4.1.5",
"@types/hast": "^2.3.1",
"@types/jest": "^26.0.2",
Expand All @@ -58,6 +59,7 @@
"@typescript-eslint/eslint-plugin": "^3.5.0",
"@typescript-eslint/parser": "^3.5.0",
"@uetchy/doctoc": "^1.5.0",
"common-tags": "^1.8.0",
"eslint": "^7.3.1",
"husky": "^4.2.5",
"jest": "^26.1.0",
Expand Down
47 changes: 47 additions & 0 deletions tests/block/markdownInsideHtml.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { stripIndent } from 'common-tags';
import { buildProcessorTestingCode } from '../utils';

it(
'allows markdown context in new paragraph',
buildProcessorTestingCode(
stripIndent`
<div>
**Markdown inside HTML**
</div>
`,
stripIndent`
root[3]
├─0 html "<div>"
├─1 paragraph[1]
│ └─0 strong[1]
│ └─0 text "Markdown inside HTML"
└─2 html "</div>"
`,
stripIndent`
<div>
<p><strong>Markdown inside HTML</strong></p>
</div>
`,
),
);

it(
'same paragraph',
buildProcessorTestingCode(
stripIndent`
<div>
**Markdown inside HTML**
</div>
`,
stripIndent`
root[1]
└─0 html "<div>\\n**Markdown inside HTML**\\n</div>"
`,
stripIndent`
<div>
**Markdown inside HTML**
</div>
`,
),
);
87 changes: 87 additions & 0 deletions tests/inline/inlineMath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { stripIndent } from 'common-tags';
import { buildProcessorTestingCode } from '../utils';

it(
'simple inlineMath',
buildProcessorTestingCode(
`$x$`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 inlineMath "x"
data: {"hName":"span","hProperties":{"className":["math","math-inline"]},"hChildren":[{"type":"text","value":"x"}]}
`,
`<p><span class="math math-inline"><span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">x</span></span></span></span></span></p>`,
),
);

it(
"isn't greedy",
buildProcessorTestingCode(
`$x$y$`,
stripIndent`
root[1]
└─0 paragraph[2]
├─0 inlineMath "x"
│ data: {"hName":"span","hProperties":{"className":["math","math-inline"]},"hChildren":[{"type":"text","value":"x"}]}
└─1 text "y$"
`,
`<p><span class="math math-inline"><span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">x</span></span></span></span></span>y$</p>`,
),
);

it(
'double dollar',
buildProcessorTestingCode(
`$$x$$`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 inlineMath "x"
data: {"hName":"span","hProperties":{"className":["math","math-inline"]},"hChildren":[{"type":"text","value":"x"}]}
`,
`<p><span class="math math-inline"><span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">x</span></span></span></span></span></p>`,
),
);

it(
'empty math',
buildProcessorTestingCode(
`$$`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 text "$$"
`,
`<p>$$</p>`,
),
);

it(
'nested math',
buildProcessorTestingCode(
`$$a$b$$c$`,
stripIndent`
root[1]
└─0 paragraph[2]
├─0 inlineMath "a$b"
│ data: {"hName":"span","hProperties":{"className":["math","math-inline"]},"hChildren":[{"type":"text","value":"a$b"}]}
└─1 text "c$"
`,
`<p><span class="math math-inline"><span class="katex-error" title="ParseError: KaTeX parse error: Can&#x27;t use function &#x27;$&#x27; in math mode at position 2: a$̲b" style="color:#cc0000">a$b</span></span>c$</p>`,
),
);

it(
'math with newline',
buildProcessorTestingCode(
`$x\ny$`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 inlineMath "x\\ny"
data: {"hName":"span","hProperties":{"className":["math","math-inline"]},"hChildren":[{"type":"text","value":"x\\ny"}]}
`,
`<p><span class="math math-inline"><span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.19444em;"></span><span class="mord mathdefault">x</span><span class="mord mathdefault" style="margin-right:0.03588em;">y</span></span></span></span></span></p>`,
),
);
78 changes: 78 additions & 0 deletions tests/inline/ruby.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { stripIndent } from 'common-tags';
import { buildProcessorTestingCode } from '../utils';

it(
'simple ruby',
buildProcessorTestingCode(
`{a|b}`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 ruby[1]
│ data: {"hName":"ruby","rubyText":"b"}
└─0 text "a"
`,
`<p><ruby>a<rt>b</rt></ruby></p>`,
),
);

it(
'enables escape in ruby body',
buildProcessorTestingCode(
`{a\\|b|c}`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 ruby[1]
│ data: {"hName":"ruby","rubyText":"b|c"}
└─0 text "a\\\\"
`,
`<p><ruby>a\\<rt>b|c</rt></ruby></p>`,
),
);

it(
'disables any inline rule in <rt>',
buildProcessorTestingCode(
`{a|*b*}`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 ruby[1]
│ data: {"hName":"ruby","rubyText":"*b*"}
└─0 text "a"
`,
`<p><ruby>a<rt>*b*</rt></ruby></p>`,
),
);

it(
'nested ruby',
buildProcessorTestingCode(
`{{a|b}|c}`,
stripIndent`
root[1]
└─0 paragraph[2]
├─0 ruby[1]
│ │ data: {"hName":"ruby","rubyText":"b"}
│ └─0 text "{a"
└─1 text "|c}"
`,
`<p><ruby>{a<rt>b</rt></ruby>|c}</p>`,
),
);

it(
'ruby with newline',
buildProcessorTestingCode(
`{a\nb|c}`,
stripIndent`
root[1]
└─0 paragraph[3]
├─0 text "{a"
├─1 break
└─2 text "b|c}"
`,
`<p>{a<br>\nb|c}</p>`,
),
);
34 changes: 34 additions & 0 deletions tests/remark2rehype/figure.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { stripIndent } from 'common-tags';
import { buildProcessorTestingCode } from '../utils';

it(
'simple figure output',
buildProcessorTestingCode(
`![caption](./img.png)`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 image
title: null
url: "./img.png"
alt: "caption"
`,
`<p><figure><img src="./img.png" alt="caption"><figcaption>caption</figcaption></figure></p>`,
),
);

it(
'image without caption',
buildProcessorTestingCode(
`![](./img.png)`,
stripIndent`
root[1]
└─0 paragraph[1]
└─0 image
title: null
url: "./img.png"
alt: null
`,
`<p><img src="./img.png"></p>`,
),
);
12 changes: 12 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unistInspect from 'unist-util-inspect';
import { VFM } from '../src';

export const buildProcessorTestingCode = (
input: string,
expectedMdast: string,
expectedHtml: string,
) => (): any => {
const vfm = VFM({ partial: true }).freeze();
expect(unistInspect.noColor(vfm.parse(input))).toBe(expectedMdast.trim());
expect(String(vfm.processSync(input))).toBe(expectedHtml);
};
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,11 @@
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==

"@types/common-tags@^1.8.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@types/common-tags/-/common-tags-1.8.0.tgz#79d55e748d730b997be5b7fce4b74488d8b26a6b"
integrity sha512-htRqZr5qn8EzMelhX/Xmx142z218lLyGaeZ3YR8jlze4TATRU9huKKvuBmAJEW4LCC4pnY1N6JAm6p85fMHjhg==

"@types/debug@^4.1.5":
version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
Expand Down Expand Up @@ -1675,6 +1680,11 @@ commander@^5.1.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-5.1.0.tgz#46abbd1652f8e059bddaef99bbdcb2ad9cf179ae"
integrity sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==

common-tags@^1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/common-tags/-/common-tags-1.8.0.tgz#8e3153e542d4a39e9b10554434afaaf98956a937"
integrity sha512-6P6g0uetGpW/sdyUy/iQQCbFF0kWVMSIVSyYz7Zgjcgh8mgw8PQzDNZeyZ5DQ2gM7LBoZPHmnjz8rUthkBG5tw==

compare-func@^1.3.1:
version "1.3.4"
resolved "https://registry.yarnpkg.com/compare-func/-/compare-func-1.3.4.tgz#6b07c4c5e8341119baf44578085bda0f4a823516"
Expand Down

0 comments on commit 34aa884

Please sign in to comment.