|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import * as assert from "assert"; |
| 6 | +import fs = require("fs"); |
| 7 | +import path = require("path"); |
| 8 | +import rewire = require("rewire"); |
| 9 | +import vscode = require("vscode"); |
| 10 | + |
| 11 | +// Setup types that are not exported. |
| 12 | +const customViews = rewire("../../src/features/CustomViews"); |
| 13 | +const htmlContentViewClass = customViews.__get__("HtmlContentView"); |
| 14 | +const HtmlContentView: typeof htmlContentViewClass = htmlContentViewClass; |
| 15 | + |
| 16 | +// interfaces for tests |
| 17 | +interface ITestFile { |
| 18 | + fileName: string; |
| 19 | + content: string; |
| 20 | +} |
| 21 | + |
| 22 | +interface IHtmlContentViewTestCase { |
| 23 | + name: string; |
| 24 | + htmlContent: string; |
| 25 | + javaScriptFiles: ITestFile[]; |
| 26 | + cssFiles: ITestFile[]; |
| 27 | + expectedHtmlString: string; |
| 28 | +} |
| 29 | + |
| 30 | +function convertToVSCodeResourceScheme(filePath: string): string { |
| 31 | + return vscode.Uri.file(filePath).toString().replace("file://", "vscode-resource://"); |
| 32 | +} |
| 33 | + |
| 34 | +suite("CustomViews tests", () => { |
| 35 | + const testCases: IHtmlContentViewTestCase[] = [ |
| 36 | + // Basic test that has no js or css. |
| 37 | + { |
| 38 | + name: "Basic", |
| 39 | + htmlContent: "hello", |
| 40 | + javaScriptFiles: [], |
| 41 | + cssFiles: [], |
| 42 | + expectedHtmlString: `<html><head></head><body> |
| 43 | +hello |
| 44 | +</body></html>`, |
| 45 | + }, |
| 46 | + |
| 47 | + // A test that adds a js file. |
| 48 | + { |
| 49 | + name: "With JavaScript file", |
| 50 | + htmlContent: "hello", |
| 51 | + javaScriptFiles: [ |
| 52 | + { |
| 53 | + fileName: "testCustomViews.js", |
| 54 | + content: "console.log('asdf');", |
| 55 | + }, |
| 56 | + ], |
| 57 | + cssFiles: [], |
| 58 | + expectedHtmlString: `<html><head></head><body> |
| 59 | +hello |
| 60 | +<script src="${convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.js"))}"></script> |
| 61 | +</body></html>`, |
| 62 | + }, |
| 63 | + |
| 64 | + // A test that adds a js file in the current directory, and the parent directory. |
| 65 | + { |
| 66 | + name: "With 2 JavaScript files in two different locations", |
| 67 | + htmlContent: "hello", |
| 68 | + javaScriptFiles: [ |
| 69 | + { |
| 70 | + fileName: "testCustomViews.js", |
| 71 | + content: "console.log('asdf');", |
| 72 | + }, |
| 73 | + { |
| 74 | + fileName: "../testCustomViews.js", |
| 75 | + content: "console.log('asdf');", |
| 76 | + }, |
| 77 | + ], |
| 78 | + cssFiles: [], |
| 79 | + expectedHtmlString: `<html><head></head><body> |
| 80 | +hello |
| 81 | +<script src="${convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.js"))}"></script> |
| 82 | +<script src="${convertToVSCodeResourceScheme(path.join(__dirname, "../testCustomViews.js"))}"></script> |
| 83 | +</body></html>`, |
| 84 | + }, |
| 85 | + |
| 86 | + // A test that adds a js file and a css file. |
| 87 | + { |
| 88 | + name: "With JavaScript and CSS file", |
| 89 | + htmlContent: "hello", |
| 90 | + javaScriptFiles: [ |
| 91 | + { |
| 92 | + fileName: "testCustomViews.js", |
| 93 | + content: "console.log('asdf');", |
| 94 | + }, |
| 95 | + ], |
| 96 | + cssFiles: [ |
| 97 | + { |
| 98 | + fileName: "testCustomViews.css", |
| 99 | + content: "body: { background-color: green; }", |
| 100 | + }, |
| 101 | + ], |
| 102 | + expectedHtmlString: `<html><head><link rel="stylesheet" href="${ |
| 103 | + convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.css"))}"> |
| 104 | +</head><body> |
| 105 | +hello |
| 106 | +<script src="${convertToVSCodeResourceScheme(path.join(__dirname, "testCustomViews.js"))}"></script> |
| 107 | +</body></html>`, |
| 108 | + }, |
| 109 | + ]; |
| 110 | + |
| 111 | + for (const testCase of testCases) { |
| 112 | + test(`Can create an HtmlContentView and get its content - ${testCase.name}`, () => { |
| 113 | + const htmlContentView = new HtmlContentView(); |
| 114 | + |
| 115 | + const jsPaths = testCase.javaScriptFiles.map((jsFile) => { |
| 116 | + const jsPath: string = path.join(__dirname, jsFile.fileName); |
| 117 | + fs.writeFileSync(jsPath, jsFile.content); |
| 118 | + return vscode.Uri.file(jsPath).toString(); |
| 119 | + }); |
| 120 | + |
| 121 | + const cssPaths = testCase.cssFiles.map((cssFile) => { |
| 122 | + const cssPath: string = path.join(__dirname, cssFile.fileName); |
| 123 | + fs.writeFileSync(cssPath, cssFile.content); |
| 124 | + return vscode.Uri.file(cssPath).toString(); |
| 125 | + }); |
| 126 | + |
| 127 | + htmlContentView.htmlContent = { |
| 128 | + bodyContent: testCase.htmlContent, |
| 129 | + javaScriptPaths: jsPaths, |
| 130 | + styleSheetPaths: cssPaths, |
| 131 | + }; |
| 132 | + try { |
| 133 | + assert.equal(htmlContentView.getContent(), testCase.expectedHtmlString); |
| 134 | + } finally { |
| 135 | + jsPaths.forEach((jsPath) => fs.unlinkSync(vscode.Uri.parse(jsPath).fsPath)); |
| 136 | + cssPaths.forEach((cssPath) => fs.unlinkSync(vscode.Uri.parse(cssPath).fsPath)); |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | +}); |
0 commit comments