-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathembed.test.js
98 lines (83 loc) · 2.07 KB
/
embed.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
import * as prettier from "prettier";
import plugin from "../src/plugin.js";
function format(content, opts = {}) {
const { plugins = [] } = opts;
return prettier.format(content, {
...opts,
parser: "xml",
plugins: [...plugins, plugin]
});
}
test("embeds properly when the name of the tag matches", async () => {
const formatted = await format("<javascript>1+1;</javascript>");
const expected = `<javascript>
1 + 1;
</javascript>
`;
expect(formatted).toEqual(expected);
});
test("embeds properly when the type of the style tag matches", async () => {
const formatted = await format(
'<style type="text/css">body{color:red;}</style>'
);
const expected = `<style type="text/css">
body {
color: red;
}
</style>
`;
expect(formatted).toEqual(expected);
});
test("embeds properly when the type of the script tag matches", async () => {
const formatted = await format(
'<script type="text/javascript">1+1;</script>'
);
const expected = `<script type="text/javascript">
1 + 1;
</script>
`;
expect(formatted).toEqual(expected);
});
const customScriptPlugin = {
parsers: {
customScript: {
astFormat: "customScript",
parse(text) {
return { type: "root", text };
},
locStart() {
return -1;
},
locEnd() {
return -1;
}
}
},
printers: {
customScript: {
print(path) {
const { hardline } = prettier.doc.builders;
return ["customScript!!!", hardline, hardline, path.getValue().text];
}
}
}
};
test("embeds properly when the type of the script tag matches a custom parser", async () => {
const formatted = await format(
'<script type="text/customScript">1+1;</script>',
{
plugins: [customScriptPlugin]
}
);
const expected = `<script type="text/customScript">
customScript!!!
1+1;
</script>
`;
expect(formatted).toEqual(expected);
});
test("does not embed when self-closing", async () => {
const expected = `<script type="text/javascript" />\n`;
const formatted = await format(expected);
expect(formatted).toEqual(expected);
});