-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.js
124 lines (115 loc) · 2.53 KB
/
index.test.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const remark = require('remark')
const html = require('remark-html')
const cheerio = require('cheerio')
const plugin = require('.')
function getHTML(md, opt) {
const h = remark()
.use(plugin, opt)
.use(html)
.processSync(md).contents
return cheerio.load(h)
}
function testContainer(cheerioIns, { type, title, content }) {
expect(cheerioIns.hasClass(type.toLowerCase())).toBeTruthy()
expect(
cheerioIns
.find('p')
.eq(0)
.text(),
).toEqual(title)
let c = content
if (!Array.isArray(content)) {
c = [content]
}
c.forEach((each, idx) => {
expect(
cheerioIns
.find('p')
.eq(idx + 1)
.text(),
).toEqual(each)
})
}
describe('test generate container', () => {
it('test normal render', () => {
const str = `::: tip
this is content
:::
`
const $ = getHTML(str)
const container = $('.remark-container')
expect(container).toHaveLength(1)
expect(container.hasClass('tip')).toBeTruthy()
testContainer(container, {
type: 'tip',
title: 'tip',
content: 'this is content',
})
})
it('test multi render', () => {
const str = `
this is a paragraph
::: Warning
this is a warning content
:::
## heading
::: Error
this is a error content
:::
`
const $ = getHTML(str)
const container = $('.remark-container')
expect(container).toHaveLength(2)
const first = container.eq(0)
const sec = container.eq(1)
testContainer(first, {
type: 'warning',
title: 'Warning',
content: 'this is a warning content',
})
testContainer(sec, {
type: 'error',
title: 'Error',
content: 'this is a error content',
})
})
it('test multi content', () => {
const str = `
::: fdsa custom title
this is line1
this is line2
:::
`
const $ = getHTML(str)
const container = $('.remark-container')
testContainer(container, {
title: 'custom title',
type: 'fdsa',
content: ['this is line1', 'this is line2'],
})
})
it('test custom className', () => {
const str = `
::: fdsa custom title
this is line1
this is line2
:::
`
const className = 'fffff'
const $ = getHTML(str, {
className,
})
const container = $(`.${className}`)
expect(container).toHaveLength(1)
})
})
it('test multi-line link', () => {
const str = `
[Link on
two lines](https://example.com)
`
const $ = getHTML(str)
const container = $('a')
expect(container).toHaveLength(1)
expect(container.text().replace(/[\r\n]/, ' ')).toEqual('Link on two lines')
})