forked from ProjectEvergreen/greenwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.default.workspace-frontmatter-imports.spec.js
More file actions
235 lines (192 loc) · 8.8 KB
/
Copy pathbuild.default.workspace-frontmatter-imports.spec.js
File metadata and controls
235 lines (192 loc) · 8.8 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Use Case
* Run Greenwood build command with a workspace that uses frontmatter imports, including custom formats.
* Added prerender: true to showcase prerendering WCs in markdown
*
* User Result
* Should generate a bare bones Greenwood build.
*
* User Command
* greenwood build
*
* User Config
* export default {
* prerender: true,
* plugins: [{
* // naive TS and Sass plugins
* }]
* }
*
* User Workspace
* src/
* components/
* counter/
* counter.js
* counter.css
* multi-hyphen/
* multi-hyphen.js
* scripts/
* frontmatter-standard.js
* frontmatter-typescript.ts
* frontmatter-custom.foo
* styles/
* frontmatter-custom.scss
* pages/
* examples/
* demo.html
* index.html
*/
import chai from "chai";
import fs from "node:fs";
import glob from "glob-promise";
import { JSDOM } from "jsdom";
import path from "node:path";
import { getOutputTeardownFiles, HASH_REGEX } from "../../../../../test/utils.js";
import { runSmokeTest } from "../../../../../test/smoke-test.js";
import { Runner } from "gallinago";
import { fileURLToPath } from "node:url";
const expect = chai.expect;
describe("Build Greenwood With: ", function () {
const LABEL = "Default Greenwood Configuration and Workspace with Frontmatter Imports";
const cliPath = path.join(process.cwd(), "packages/cli/src/bin.js");
const outputPath = fileURLToPath(new URL(".", import.meta.url));
let runner;
before(function () {
this.context = {
publicDir: path.join(outputPath, "public"),
};
runner = new Runner();
});
describe(LABEL, function () {
before(async function () {
await runner.setup(outputPath);
await runner.runCommand(cliPath, "build");
});
runSmokeTest(["public", "index"], LABEL);
describe("Content and file output for the Demo page", function () {
let dom;
let html;
before(async function () {
const htmlPath = path.resolve(this.context.publicDir, "examples/demo", "index.html");
dom = await JSDOM.fromFile(path.resolve(htmlPath));
html = await fs.promises.readFile(htmlPath, "utf-8");
});
it("should output the expected number of JS files from frontmatter imports", async function () {
const jsFiles = await glob.promise(`${this.context.publicDir}**/**/*.js`);
expect(jsFiles).to.have.lengthOf(5);
});
it("should output an unoptimized counter.css file from frontmatter import", async function () {
const cssFiles = await glob.promise(`${this.context.publicDir}**/**/counter.*.css`);
expect(cssFiles).to.have.lengthOf(1);
});
it("should output a counter.js file from frontmatter import", async function () {
const jsFiles = await glob.promise(`${this.context.publicDir}**/**/counter.*.js`);
expect(jsFiles).to.have.lengthOf(1);
});
it("should output a multi-hyphen.js file from frontmatter import", async function () {
const jsFiles = await glob.promise(`${this.context.publicDir}**/**/multi-hyphen.*.js`);
expect(jsFiles).to.have.lengthOf(1);
});
it("should a page heading", function () {
const heading = dom.window.document.querySelectorAll("body h2");
expect(heading.length).to.be.equal(1);
expect(heading[0].textContent).to.be.equal("Demo Page Example");
});
describe("Counter <x-counter> component from front matter that is prerendered", () => {
it("should output a custom <x-counter> tag that", function () {
const counter = dom.window.document.querySelectorAll("body x-counter");
expect(counter.length).to.be.equal(1);
});
it("should output a custom <x-counter> tag that is _not_ wrapped in a <p> tag", function () {
expect(/<p><x-counter>/.test(html)).to.be.false;
expect(/<\/x-counter><\/p>/.test(html)).to.be.false;
});
it("should output a heading tag from the custom element", function () {
expect(html).to.contain("<h3>My Counter</h3>");
});
it("should have expected attributes on the `<link>` tag from frontmatter imports", async function () {
const regexExpression = new RegExp(`/components/counter/counter.${HASH_REGEX}.css`);
const link = Array.from(dom.window.document.querySelectorAll("head link")).find(
(link) => regexExpression.test(link.getAttribute("href")) && link.rel !== "preload",
);
expect(link.getAttribute("data-gwd-opt")).to.equal(null);
expect(link.getAttribute("foo")).to.equal("bar");
expect(link.getAttribute("baz")).to.equal("bar");
});
});
describe("Custom Multihyphen component", () => {
it("should output a custom <multihyphen-custom-element> tag", function () {
const hyphen = dom.window.document.querySelectorAll("body multihyphen-custom-element");
expect(hyphen.length).to.be.equal(1);
});
it("should output a <multihyphen-custom-element> tag that is _not_ wrapped in a <p> tag", function () {
expect(/<p><multihyphen-custom-element>/.test(html)).to.be.false;
expect(/<\/multihyphen-custom-element><\/p>/.test(html)).to.be.false;
});
it("should have the expected prerendered content", function () {
expect(html).to.contain("I have multiple hyphens in my tag name!");
});
it("should have expected attributes on the `<script>` tag from frontmatter imports", async function () {
const script = Array.from(dom.window.document.querySelectorAll("head script")).find(
(script) => script.getAttribute("src")?.startsWith("/multi-hyphen"),
);
expect(script.getAttribute("foo")).to.equal("bar");
expect(script.getAttribute("type")).to.equal("module");
});
});
describe("Frontmatter JavaScript import", () => {
it("should output a transformed frontmatter-standard.js file from custom frontmatter import", async function () {
const jsFiles = await glob.promise(
`${this.context.publicDir}**/**/frontmatter-standard.*.js`,
);
const contents = await fs.promises.readFile(jsFiles[0], "utf-8");
expect(jsFiles).to.have.lengthOf(1);
expect(contents).to.contain('console.log({message:"Hello from frontmatter standard"});');
});
it("should have expected attributes on the `<script>` tag from frontmatter imports", function () {
const script = Array.from(dom.window.document.querySelectorAll("head script")).find(
(script) => script.getAttribute("src")?.startsWith("/frontmatter-standard.DoXOcwxl"),
);
expect(script.getAttribute("type")).to.equal("module");
});
});
describe("Custom Frontmatter JavaScript import format with expected contents transformed", () => {
it("should output a transformed frontmatter-typescript.js file from custom frontmatter import", async function () {
const jsFiles = await glob.promise(
`${this.context.publicDir}**/**/frontmatter-typescript.*.js`,
);
const contents = await fs.promises.readFile(jsFiles[0], "utf-8");
expect(jsFiles).to.have.lengthOf(1);
expect(contents).to.contain('console.log({message:"Hello from frontmatter custom"});');
});
it("should have expected attributes on the `<script>` tag from frontmatter imports", function () {
const script = Array.from(dom.window.document.querySelectorAll("head script")).find(
(script) =>
script.getAttribute("src")?.startsWith("/frontmatter-typescript.CqVHikqT.js"),
);
expect(script.getAttribute("type")).to.equal("module");
});
});
describe("Custom Frontmatter CSS import format with expected contents transformed", () => {
it("should output a frontmatter-custom.css file from custom frontmatter import", async function () {
const cssFiles = await glob.promise(
`${this.context.publicDir}**/**/frontmatter-custom.*.css`,
);
const contents = await fs.promises.readFile(cssFiles[0], "utf-8");
expect(cssFiles).to.have.lengthOf(1);
expect(contents).to.equal("body{--primary-color:'red'}");
});
it("should have expected attributes on the `<link>` tag from frontmatter imports", async function () {
const regexExpression = new RegExp(`/styles/frontmatter-custom.${HASH_REGEX}.css`);
const link = Array.from(dom.window.document.querySelectorAll("head link")).find((link) =>
regexExpression.test(link.getAttribute("href")),
);
expect(link).to.not.be.undefined;
});
});
});
});
after(async function () {
await runner.teardown(getOutputTeardownFiles(outputPath));
});
});