@microsoft/fast-test-harness 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +24 -18
  2. package/dist/dts/build/generate-templates.d.ts +35 -0
  3. package/dist/dts/build/generate-templates.d.ts.map +1 -1
  4. package/dist/dts/build/generate-webui-templates.d.ts +13 -0
  5. package/dist/dts/build/generate-webui-templates.d.ts.map +1 -1
  6. package/dist/dts/fixtures/csr-fixture.d.ts +28 -0
  7. package/dist/dts/fixtures/csr-fixture.d.ts.map +1 -1
  8. package/dist/dts/fixtures/ssr-fixture.d.ts +19 -0
  9. package/dist/dts/fixtures/ssr-fixture.d.ts.map +1 -1
  10. package/dist/dts/ssr/render.d.ts.map +1 -1
  11. package/dist/esm/build/generate-templates.js +61 -1
  12. package/dist/esm/build/generate-webui-templates.js +8 -25
  13. package/dist/esm/fixtures/ssr-fixture.js +19 -1
  14. package/dist/esm/ssr/render.js +38 -5
  15. package/package.json +26 -15
  16. package/dist/dts/build/dom-shim.test.d.ts +0 -2
  17. package/dist/dts/build/dom-shim.test.d.ts.map +0 -1
  18. package/dist/dts/build/generate-stylesheets.test.d.ts +0 -2
  19. package/dist/dts/build/generate-stylesheets.test.d.ts.map +0 -1
  20. package/dist/dts/build/generate-templates.test.d.ts +0 -2
  21. package/dist/dts/build/generate-templates.test.d.ts.map +0 -1
  22. package/dist/dts/build/generate-webui-templates.test.d.ts +0 -2
  23. package/dist/dts/build/generate-webui-templates.test.d.ts.map +0 -1
  24. package/dist/dts/fixtures/csr-fixture.pw.spec.d.ts +0 -2
  25. package/dist/dts/fixtures/csr-fixture.pw.spec.d.ts.map +0 -1
  26. package/dist/dts/fixtures/ssr-fixture.pw.spec.d.ts +0 -2
  27. package/dist/dts/fixtures/ssr-fixture.pw.spec.d.ts.map +0 -1
  28. package/dist/dts/ssr/render.test.d.ts +0 -2
  29. package/dist/dts/ssr/render.test.d.ts.map +0 -1
  30. package/dist/esm/build/dom-shim.test.js +0 -202
  31. package/dist/esm/build/generate-stylesheets.test.js +0 -74
  32. package/dist/esm/build/generate-templates.test.js +0 -231
  33. package/dist/esm/build/generate-webui-templates.test.js +0 -179
  34. package/dist/esm/fixtures/csr-fixture.pw.spec.js +0 -137
  35. package/dist/esm/fixtures/ssr-fixture.pw.spec.js +0 -189
  36. package/dist/esm/ssr/render.test.js +0 -236
