-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathCustomViews.test.ts
140 lines (127 loc) · 4.92 KB
/
CustomViews.test.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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as assert from "assert";
import path = require("path");
import rewire = require("rewire");
import vscode = require("vscode");
// Setup types that are not exported.
const customViews = rewire("../../src/features/CustomViews");
const htmlContentViewClass = customViews.__get__("HtmlContentView");
const HtmlContentView: typeof htmlContentViewClass = htmlContentViewClass;
// interfaces for tests
interface ITestFile {
fileName: string;
content: string;
}
interface IHtmlContentViewTestCase {
name: string;
htmlContent: string;
javaScriptFiles: ITestFile[];
cssFiles: ITestFile[];
expectedHtmlString: string;
}
function convertToVSCodeResourceScheme(filePath: string): string {
return vscode.Uri.file(filePath).toString().replace("file://", "vscode-resource://");
}
describe("CustomViews feature", function () {
const testCases: IHtmlContentViewTestCase[] = [
{
name: "with no JavaScript or CSS",
htmlContent: "hello",
javaScriptFiles: [],
cssFiles: [],
expectedHtmlString: `<html><head></head><body>
hello
</body></html>`,
},
// A test that adds a js file.
{
name: "with a JavaScript file but no CSS",
htmlContent: "hello",
javaScriptFiles: [
{
fileName: "testCustomViews.js",
content: "console.log('asdf');",
},
],
cssFiles: [],
expectedHtmlString: `<html><head></head><body>
hello
<script src="${convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.js"))}"></script>
</body></html>`,
},
// A test that adds a js file in the current directory, and the parent directory.
{
name: "with two JavaScript files in different locations, but no CSS",
htmlContent: "hello",
javaScriptFiles: [
{
fileName: "testCustomViews.js",
content: "console.log('asdf');",
},
{
fileName: "../testCustomViews.js",
content: "console.log('asdf');",
},
],
cssFiles: [],
expectedHtmlString: `<html><head></head><body>
hello
<script src="${convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.js"))}"></script>
<script src="${convertToVSCodeResourceScheme(path.join(__dirname, "../testCustomViews.js"))}"></script>
</body></html>`,
},
// A test that adds a js file and a css file.
{
name: "with a JavaScript and a CSS file",
htmlContent: "hello",
javaScriptFiles: [
{
fileName: "testCustomViews.js",
content: "console.log('asdf');",
},
],
cssFiles: [
{
fileName: "testCustomViews.css",
content: "body: { background-color: green; }",
},
],
expectedHtmlString: `<html><head><link rel="stylesheet" href="${convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.css"))}">
</head><body>
hello
<script src="${convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.js"))}"></script>
</body></html>`,
},
];
for (const testCase of testCases) {
it(`Correctly creates an HtmlContentView ${testCase.name}`, async function () {
const htmlContentView = new HtmlContentView();
const jsPaths = await Promise.all(testCase.javaScriptFiles.map(async (jsFile) => {
const jsPath: vscode.Uri = vscode.Uri.file(path.join(__dirname, jsFile.fileName));
await vscode.workspace.fs.writeFile(jsPath, Buffer.from(jsFile.content));
return jsPath.toString();
}));
const cssPaths = await Promise.all(testCase.cssFiles.map(async (cssFile) => {
const cssPath: vscode.Uri = vscode.Uri.file(path.join(__dirname, cssFile.fileName));
await vscode.workspace.fs.writeFile(cssPath, Buffer.from(cssFile.content));
return cssPath.toString();
}));
htmlContentView.htmlContent = {
bodyContent: testCase.htmlContent,
javaScriptPaths: jsPaths,
styleSheetPaths: cssPaths,
};
try {
assert.strictEqual(htmlContentView.getContent(), testCase.expectedHtmlString);
} finally {
for (const jsPath of jsPaths) {
await vscode.workspace.fs.delete(vscode.Uri.parse(jsPath));
}
for (const cssPath of cssPaths) {
await vscode.workspace.fs.delete(vscode.Uri.parse(cssPath));
}
}
});
}
});