-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdocument_end.spec.ts
59 lines (58 loc) · 1.5 KB
/
document_end.spec.ts
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
import test from "ava";
import { DocumentEnd } from "..";
import { HTMLRewriter, wait } from ".";
test("handles document end specific mutations", async (t) => {
// append
const res = await new HTMLRewriter()
.onDocument({
end(end) {
end.append("<span>append</span>");
end.append("<span>append html</span>", { html: true });
},
})
.transform("<p>test</p>");
t.is(
res,
[
"<p>",
"test",
"</p>",
"<span>append</span>",
"<span>append html</span>",
].join("")
);
});
test("document end allows chaining", async (t) => {
t.plan(1);
await new HTMLRewriter()
.onDocument({
end(end) {
t.is(end.append(""), end);
},
})
.transform("<p>test</p>");
});
test("handles document end async handler", async (t) => {
const res = await new HTMLRewriter()
.onDocument({
async end(end) {
await wait(50);
end.append("<span>append html</span>", { html: true });
},
})
.transform("<p>test</p>");
t.is(res, "<p>test</p><span>append html</span>");
});
test("handles document end class handler", async (t) => {
class Handler {
constructor(private content: string) {}
// noinspection JSUnusedGlobalSymbols
end(end: DocumentEnd) {
end.append(this.content, { html: true });
}
}
const res = await new HTMLRewriter()
.onDocument(new Handler("<span>append html</span>"))
.transform("<p>test</p>");
t.is(res, "<p>test</p><span>append html</span>");
});