Created
March 24, 2022 14:54
-
-
Save minademian/6e610ec8f3f45b6a27757fdd29958249 to your computer and use it in GitHub Desktop.
TDD #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const htmlCreator = require("html-creator"); | |
const fs = require("fs/promises"); | |
async function main(args) { | |
const distPath = args[0]; | |
const stylesList = await loadAssets(".css", distPath); | |
const scriptsList = await loadAssets(".js", distPath); | |
const styles = generateAssetsList(stylesList); | |
const scripts = generateAssetsList(scriptsList); | |
const result = generateHTML(styles, scripts); | |
await saveOutput(result); | |
} | |
function generateAssetsList(src) { | |
const DIST_PATH = "/etjanst/samtycke-funktionsnedsattning/"; | |
return src | |
.filter((item) => item.type !== "title") | |
.map((script) => { | |
return { | |
type: "script", | |
content: `document.createStyleSheet(\"${DIST_PATH}${script}\");`, | |
}; | |
}); | |
} | |
async function loadAssets(filepattern, distPath) { | |
let files; | |
try { | |
files = await fs.readdir(distPath); | |
} catch (err) { | |
throw err; | |
} | |
return files.filter((file) => file.includes(filepattern)); | |
} | |
function generateHTML(styles, scripts) { | |
return new htmlCreator([ | |
{ | |
type: "head", | |
content: styles.concat([{ type: "title", content: "" }]), | |
}, | |
{ | |
type: "body", | |
attributes: { class: "gsk-body" }, | |
content: [ | |
{ | |
type: "app-root", | |
content: "Loading...", | |
}, | |
].concat(scripts), | |
}, | |
]); | |
} | |
async function saveOutput(data) { | |
const OUTPUT_FILE = 'deploy.html'; | |
const deployOutput = data.renderHTML(); | |
fs.writeFile(OUTPUT_FILE, deployOutput); | |
console.log("Deploy.file for SiteVision generated successfully\n"); | |
console.log("Output saved:\n"); | |
showOutput(deployOutput); | |
} | |
function showOutput(result) { | |
console.log(result); | |
} | |
module.exports = main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const htmlCreator = require("html-creator"); | |
import { main } from "./generate-deploy-file"; | |
describe("GenerateDeployFile", () => { | |
let styles, scripts, result; | |
let fs; | |
beforeEach(() => { | |
styles = [ | |
{ type: "script", content: "style1.css" }, | |
{ type: "script", content: "style2.css" }, | |
{ type: "script", content: "style3.css" }, | |
]; | |
scripts = [ | |
{ type: "script", content: "script1.js" }, | |
{ type: "script", content: "script2.js" }, | |
{ type: "script", content: "script3.js" }, | |
]; | |
result = new htmlCreator([ | |
{ | |
type: "head", | |
content: [ | |
{ | |
type: "script", | |
content: `document.createStyleSheet(\"/etjanst/samtycke-funktionsnedsattning/style1.css\");`, | |
}, | |
{ | |
type: "script", | |
content: `document.createStyleSheet(\"/etjanst/samtycke-funktionsnedsattning/style2.css\");`, | |
}, | |
{ | |
type: "script", | |
content: `document.createStyleSheet(\"/etjanst/samtycke-funktionsnedsattning/style3.css\");`, | |
}, | |
{ type: "title", content: "" }, | |
], | |
}, | |
{ | |
type: "body", | |
attributes: { class: "gsk-body" }, | |
content: [ | |
{ | |
type: "app-root", | |
content: "Loading...", | |
}, | |
{ | |
type: "script", | |
content: `document.createStyleSheet(\"/etjanst/samtycke-funktionsnedsattning/script1.js\");`, | |
}, | |
{ | |
type: "script", | |
content: `document.createStyleSheet(\"/etjanst/samtycke-funktionsnedsattning/script2.js\");`, | |
}, | |
{ | |
type: "script", | |
content: `document.createStyleSheet(\"/etjanst/samtycke-funktionsnedsattning/script3.js\");`, | |
}, | |
], | |
}, | |
]); | |
}); | |
it("ASSERT main when passed non-arrays FAILS", () => { | |
expect(() => { | |
main({}, {}); | |
}).toThrow(Error("src list must be an array")); | |
}); | |
it("ASSERT main renders list of stylesheets in head tag SUCCESSFULLY", () => { | |
expect(main(styles, scripts)).toEqual(result); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment