-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathCustomViews.test.ts
139 lines (126 loc) · 4.73 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as assert from "assert";
import fs = require("fs");
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://");
}
suite("CustomViews tests", () => {
const testCases: IHtmlContentViewTestCase[] = [
// Basic test that has no js or css.
{
name: "Basic",
htmlContent: "hello",
javaScriptFiles: [],
cssFiles: [],
expectedHtmlString: `<html><head></head><body>
hello
</body></html>`,
},
// A test that adds a js file.
{
name: "With JavaScript file",
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 2 JavaScript files in two different locations",
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 JavaScript and 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) {
test(`Can create an HtmlContentView and get its content - ${testCase.name}`, () => {
const htmlContentView = new HtmlContentView();
const jsPaths = testCase.javaScriptFiles.map((jsFile) => {
const jsPath: string = path.join(__dirname, jsFile.fileName);
fs.writeFileSync(jsPath, jsFile.content);
return vscode.Uri.file(jsPath).toString();
});
const cssPaths = testCase.cssFiles.map((cssFile) => {
const cssPath: string = path.join(__dirname, cssFile.fileName);
fs.writeFileSync(cssPath, cssFile.content);
return vscode.Uri.file(cssPath).toString();
});
htmlContentView.htmlContent = {
bodyContent: testCase.htmlContent,
javaScriptPaths: jsPaths,
styleSheetPaths: cssPaths,
};
try {
assert.equal(htmlContentView.getContent(), testCase.expectedHtmlString);
} finally {
jsPaths.forEach((jsPath) => fs.unlinkSync(vscode.Uri.parse(jsPath).fsPath));
cssPaths.forEach((cssPath) => fs.unlinkSync(vscode.Uri.parse(cssPath).fsPath));
}
});
}
});