-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
141 lines (133 loc) · 3.71 KB
/
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const unified = require("unified");
const rehypeParse = require("rehype-parse");
const rehypeStringify = require("rehype-stringify");
const h = require("hastscript");
const rehypeInsert = require(".");
function processDocument(input, optionsForRehypeInsert) {
return unified()
.use(rehypeParse, { fragment: true })
.use(rehypeInsert, optionsForRehypeInsert)
.use(rehypeStringify)
.process(input);
}
test("prepend to an element", async () => {
const input =
`<div id="alpha">` +
`<p class="a">alpha</p>` +
`<p class="b">bravo</p>` +
`</div>`;
const options = {
insertions: [
{ selector: "#alpha", insert: h("p", "zamboni"), action: "prepend" },
],
};
const expected =
`<div id="alpha">` +
`<p>zamboni</p>` +
`<p class="a">alpha</p>` +
`<p class="b">bravo</p>` +
`</div>`;
const output = await processDocument(input, options);
expect(output.contents).toBe(expected);
});
test("append to an element", async () => {
const input =
`<div id="alpha">` +
`<p class="a">alpha</p>` +
`<p class="b">bravo</p>` +
`</div>`;
const options = {
insertions: [
{ selector: "#alpha", insert: h("p", "zamboni"), action: "append" },
],
};
const expected =
`<div id="alpha">` +
`<p class="a">alpha</p>` +
`<p class="b">bravo</p>` +
`<p>zamboni</p>` +
`</div>`;
const output = await processDocument(input, options);
expect(output.contents).toBe(expected);
});
test("replace contents of an element", async () => {
const input =
`<div id="alpha">` +
`<p class="a">alpha</p>` +
`<p class="b">bravo</p>` +
`</div>`;
const options = {
insertions: [{ selector: "#alpha", insert: h("p", "zamboni") }],
};
const output = await processDocument(input, options);
const expected = `<div id="alpha">` + `<p>zamboni</p>` + `</div>`;
expect(output.contents).toBe(expected);
});
test("replace multiple texts", async () => {
const input =
`<div id="alpha">` +
`<p class="a">alpha</p>` +
`<p class="b">bravo</p>` +
`</div>`;
const options = {
insertions: [
{ selector: "#alpha .a", insert: "apple" },
{ selector: "#alpha .b", insert: "banana" },
],
};
const output = await processDocument(input, options);
const expected =
`<div id="alpha">` +
`<p class="a">apple</p>` +
`<p class="b">banana</p>` +
`</div>`;
expect(output.contents).toBe(expected);
});
test("replace with multiple nodes", async () => {
const input =
`<div id="alpha">` +
`<p class="a">alpha</p>` +
`<p class="b">bravo</p>` +
`</div>`;
const options = {
insertions: [
{ selector: "#alpha", insert: [h("p.c", "charlie"), h("p.d", "delta")] },
],
};
const output = await processDocument(input, options);
const expected =
`<div id="alpha">` +
`<p class="c">charlie</p>` +
`<p class="d">delta</p>` +
`</div>`;
expect(output.contents).toBe(expected);
});
test("readme example should work", async () => {
const input =
`<article>` +
`<h1 id="title">Untitled</h1>` +
`<section class="content">` +
`<p>It was the best of times...</p>` +
`</section>` +
`</article>`;
const options = {
insertions: [
{ selector: "#title", insert: "A Tale of Two Cities" },
{
selector: ".content",
insert: h("p", "It was the worst of times."),
action: "append",
},
],
};
const output = await processDocument(input, options);
const expected =
`<article>` +
`<h1 id="title">A Tale of Two Cities</h1>` +
`<section class="content">` +
`<p>It was the best of times...</p>` +
`<p>It was the worst of times.</p>` +
`</section>` +
`</article>`;
expect(output.contents).toBe(expected);
});