@@ -1,236 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
- import { buildEntryHtml, buildState, createSSRRenderer, parseDefaultValue, renderTemplate, } from "@microsoft/fast-test-harness/ssr/render.js";
4
- test.describe("parseDefaultValue", () => {
5
- test("should return empty string for empty input", () => {
6
- assert.strictEqual(parseDefaultValue(""), "");
7
- });
8
- test("should return empty string for 'undefined'", () => {
9
- assert.strictEqual(parseDefaultValue("undefined"), "");
10
- });
11
- test("should return empty string for 'null'", () => {
12
- assert.strictEqual(parseDefaultValue("null"), "");
13
- });
14
- test("should return true for 'true'", () => {
15
- assert.strictEqual(parseDefaultValue("true"), true);
16
- });
17
- test("should return false for 'false'", () => {
18
- assert.strictEqual(parseDefaultValue("false"), false);
19
- });
20
- test("should strip single quotes from strings", () => {
21
- assert.strictEqual(parseDefaultValue("'img'"), "img");
22
- });
23
- test("should strip double quotes from strings", () => {
24
- assert.strictEqual(parseDefaultValue('"hello"'), "hello");
25
- });
26
- test("should parse integers", () => {
27
- assert.strictEqual(parseDefaultValue("42"), 42);
28
- });
29
- test("should parse floats", () => {
30
- assert.strictEqual(parseDefaultValue("3.14"), 3.14);
31
- });
32
- test("should parse zero", () => {
33
- assert.strictEqual(parseDefaultValue("0"), 0);
34
- });
35
- test("should parse JSON arrays", () => {
36
- assert.deepStrictEqual(parseDefaultValue("[1,2,3]"), [1, 2, 3]);
37
- });
38
- test("should parse JSON objects", () => {
39
- assert.deepStrictEqual(parseDefaultValue('{"a":1}'), { a: 1 });
40
- });
41
- test("should return empty string for unparseable values", () => {
42
- assert.strictEqual(parseDefaultValue("some random text"), "");
43
- });
44
- test("should trim whitespace", () => {
45
- assert.strictEqual(parseDefaultValue(" true "), true);
46
- });
47
- });
48
- test.describe("renderTemplate", () => {
49
- test("should replace {{styles}} with a link tag", () => {
50
- const result = renderTemplate("<template>{{styles}}<div>hi</div></template>", "/styles.css");
51
- assert.ok(result.includes('<link rel="stylesheet" href="/styles.css">'));
52
- assert.ok(!result.includes("{{styles}}"));
53
- });
54
- test("should inject link after <template> if {{styles}} is absent", () => {
55
- const result = renderTemplate("<template><div>hi</div></template>", "/styles.css");
56
- assert.ok(result.includes('<link rel="stylesheet" href="/styles.css">'));
57
- assert.ok(result.indexOf("<link") > result.indexOf("<template>"));
58
- });
59
- test("should remove {{styles}} marker when styles URL is empty", () => {
60
- const input = "<template>{{styles}}<div>hi</div></template>";
61
- const result = renderTemplate(input, "");
62
- assert.strictEqual(result, "<template><div>hi</div></template>");
63
- assert.ok(!result.includes('<link rel="stylesheet"'));
64
- });
65
- });
66
- test.describe("buildEntryHtml", () => {
67
- test("should return raw html when 'html' key is present", () => {
68
- const result = buildEntryHtml({ html: "<div>raw</div>" });
69
- assert.strictEqual(result, "<div>raw</div>");
70
- });
71
- test("should build a custom element tag from tagName", () => {
72
- const result = buildEntryHtml({ tagName: "my-el" });
73
- assert.strictEqual(result, "<my-el></my-el>");
74
- });
75
- test("should include innerHTML", () => {
76
- const result = buildEntryHtml({
77
- tagName: "my-el",
78
- innerHTML: "content",
79
- });
80
- assert.strictEqual(result, "<my-el>content</my-el>");
81
- });
82
- test("should serialize attributes", () => {
83
- const result = buildEntryHtml({
84
- tagName: "my-el",
85
- attributes: JSON.stringify({ role: "button", size: "large" }),
86
- });
87
- assert.ok(result.includes('role="button"'));
88
- assert.ok(result.includes('size="large"'));
89
- });
90
- test("should handle boolean true attributes", () => {
91
- const result = buildEntryHtml({
92
- tagName: "my-el",
93
- attributes: JSON.stringify({ disabled: true }),
94
- });
95
- assert.ok(result.includes("disabled"));
96
- assert.ok(!result.includes('disabled="'));
97
- });
98
- test("should return empty string when no tagName or html", () => {
99
- assert.strictEqual(buildEntryHtml({}), "");
100
- });
101
- test("should handle invalid JSON in attributes gracefully", () => {
102
- const result = buildEntryHtml({
103
- tagName: "my-el",
104
- attributes: "not-json",
105
- });
106
- assert.strictEqual(result, "<my-el></my-el>");
107
- });
108
- test("should omit attributes with false, null, or undefined values", () => {
109
- const result = buildEntryHtml({
110
- tagName: "my-el",
111
- attributes: JSON.stringify({ disabled: false, hidden: null, role: "button" }),
112
- });
113
- assert.ok(!result.includes("disabled"), `should not include disabled: ${result}`);
114
- assert.ok(!result.includes("hidden"), `should not include hidden: ${result}`);
115
- assert.ok(result.includes('role="button"'), `should include role: ${result}`);
116
- });
117
- test("should escape special characters in attribute values", () => {
118
- const result = buildEntryHtml({
119
- tagName: "my-el",
120
- attributes: JSON.stringify({ title: 'a&b<c>"d' }),
121
- });
122
- assert.ok(result.includes("a&amp;b&lt;c&gt;&quot;d"), `got: ${result}`);
123
- });
124
- });
125
- test.describe("buildState", () => {
126
- test("should extract attributes into state", () => {
127
- const state = buildState({
128
- attributes: JSON.stringify({ size: "large", active: true }),
129
- });
130
- assert.strictEqual(state.size, "large");
131
- assert.strictEqual(state.active, true);
132
- });
133
- test("should add hyphen-stripped variants", () => {
134
- const state = buildState({
135
- attributes: JSON.stringify({ "aria-label": "Close" }),
136
- });
137
- assert.strictEqual(state["aria-label"], "Close");
138
- assert.strictEqual(state["arialabel"], "Close");
139
- });
140
- test("should return empty state for missing attributes", () => {
141
- const state = buildState({});
142
- assert.deepStrictEqual(state, {});
143
- });
144
- test("should handle invalid JSON gracefully", () => {
145
- const state = buildState({ attributes: "broken" });
146
- assert.deepStrictEqual(state, {});
147
- });
148
- });
149
- test.describe("createSSRRenderer", () => {
150
- test("should return a render function when @microsoft/fast-build is installed", () => {
151
- const renderer = createSSRRenderer({
152
- tagPrefix: "test",
153
- components: [{ name: "widget", packageName: "@microsoft/fast-test-harness" }],
154
- });
155
- assert.ok(typeof renderer.render === "function");
156
- });
157
- test("should return a RenderResult from render()", () => {
158
- const { render } = createSSRRenderer({
159
- tagPrefix: "test",
160
- components: [{ name: "widget", packageName: "@microsoft/fast-test-harness" }],
161
- });
162
- const result = render({ tagName: "test-widget", innerHTML: "hello" });
163
- assert.ok("template" in result, "result should have template");
164
- assert.ok("fixture" in result, "result should have fixture");
165
- assert.ok("preloadLinks" in result, "result should have preloadLinks");
166
- });
167
- test("should render a custom element tag in the fixture", () => {
168
- const { render } = createSSRRenderer({
169
- tagPrefix: "test",
170
- components: [{ name: "widget", packageName: "@microsoft/fast-test-harness" }],
171
- });
172
- const result = render({ tagName: "test-widget", innerHTML: "content" });
173
- assert.ok(result.fixture.includes("<test-widget"), `fixture should include element, got: ${result.fixture}`);
174
- assert.ok(result.fixture.includes("content"), `fixture should include innerHTML, got: ${result.fixture}`);
175
- });
176
- test("should include attributes in the rendered fixture", () => {
177
- const { render } = createSSRRenderer({
178
- tagPrefix: "test",
179
- components: [{ name: "widget", packageName: "@microsoft/fast-test-harness" }],
180
- });
181
- const result = render({
182
- tagName: "test-widget",
183
- attributes: JSON.stringify({ label: "Test", disabled: true }),
184
- });
185
- assert.ok(result.fixture.includes("label="), `fixture should include label attr, got: ${result.fixture}`);
186
- assert.ok(result.fixture.includes("disabled"), `fixture should include disabled attr, got: ${result.fixture}`);
187
- });
188
- test("should include theme stylesheet in preloadLinks", () => {
189
- const { render } = createSSRRenderer({
190
- tagPrefix: "test",
191
- components: [{ name: "widget", packageName: "@microsoft/fast-test-harness" }],
192
- themeStylesheet: "/theme.css",
193
- });
194
- const result = render({ tagName: "test-widget" });
195
- assert.ok(result.preloadLinks.includes("/theme.css"), `preloadLinks should include theme URL, got: ${result.preloadLinks}`);
196
- });
197
- test("should return empty preloadLinks without a theme", () => {
198
- const { render } = createSSRRenderer({
199
- tagPrefix: "test",
200
- components: [{ name: "widget", packageName: "@microsoft/fast-test-harness" }],
201
- });
202
- const result = render({ tagName: "test-widget" });
203
- assert.strictEqual(result.preloadLinks, "");
204
- });
205
- test("should handle raw HTML via the html key", () => {
206
- const { render } = createSSRRenderer({
207
- tagPrefix: "test",
208
- components: [{ name: "widget", packageName: "@microsoft/fast-test-harness" }],
209
- });
210
- const result = render({
211
- html: "<test-widget>raw html</test-widget>",
212
- });
213
- assert.ok(result.fixture.includes("raw html"), `fixture should contain raw html content, got: ${result.fixture}`);
214
- });
215
- test("should return empty fixture for empty query", () => {
216
- const { render } = createSSRRenderer({
217
- tagPrefix: "test",
218
- components: [{ name: "widget", packageName: "@microsoft/fast-test-harness" }],
219
- });
220
- const result = render({});
221
- assert.strictEqual(result.fixture, "");
222
- });
223
- test("should work with monolithic packageName mode", () => {
224
- // The test harness itself doesn't have component subdirectories,
225
- // so the template map will be empty — but the factory should not throw.
226
- const { render } = createSSRRenderer({
227
- tagPrefix: "fast",
228
- packageName: "@microsoft/fast-test-harness",
229
- distDir: "dist/esm",
230
- });
231
- assert.ok(typeof render === "function");
232
- const result = render({ tagName: "fast-widget", innerHTML: "hello" });
233
- // Without matching templates the WASM renderer should still produce output.
234
- assert.ok("fixture" in result);
235
- });
236
- });