UNPKG

statigen

Version:

A static site generator that supports html, ejs, and markdown source files

178 lines 7.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const fsExtra = require("fs-extra"); const sinon_1 = require("sinon"); const diagnosticUtils_1 = require("./diagnosticUtils"); const util_1 = require("./util"); const testHelpers_spec_1 = require("./testHelpers.spec"); describe('StaticSiteGenerator', () => { it('copies static files', async () => { (0, testHelpers_spec_1.writeFiles)({ 'app.css': '/*css*/', 'scripts/script.js': '//js' }); await (0, testHelpers_spec_1.run)(); expectFileToEqual(`${testHelpers_spec_1.outDir}/app.css`, '/*css*/'); expectFileToEqual(`${testHelpers_spec_1.outDir}/scripts/script.js`, '//js'); }); it('compiles ejs in ejs files', async () => { (0, testHelpers_spec_1.writeFiles)({ 'index.ejs': `<title><%="Page title"%></title>` }); await (0, testHelpers_spec_1.run)(); expectFileToEqual(`${testHelpers_spec_1.outDir}/index.html`, `<title>Page title</title>`); }); it('reads folder name from children files', async () => { (0, testHelpers_spec_1.writeFiles)({ 'some-folder/index.ejs': (0, testHelpers_spec_1.trim) ` --- template: customTemplate.ejs parentTitle: Overridden Title --- <title>Page title</title> ` }); const generator = await (0, testHelpers_spec_1.run)(); const tree = generator.project.getTree(); (0, chai_1.expect)(tree.children[0].title).to.eql('Overridden Title'); }); it('supports html as template', async () => { (0, testHelpers_spec_1.writeFiles)({ 'file.md': `# header`, '_template.html': `<title><!--content--></title>` }); await (0, testHelpers_spec_1.run)(); expectFileToEqual(`${testHelpers_spec_1.outDir}/file.html`, `<title><a href="#header"><h1 id="header">header</h1></a></title>`); }); it('does not render files starting with underscore', async () => { (0, testHelpers_spec_1.writeFiles)({ '_file1.html': '', '_file2.ejs': '', '_file3.css': '' }); await (0, testHelpers_spec_1.run)(); (0, chai_1.expect)(fsExtra.pathExistsSync(`${testHelpers_spec_1.outDir}/_file1.html`)).to.be.false; (0, chai_1.expect)(fsExtra.pathExistsSync(`${testHelpers_spec_1.outDir}/_file2.ejs`)).to.be.false; (0, chai_1.expect)(fsExtra.pathExistsSync(`${testHelpers_spec_1.outDir}/_file2.html`)).to.be.false; (0, chai_1.expect)(fsExtra.pathExistsSync(`${testHelpers_spec_1.outDir}/_file3.css`)).to.be.false; }); it('supports html file with frontmatter', async () => { (0, testHelpers_spec_1.writeFiles)({ 'file.html': (0, testHelpers_spec_1.trim) ` --- template: customTemplate.ejs title: File!!! --- <i>Hello world</i> `, '_template.html': `<title><!--content--></title>`, 'customTemplate.ejs': `custom<%-content%>template` }); await (0, testHelpers_spec_1.run)(); expectFileToEqual(`${testHelpers_spec_1.outDir}/file.html`, `custom<i>Hello world</i>template`); }); it('compiles simple markdown file', async () => { (0, testHelpers_spec_1.writeFiles)({ 'about.md': '# Hello world' }); await (0, testHelpers_spec_1.run)(); expectFileToEqual(`${testHelpers_spec_1.outDir}/about.html`, '<a href="#hello-world"><h1 id="hello-world">Hello world</h1></a>'); }); it('transpiles markdown with default template', async () => { (0, testHelpers_spec_1.writeFiles)({ 'about.md': '*italic*', '_template.ejs': ` <div id="content"> <%-content%> </div> ` }); await (0, testHelpers_spec_1.run)(); expectFileToEqual(`${testHelpers_spec_1.outDir}/about.html`, ` <div id="content"> <p><em>italic</em></p> </div> `); }); it('passes custom frontmatter props to template', async () => { (0, testHelpers_spec_1.writeFiles)({ 'about.md': (0, testHelpers_spec_1.trim) ` --- title: Hello title --- *italic* `, '_template.ejs': ` <title><%=attributes.title%></title> ` }); await (0, testHelpers_spec_1.run)(); expectFileToEqual(`${testHelpers_spec_1.outDir}/about.html`, ` <title>Hello title</title> `); }); it('does not crash on ejs syntax errors', async () => { (0, testHelpers_spec_1.writeFiles)({ 'test.ejs': (0, testHelpers_spec_1.trim) `<%=hello world%>` }); const generator = await (0, testHelpers_spec_1.run)(); (0, chai_1.expect)(generator.project.getDiagnostics().map(x => x.message)).to.eql([ 'Unexpected token' ]); }); it('parses ejs errors not handled by ejs-lint', async () => { (0, testHelpers_spec_1.writeFiles)({ 'test.ejs': (0, testHelpers_spec_1.trim) ` <html> <%=hello%> ` }); const generator = await (0, testHelpers_spec_1.run)(); const diagnostics = generator.project.getDiagnostics(); (0, chai_1.expect)(diagnostics.map(x => x.message)).to.eql([ 'hello is not defined' ]); (0, chai_1.expect)(diagnostics.map(x => x.range)).to.eql([ (0, util_1.createRange)(1, 0, 1, 0) ]); }); it('computes tree title from markdown header', async () => { (0, testHelpers_spec_1.writeFiles)({ 'SomeFolder/SomeFile.md': (0, testHelpers_spec_1.trim) ` # MyHeader some content ` }); const generator = await (0, testHelpers_spec_1.run)(); (0, chai_1.expect)(generator.project.getTree().children[0].children[0].title).to.eql('MyHeader'); }); it('calculates outDir from absolute path', async () => { const generator = await (0, testHelpers_spec_1.run)(); const filePath = (0, util_1.s) `${testHelpers_spec_1.sourceDir}/sub1\\sub2/index.md`; fsExtra.outputFileSync(filePath, '#stuff'); const file = generator.project.setFile(filePath); (0, chai_1.expect)((0, util_1.s) `${file.outPath}`).to.eql((0, util_1.s) `${testHelpers_spec_1.outDir}/sub1/sub2/index.html`); }); it.skip('temp test', async () => { testHelpers_spec_1.options.cwd = 'C:/projects/roku/vscode-brightscript-language'; testHelpers_spec_1.options.outDir = 'dist-docs'; testHelpers_spec_1.options.sourceDir = 'docs'; testHelpers_spec_1.options.files = ['**/*']; testHelpers_spec_1.options.watch = true; const generator = await (0, testHelpers_spec_1.run)(); (0, diagnosticUtils_1.printDiagnostics)(generator.project.getDiagnostics()); }); }); function expectFileToEqual(filePath, expectedText, trim = true) { if (!fsExtra.pathExistsSync(filePath)) { sinon_1.assert.fail(`Expected file to exist at "${filePath}"`); } let actualText = fsExtra.readFileSync(filePath).toString(); if (trim) { actualText = (0, testHelpers_spec_1.trimLeading)(actualText); expectedText = (0, testHelpers_spec_1.trimLeading)(expectedText); } (0, chai_1.expect)(actualText).to.eql(expectedText); } //# sourceMappingURL=StaticSiteGenerator.spec.js.map