@hamur/test-utils 0.0.0-stage → 0.0.2
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.
- package/LICENSE +22 -0
- package/README.md +12 -2
- package/dist/chunks/bun-index-td0qcx6r.js +151 -0
- package/dist/src/preact/index.js +24 -0
- package/dist/src/react/index.js +24 -0
- package/dist/src/solid/index.js +24 -0
- package/dist/src/svelte/index.js +24 -0
- package/dist/src/vue/index.js +24 -0
- package/dist/types/src/mount.d.ts +21 -0
- package/dist/types/src/preact/index.d.ts +10 -0
- package/dist/types/src/preact/mount-entry.d.ts +4 -0
- package/dist/types/src/react/index.d.ts +10 -0
- package/dist/types/src/react/mount-entry.d.ts +4 -0
- package/dist/types/src/solid/index.d.ts +11 -0
- package/dist/types/src/solid/mount-entry.d.ts +5 -0
- package/dist/types/src/svelte/index.d.ts +10 -0
- package/dist/types/src/vue/index.d.ts +13 -0
- package/dist/types/src/vue/mount-entry.d.ts +7 -0
- package/package.json +105 -4
- package/src/mount.ts +188 -0
- package/src/preact/index.ts +36 -0
- package/src/preact/mount-entry.ts +38 -0
- package/src/react/index.ts +39 -0
- package/src/react/mount-entry.ts +36 -0
- package/src/solid/index.ts +37 -0
- package/src/solid/mount-entry.ts +36 -0
- package/src/svelte/index.ts +36 -0
- package/src/svelte/mount-entry.svelte +24 -0
- package/src/vue/index.ts +43 -0
- package/src/vue/mount-entry.ts +25 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 productdevbook
|
|
4
|
+
Copyright (c) 2019 Material-UI SAS
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hamur/test-utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Component tests for [Hamur](https://github.com/productdevbook/hamur) apps, one entry per UI library: `@hamur/test-utils/react`, `/vue`, `/preact`, `/svelte` and `/solid`. It builds on `hamur/test`, which builds and serves the app inside `bun test`. It has no dependency; `hamur`, the adapter and the library are peers.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add -d @hamur/test-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Docs
|
|
12
|
+
|
|
13
|
+
[Documentation](https://docs.productdevbook.com/) and the [API reference](https://docs.productdevbook.com/api-reference/test-utils). MIT license.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/test-utils/src/mount.ts
|
|
3
|
+
import {
|
|
4
|
+
createPage,
|
|
5
|
+
renderComponent
|
|
6
|
+
} from "hamur/test";
|
|
7
|
+
import { componentMocks, firstValue, isRouteStub, mockComponentFile, mountServer } from "hamur/test/internal";
|
|
8
|
+
var LIBRARIES = new Set(["react", "vue", "preact", "svelte", "solid"]);
|
|
9
|
+
var WAIT = 1e4;
|
|
10
|
+
var IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
11
|
+
var VALUE = `@hamur/test-utils: mountSuspended needs a module path such as "app/components/Counter.vue"; a component value cannot be sent to the browser`;
|
|
12
|
+
var FRAMES = "new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(() => resolve(true), 50))))";
|
|
13
|
+
var READY = `(async () => {
|
|
14
|
+
const end = Date.now() + ${WAIT};
|
|
15
|
+
while (!window.__hamur_test_mount) {
|
|
16
|
+
if (Date.now() > end) throw new Error("the mount entry did not run within ${WAIT} ms");
|
|
17
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
18
|
+
}
|
|
19
|
+
await Promise.race([window.__hamur_test_mount.ready, new Promise((_, reject) => setTimeout(() => reject(new Error("the mount did not settle within ${WAIT} ms")), Math.max(1, end - Date.now())))]);
|
|
20
|
+
return true;
|
|
21
|
+
})()`;
|
|
22
|
+
var extensions = {
|
|
23
|
+
react: ".tsx",
|
|
24
|
+
vue: ".vue",
|
|
25
|
+
preact: ".tsx",
|
|
26
|
+
svelte: ".svelte",
|
|
27
|
+
solid: ".tsx"
|
|
28
|
+
};
|
|
29
|
+
var mockName = (label) => label.replace(/^.*[/\\]/, "").replace(/\.[^.]*$/, "");
|
|
30
|
+
var message = (error) => error instanceof Error ? error.message : String(error);
|
|
31
|
+
function isPlain(value) {
|
|
32
|
+
const prototype = Object.getPrototypeOf(value);
|
|
33
|
+
return prototype === Object.prototype || prototype === null;
|
|
34
|
+
}
|
|
35
|
+
function notJSON(value) {
|
|
36
|
+
if (typeof value === "function")
|
|
37
|
+
return "a function";
|
|
38
|
+
if (typeof value === "bigint" || typeof value === "symbol")
|
|
39
|
+
return `a ${typeof value}`;
|
|
40
|
+
if (typeof value === "number")
|
|
41
|
+
return Number.isFinite(value) ? undefined : String(value);
|
|
42
|
+
if (typeof value !== "object" || value === null || Array.isArray(value) || isPlain(value))
|
|
43
|
+
return;
|
|
44
|
+
const name = value.constructor?.name;
|
|
45
|
+
return typeof name === "string" && name ? `a ${name}` : "not a plain object";
|
|
46
|
+
}
|
|
47
|
+
function jsonProblem(value, path, seen) {
|
|
48
|
+
const problem = notJSON(value);
|
|
49
|
+
if (problem)
|
|
50
|
+
return `${path} is ${problem}`;
|
|
51
|
+
if (typeof value !== "object" || value === null)
|
|
52
|
+
return;
|
|
53
|
+
if (seen.has(value))
|
|
54
|
+
return `${path} is a circular reference`;
|
|
55
|
+
seen.add(value);
|
|
56
|
+
try {
|
|
57
|
+
if (Array.isArray(value)) {
|
|
58
|
+
for (const [index, item] of value.entries()) {
|
|
59
|
+
const found = item === undefined ? `${path}[${index}] is undefined` : jsonProblem(item, `${path}[${index}]`, seen);
|
|
60
|
+
if (found)
|
|
61
|
+
return found;
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
for (const [key, item] of Object.entries(value)) {
|
|
66
|
+
if (item === undefined)
|
|
67
|
+
continue;
|
|
68
|
+
const found = jsonProblem(item, IDENTIFIER.test(key) ? `${path}.${key}` : `${path}[${JSON.stringify(key)}]`, seen);
|
|
69
|
+
if (found)
|
|
70
|
+
return found;
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
} finally {
|
|
74
|
+
seen.delete(value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function jsonProps(props) {
|
|
78
|
+
const problem = typeof props !== "object" || props === null || Array.isArray(props) ? "props is not an object" : jsonProblem(props, "props", new Set);
|
|
79
|
+
if (problem)
|
|
80
|
+
throw new Error(`@hamur/test-utils: mountSuspended props must be JSON values; ${problem}`);
|
|
81
|
+
return props;
|
|
82
|
+
}
|
|
83
|
+
function entryError(framework) {
|
|
84
|
+
if (!LIBRARIES.has(framework))
|
|
85
|
+
return new Error(`@hamur/test-utils: the app's framework is ${JSON.stringify(framework)}, which has no @hamur/test-utils entry`);
|
|
86
|
+
return new Error(`@hamur/test-utils: the app's framework is ${JSON.stringify(framework)}; import mountSuspended from "@hamur/test-utils/${framework}"`);
|
|
87
|
+
}
|
|
88
|
+
async function mount(library, entry, component, options = {}) {
|
|
89
|
+
const stub = isRouteStub(component) ? component : undefined;
|
|
90
|
+
if (stub) {
|
|
91
|
+
const value = firstValue(stub.routes);
|
|
92
|
+
if (value !== undefined)
|
|
93
|
+
throw new Error(`@hamur/test-utils: mountSuspended needs module paths in createRouteStub; ${JSON.stringify(value)} has a component value`);
|
|
94
|
+
} else if (typeof component !== "string")
|
|
95
|
+
throw new Error(VALUE);
|
|
96
|
+
const props = jsonProps(options.props ?? {});
|
|
97
|
+
const server = await mountServer();
|
|
98
|
+
if (server.framework !== library)
|
|
99
|
+
throw entryError(server.framework);
|
|
100
|
+
for (const { label, replacement } of componentMocks().values()) {
|
|
101
|
+
if (typeof replacement === "function") {
|
|
102
|
+
throw new Error(`@hamur/test-utils: mountSuspended cannot send the factory of mockComponent(${JSON.stringify(label)}) to the browser; pass a module path: mockComponent(${JSON.stringify(label)}, "test/mocks/${mockName(label)}${extensions[library]}")`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const url = await server.document({
|
|
106
|
+
entry,
|
|
107
|
+
component: stub ?? component,
|
|
108
|
+
props,
|
|
109
|
+
route: options.route ?? stub?.initialURL ?? "/"
|
|
110
|
+
});
|
|
111
|
+
const page = await createPage(url, { wait: "load", backend: options.backend });
|
|
112
|
+
try {
|
|
113
|
+
await page.evaluate(READY);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
const problems = page.problems();
|
|
116
|
+
await page.close().catch(() => {});
|
|
117
|
+
throw new Error(`@hamur/test-utils: mountSuspended(${stub ? "a route stub" : JSON.stringify(component)}) did not mount: ${message(error)}${problems.length ? `
|
|
118
|
+
${problems.join(`
|
|
119
|
+
`)}` : ""}`, { cause: error });
|
|
120
|
+
}
|
|
121
|
+
const root = `[id=${JSON.stringify(server.rootId)}]`;
|
|
122
|
+
const call = (script) => page.evaluate(`window.__hamur_test_mount.${script}`);
|
|
123
|
+
const mounted = {
|
|
124
|
+
page,
|
|
125
|
+
text: (selector) => page.text(selector ?? root),
|
|
126
|
+
html: (selector) => selector === undefined ? page.evaluate(`document.querySelector(${JSON.stringify(root)})?.innerHTML ?? ""`) : page.html(selector),
|
|
127
|
+
async click(selector) {
|
|
128
|
+
await page.click(selector);
|
|
129
|
+
await page.evaluate(FRAMES);
|
|
130
|
+
},
|
|
131
|
+
async setProps(next) {
|
|
132
|
+
await call(`setProps(${JSON.stringify(jsonProps(next))})`);
|
|
133
|
+
},
|
|
134
|
+
async unmount() {
|
|
135
|
+
try {
|
|
136
|
+
await call("unmount()");
|
|
137
|
+
} finally {
|
|
138
|
+
await page.close();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
return mounted;
|
|
143
|
+
}
|
|
144
|
+
function render(component, options) {
|
|
145
|
+
return renderComponent(component, options);
|
|
146
|
+
}
|
|
147
|
+
function mockComponent2(path, replacement) {
|
|
148
|
+
return mockComponentFile(path, replacement);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export { mount, render, mockComponent2 };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
mount,
|
|
4
|
+
render,
|
|
5
|
+
mockComponent2
|
|
6
|
+
} from "../../chunks/bun-index-td0qcx6r.js";
|
|
7
|
+
|
|
8
|
+
// packages/test-utils/src/preact/index.ts
|
|
9
|
+
import { fileURLToPath } from "url";
|
|
10
|
+
var ENTRY = fileURLToPath(new URL("./mount-entry.ts", new URL("../../../src/preact/index.ts", import.meta.url).href));
|
|
11
|
+
function mountSuspended(component, options) {
|
|
12
|
+
return mount("preact", ENTRY, component, options);
|
|
13
|
+
}
|
|
14
|
+
function renderSuspended(component, options) {
|
|
15
|
+
return render(component, options);
|
|
16
|
+
}
|
|
17
|
+
function mockComponent(path, replacement) {
|
|
18
|
+
return mockComponent2(path, replacement);
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
mockComponent,
|
|
22
|
+
mountSuspended,
|
|
23
|
+
renderSuspended
|
|
24
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
mount,
|
|
4
|
+
render,
|
|
5
|
+
mockComponent2
|
|
6
|
+
} from "../../chunks/bun-index-td0qcx6r.js";
|
|
7
|
+
|
|
8
|
+
// packages/test-utils/src/react/index.ts
|
|
9
|
+
import { fileURLToPath } from "url";
|
|
10
|
+
var ENTRY = fileURLToPath(new URL("./mount-entry.ts", new URL("../../../src/react/index.ts", import.meta.url).href));
|
|
11
|
+
function mountSuspended(component, options) {
|
|
12
|
+
return mount("react", ENTRY, component, options);
|
|
13
|
+
}
|
|
14
|
+
function renderSuspended(component, options) {
|
|
15
|
+
return render(component, options);
|
|
16
|
+
}
|
|
17
|
+
function mockComponent(path, replacement) {
|
|
18
|
+
return mockComponent2(path, replacement);
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
mockComponent,
|
|
22
|
+
mountSuspended,
|
|
23
|
+
renderSuspended
|
|
24
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
mount,
|
|
4
|
+
render,
|
|
5
|
+
mockComponent2
|
|
6
|
+
} from "../../chunks/bun-index-td0qcx6r.js";
|
|
7
|
+
|
|
8
|
+
// packages/test-utils/src/solid/index.ts
|
|
9
|
+
import { fileURLToPath } from "url";
|
|
10
|
+
var ENTRY = fileURLToPath(new URL("./mount-entry.ts", new URL("../../../src/solid/index.ts", import.meta.url).href));
|
|
11
|
+
function mountSuspended(component, options) {
|
|
12
|
+
return mount("solid", ENTRY, component, options);
|
|
13
|
+
}
|
|
14
|
+
function renderSuspended(component, options) {
|
|
15
|
+
return render(component, options);
|
|
16
|
+
}
|
|
17
|
+
function mockComponent(path, replacement) {
|
|
18
|
+
return mockComponent2(path, replacement);
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
mockComponent,
|
|
22
|
+
mountSuspended,
|
|
23
|
+
renderSuspended
|
|
24
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
mount,
|
|
4
|
+
render,
|
|
5
|
+
mockComponent2
|
|
6
|
+
} from "../../chunks/bun-index-td0qcx6r.js";
|
|
7
|
+
|
|
8
|
+
// packages/test-utils/src/svelte/index.ts
|
|
9
|
+
import { fileURLToPath } from "url";
|
|
10
|
+
var ENTRY = fileURLToPath(new URL("./mount-entry.svelte", new URL("../../../src/svelte/index.ts", import.meta.url).href));
|
|
11
|
+
function mountSuspended(component, options) {
|
|
12
|
+
return mount("svelte", ENTRY, component, options);
|
|
13
|
+
}
|
|
14
|
+
function renderSuspended(component, options) {
|
|
15
|
+
return render(component, options);
|
|
16
|
+
}
|
|
17
|
+
function mockComponent(path, replacement) {
|
|
18
|
+
return mockComponent2(path, replacement);
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
mockComponent,
|
|
22
|
+
mountSuspended,
|
|
23
|
+
renderSuspended
|
|
24
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
mount,
|
|
4
|
+
render,
|
|
5
|
+
mockComponent2
|
|
6
|
+
} from "../../chunks/bun-index-td0qcx6r.js";
|
|
7
|
+
|
|
8
|
+
// packages/test-utils/src/vue/index.ts
|
|
9
|
+
import { fileURLToPath } from "url";
|
|
10
|
+
var ENTRY = fileURLToPath(new URL("./mount-entry.ts", new URL("../../../src/vue/index.ts", import.meta.url).href));
|
|
11
|
+
function mountSuspended(component, options) {
|
|
12
|
+
return mount("vue", ENTRY, component, options);
|
|
13
|
+
}
|
|
14
|
+
function renderSuspended(component, options) {
|
|
15
|
+
return render(component, options);
|
|
16
|
+
}
|
|
17
|
+
function mockComponent(path, replacement) {
|
|
18
|
+
return mockComponent2(path, replacement);
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
mockComponent,
|
|
22
|
+
mountSuspended,
|
|
23
|
+
renderSuspended
|
|
24
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type RenderComponentOptions, type RenderedComponent, type TestPage } from "hamur/test";
|
|
2
|
+
export type Library = "react" | "vue" | "preact" | "svelte" | "solid";
|
|
3
|
+
export interface MountOptionsOf<P> {
|
|
4
|
+
props?: P;
|
|
5
|
+
route?: string;
|
|
6
|
+
backend?: "chrome" | "webkit" | "cdp";
|
|
7
|
+
}
|
|
8
|
+
export interface MountedOf<P> {
|
|
9
|
+
readonly page: TestPage;
|
|
10
|
+
text(selector?: string): Promise<string>;
|
|
11
|
+
html(selector?: string): Promise<string>;
|
|
12
|
+
click(selector: string): Promise<void>;
|
|
13
|
+
setProps(props: Partial<P>): Promise<void>;
|
|
14
|
+
unmount(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export type RenderSuspendedOptionsOf<P> = Omit<RenderComponentOptions, "props"> & {
|
|
17
|
+
props?: P;
|
|
18
|
+
};
|
|
19
|
+
export declare function mount<P>(library: Library, entry: string, component: unknown, options?: MountOptionsOf<P>): Promise<MountedOf<P>>;
|
|
20
|
+
export declare function render<P>(component: unknown, options?: RenderSuspendedOptionsOf<P>): Promise<RenderedComponent>;
|
|
21
|
+
export declare function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ComponentProps, ComponentType } from "preact";
|
|
2
|
+
import type { RenderedComponent, RouteStub } from "hamur/test";
|
|
3
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal";
|
|
4
|
+
import { type MountedOf, type MountOptionsOf, type RenderSuspendedOptionsOf } from "../mount.ts";
|
|
5
|
+
export type PropsOf<C> = LibraryProps<C, C extends ComponentType<any> ? ComponentProps<C> : Record<string, unknown>>;
|
|
6
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>;
|
|
7
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>;
|
|
8
|
+
export declare function mountSuspended<C = unknown>(component: string | RouteStub, options?: MountOptions<C>): Promise<Mounted<C>>;
|
|
9
|
+
export declare function renderSuspended<C>(component: C | string | RouteStub, options?: RenderSuspendedOptionsOf<PropsOf<C>>): Promise<RenderedComponent>;
|
|
10
|
+
export declare function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export default function HamurTestMount(): import("preact").VNode<Record<string, unknown>> | null;
|
|
2
|
+
export declare function show(component: unknown, props: Record<string, unknown>): void;
|
|
3
|
+
export declare function setProps(props: Record<string, unknown>): void;
|
|
4
|
+
export declare function unmount(): void;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ComponentProps, JSXElementConstructor } from "react";
|
|
2
|
+
import type { RenderedComponent, RouteStub } from "hamur/test";
|
|
3
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal";
|
|
4
|
+
import { type MountedOf, type MountOptionsOf, type RenderSuspendedOptionsOf } from "../mount.ts";
|
|
5
|
+
export type PropsOf<C> = LibraryProps<C, C extends JSXElementConstructor<any> ? ComponentProps<C> : Record<string, unknown>>;
|
|
6
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>;
|
|
7
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>;
|
|
8
|
+
export declare function mountSuspended<C = unknown>(component: string | RouteStub, options?: MountOptions<C>): Promise<Mounted<C>>;
|
|
9
|
+
export declare function renderSuspended<C>(component: C | string | RouteStub, options?: RenderSuspendedOptionsOf<PropsOf<C>>): Promise<RenderedComponent>;
|
|
10
|
+
export declare function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export default function HamurTestMount(): import("react").ReactElement<Record<string, unknown>, string | import("react").JSXElementConstructor<any>> | null;
|
|
2
|
+
export declare function show(component: unknown, props: Record<string, unknown>): void;
|
|
3
|
+
export declare function setProps(props: Record<string, unknown>): void;
|
|
4
|
+
export declare function unmount(): void;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Component } from "solid-js";
|
|
2
|
+
import type { ComponentProps } from "@solidjs/web";
|
|
3
|
+
import type { RenderedComponent, RouteStub } from "hamur/test";
|
|
4
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal";
|
|
5
|
+
import { type MountedOf, type MountOptionsOf, type RenderSuspendedOptionsOf } from "../mount.ts";
|
|
6
|
+
export type PropsOf<C> = LibraryProps<C, C extends Component<any> ? ComponentProps<C> : Record<string, unknown>>;
|
|
7
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>;
|
|
8
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>;
|
|
9
|
+
export declare function mountSuspended<C = unknown>(component: string | RouteStub, options?: MountOptions<C>): Promise<Mounted<C>>;
|
|
10
|
+
export declare function renderSuspended<C>(component: C | string | RouteStub, options?: RenderSuspendedOptionsOf<PropsOf<C>>): Promise<RenderedComponent>;
|
|
11
|
+
export declare function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
export default function HamurTestMount(): JSX.Element;
|
|
3
|
+
export declare function show(component: unknown, props: Record<string, unknown>): void;
|
|
4
|
+
export declare function setProps(props: Record<string, unknown>): void;
|
|
5
|
+
export declare function unmount(): void;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Component, ComponentProps } from "svelte";
|
|
2
|
+
import type { RenderedComponent, RouteStub } from "hamur/test";
|
|
3
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal";
|
|
4
|
+
import { type MountedOf, type MountOptionsOf, type RenderSuspendedOptionsOf } from "../mount.ts";
|
|
5
|
+
export type PropsOf<C> = LibraryProps<C, C extends Component<any, any> ? ComponentProps<C> : Record<string, unknown>>;
|
|
6
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>;
|
|
7
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>;
|
|
8
|
+
export declare function mountSuspended<C = unknown>(component: string | RouteStub, options?: MountOptions<C>): Promise<Mounted<C>>;
|
|
9
|
+
export declare function renderSuspended<C>(component: C | string | RouteStub, options?: RenderSuspendedOptionsOf<PropsOf<C>>): Promise<RenderedComponent>;
|
|
10
|
+
export declare function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RenderedComponent, RouteStub } from "hamur/test";
|
|
2
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal";
|
|
3
|
+
import { type MountedOf, type MountOptionsOf, type RenderSuspendedOptionsOf } from "../mount.ts";
|
|
4
|
+
type ComponentProps<C> = C extends abstract new (...args: never) => {
|
|
5
|
+
$props: infer P;
|
|
6
|
+
} ? P : C extends (...args: infer A) => unknown ? A extends [(infer P)?, ...unknown[]] ? P : Record<string, unknown> : Record<string, unknown>;
|
|
7
|
+
export type PropsOf<C> = LibraryProps<C, ComponentProps<C>>;
|
|
8
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>;
|
|
9
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>;
|
|
10
|
+
export declare function mountSuspended<C = unknown>(component: string | RouteStub, options?: MountOptions<C>): Promise<Mounted<C>>;
|
|
11
|
+
export declare function renderSuspended<C>(component: C | string | RouteStub, options?: RenderSuspendedOptionsOf<PropsOf<C>>): Promise<RenderedComponent>;
|
|
12
|
+
export declare function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
declare const _default: import("vue").DefineComponent<{}, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
2
|
+
[key: string]: any;
|
|
3
|
+
}> | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
4
|
+
export default _default;
|
|
5
|
+
export declare function show(component: unknown, next: Record<string, unknown>): void;
|
|
6
|
+
export declare function setProps(next: Record<string, unknown>): void;
|
|
7
|
+
export declare function unmount(): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,107 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamur/test-utils",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/productdevbook/hamur.git",
|
|
9
|
+
"directory": "packages/test-utils"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
"./react": {
|
|
13
|
+
"types": "./dist/types/src/react/index.d.ts",
|
|
14
|
+
"bun": "./src/react/index.ts",
|
|
15
|
+
"hamur-source": "./src/react/index.ts",
|
|
16
|
+
"node": "./dist/src/react/index.js",
|
|
17
|
+
"default": "./src/react/index.ts"
|
|
18
|
+
},
|
|
19
|
+
"./vue": {
|
|
20
|
+
"types": "./dist/types/src/vue/index.d.ts",
|
|
21
|
+
"bun": "./src/vue/index.ts",
|
|
22
|
+
"hamur-source": "./src/vue/index.ts",
|
|
23
|
+
"node": "./dist/src/vue/index.js",
|
|
24
|
+
"default": "./src/vue/index.ts"
|
|
25
|
+
},
|
|
26
|
+
"./preact": {
|
|
27
|
+
"types": "./dist/types/src/preact/index.d.ts",
|
|
28
|
+
"bun": "./src/preact/index.ts",
|
|
29
|
+
"hamur-source": "./src/preact/index.ts",
|
|
30
|
+
"node": "./dist/src/preact/index.js",
|
|
31
|
+
"default": "./src/preact/index.ts"
|
|
32
|
+
},
|
|
33
|
+
"./svelte": {
|
|
34
|
+
"types": "./dist/types/src/svelte/index.d.ts",
|
|
35
|
+
"bun": "./src/svelte/index.ts",
|
|
36
|
+
"hamur-source": "./src/svelte/index.ts",
|
|
37
|
+
"node": "./dist/src/svelte/index.js",
|
|
38
|
+
"default": "./src/svelte/index.ts"
|
|
39
|
+
},
|
|
40
|
+
"./solid": {
|
|
41
|
+
"types": "./dist/types/src/solid/index.d.ts",
|
|
42
|
+
"bun": "./src/solid/index.ts",
|
|
43
|
+
"hamur-source": "./src/solid/index.ts",
|
|
44
|
+
"node": "./dist/src/solid/index.js",
|
|
45
|
+
"default": "./src/solid/index.ts"
|
|
46
|
+
},
|
|
47
|
+
"./package.json": "./package.json"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"dist",
|
|
51
|
+
"src"
|
|
52
|
+
],
|
|
53
|
+
"dependencies": {},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"hamur": "^0.0.2",
|
|
56
|
+
"@hamur/adapter-react": "^0.0.2",
|
|
57
|
+
"@hamur/adapter-vue": "^0.0.2",
|
|
58
|
+
"@hamur/adapter-preact": "^0.0.2",
|
|
59
|
+
"@hamur/adapter-svelte": "^0.0.2",
|
|
60
|
+
"@hamur/adapter-solid": "^0.0.2",
|
|
61
|
+
"react": "^19.3.1",
|
|
62
|
+
"react-dom": "^19.3.1",
|
|
63
|
+
"vue": "^3.5.44",
|
|
64
|
+
"preact": "^11.0.2",
|
|
65
|
+
"svelte": "^5.57.2",
|
|
66
|
+
"solid-js": "^2.0.4",
|
|
67
|
+
"@solidjs/web": "^2.0.4"
|
|
68
|
+
},
|
|
69
|
+
"peerDependenciesMeta": {
|
|
70
|
+
"@hamur/adapter-react": {
|
|
71
|
+
"optional": true
|
|
72
|
+
},
|
|
73
|
+
"@hamur/adapter-vue": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"@hamur/adapter-preact": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"@hamur/adapter-svelte": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"@hamur/adapter-solid": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"react": {
|
|
86
|
+
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"react-dom": {
|
|
89
|
+
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"vue": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"preact": {
|
|
95
|
+
"optional": true
|
|
96
|
+
},
|
|
97
|
+
"svelte": {
|
|
98
|
+
"optional": true
|
|
99
|
+
},
|
|
100
|
+
"solid-js": {
|
|
101
|
+
"optional": true
|
|
102
|
+
},
|
|
103
|
+
"@solidjs/web": {
|
|
104
|
+
"optional": true
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
package/src/mount.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPage,
|
|
3
|
+
renderComponent,
|
|
4
|
+
type RenderComponentOptions,
|
|
5
|
+
type RenderedComponent,
|
|
6
|
+
type TestPage,
|
|
7
|
+
} from "hamur/test"
|
|
8
|
+
import { componentMocks, firstValue, isRouteStub, mockComponentFile, mountServer } from "hamur/test/internal"
|
|
9
|
+
|
|
10
|
+
export type Library = "react" | "vue" | "preact" | "svelte" | "solid"
|
|
11
|
+
|
|
12
|
+
export interface MountOptionsOf<P> {
|
|
13
|
+
props?: P
|
|
14
|
+
route?: string
|
|
15
|
+
backend?: "chrome" | "webkit" | "cdp"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface MountedOf<P> {
|
|
19
|
+
readonly page: TestPage
|
|
20
|
+
text(selector?: string): Promise<string>
|
|
21
|
+
html(selector?: string): Promise<string>
|
|
22
|
+
click(selector: string): Promise<void>
|
|
23
|
+
setProps(props: Partial<P>): Promise<void>
|
|
24
|
+
unmount(): Promise<void>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type RenderSuspendedOptionsOf<P> = Omit<RenderComponentOptions, "props"> & { props?: P }
|
|
28
|
+
|
|
29
|
+
const LIBRARIES = new Set<string>(["react", "vue", "preact", "svelte", "solid"])
|
|
30
|
+
const WAIT = 10_000
|
|
31
|
+
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/
|
|
32
|
+
const VALUE = `@hamur/test-utils: mountSuspended needs a module path such as "app/components/Counter.vue"; a component value cannot be sent to the browser`
|
|
33
|
+
const FRAMES =
|
|
34
|
+
"new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(() => resolve(true), 50))))"
|
|
35
|
+
const READY = `(async () => {
|
|
36
|
+
const end = Date.now() + ${WAIT};
|
|
37
|
+
while (!window.__hamur_test_mount) {
|
|
38
|
+
if (Date.now() > end) throw new Error("the mount entry did not run within ${WAIT} ms");
|
|
39
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
40
|
+
}
|
|
41
|
+
await Promise.race([window.__hamur_test_mount.ready, new Promise((_, reject) => setTimeout(() => reject(new Error("the mount did not settle within ${WAIT} ms")), Math.max(1, end - Date.now())))]);
|
|
42
|
+
return true;
|
|
43
|
+
})()`
|
|
44
|
+
|
|
45
|
+
const extensions: Record<Library, string> = {
|
|
46
|
+
react: ".tsx",
|
|
47
|
+
vue: ".vue",
|
|
48
|
+
preact: ".tsx",
|
|
49
|
+
svelte: ".svelte",
|
|
50
|
+
solid: ".tsx",
|
|
51
|
+
}
|
|
52
|
+
const mockName = (label: string) => label.replace(/^.*[/\\]/, "").replace(/\.[^.]*$/, "")
|
|
53
|
+
const message = (error: unknown) => (error instanceof Error ? error.message : String(error))
|
|
54
|
+
|
|
55
|
+
function isPlain(value: object): boolean {
|
|
56
|
+
const prototype: unknown = Object.getPrototypeOf(value)
|
|
57
|
+
return prototype === Object.prototype || prototype === null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function notJSON(value: unknown): string | undefined {
|
|
61
|
+
if (typeof value === "function") return "a function"
|
|
62
|
+
if (typeof value === "bigint" || typeof value === "symbol") return `a ${typeof value}`
|
|
63
|
+
if (typeof value === "number") return Number.isFinite(value) ? undefined : String(value)
|
|
64
|
+
if (typeof value !== "object" || value === null || Array.isArray(value) || isPlain(value)) return undefined
|
|
65
|
+
const name = (value as { constructor?: { name?: unknown } }).constructor?.name
|
|
66
|
+
return typeof name === "string" && name ? `a ${name}` : "not a plain object"
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function jsonProblem(value: unknown, path: string, seen: Set<object>): string | undefined {
|
|
70
|
+
const problem = notJSON(value)
|
|
71
|
+
if (problem) return `${path} is ${problem}`
|
|
72
|
+
if (typeof value !== "object" || value === null) return undefined
|
|
73
|
+
if (seen.has(value)) return `${path} is a circular reference`
|
|
74
|
+
seen.add(value)
|
|
75
|
+
try {
|
|
76
|
+
if (Array.isArray(value)) {
|
|
77
|
+
for (const [index, item] of value.entries()) {
|
|
78
|
+
const found =
|
|
79
|
+
item === undefined ? `${path}[${index}] is undefined` : jsonProblem(item, `${path}[${index}]`, seen)
|
|
80
|
+
if (found) return found
|
|
81
|
+
}
|
|
82
|
+
return undefined
|
|
83
|
+
}
|
|
84
|
+
for (const [key, item] of Object.entries(value)) {
|
|
85
|
+
if (item === undefined) continue
|
|
86
|
+
const found = jsonProblem(item, IDENTIFIER.test(key) ? `${path}.${key}` : `${path}[${JSON.stringify(key)}]`, seen)
|
|
87
|
+
if (found) return found
|
|
88
|
+
}
|
|
89
|
+
return undefined
|
|
90
|
+
} finally {
|
|
91
|
+
seen.delete(value)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function jsonProps(props: unknown): Record<string, unknown> {
|
|
96
|
+
const problem =
|
|
97
|
+
typeof props !== "object" || props === null || Array.isArray(props)
|
|
98
|
+
? "props is not an object"
|
|
99
|
+
: jsonProblem(props, "props", new Set())
|
|
100
|
+
if (problem) throw new Error(`@hamur/test-utils: mountSuspended props must be JSON values; ${problem}`)
|
|
101
|
+
return props as Record<string, unknown>
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function entryError(framework: string): Error {
|
|
105
|
+
if (!LIBRARIES.has(framework))
|
|
106
|
+
return new Error(
|
|
107
|
+
`@hamur/test-utils: the app's framework is ${JSON.stringify(framework)}, which has no @hamur/test-utils entry`,
|
|
108
|
+
)
|
|
109
|
+
return new Error(
|
|
110
|
+
`@hamur/test-utils: the app's framework is ${JSON.stringify(framework)}; import mountSuspended from "@hamur/test-utils/${framework}"`,
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function mount<P>(
|
|
115
|
+
library: Library,
|
|
116
|
+
entry: string,
|
|
117
|
+
component: unknown,
|
|
118
|
+
options: MountOptionsOf<P> = {},
|
|
119
|
+
): Promise<MountedOf<P>> {
|
|
120
|
+
const stub = isRouteStub(component) ? component : undefined
|
|
121
|
+
if (stub) {
|
|
122
|
+
const value = firstValue(stub.routes)
|
|
123
|
+
if (value !== undefined)
|
|
124
|
+
throw new Error(
|
|
125
|
+
`@hamur/test-utils: mountSuspended needs module paths in createRouteStub; ${JSON.stringify(value)} has a component value`,
|
|
126
|
+
)
|
|
127
|
+
} else if (typeof component !== "string") throw new Error(VALUE)
|
|
128
|
+
const props = jsonProps(options.props ?? {})
|
|
129
|
+
const server = await mountServer()
|
|
130
|
+
if (server.framework !== library) throw entryError(server.framework)
|
|
131
|
+
for (const { label, replacement } of componentMocks().values()) {
|
|
132
|
+
if (typeof replacement === "function") {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`@hamur/test-utils: mountSuspended cannot send the factory of mockComponent(${JSON.stringify(label)}) to the browser; pass a module path: mockComponent(${JSON.stringify(label)}, "test/mocks/${mockName(label)}${extensions[library]}")`,
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const url = await server.document({
|
|
139
|
+
entry,
|
|
140
|
+
component: stub ?? (component as string),
|
|
141
|
+
props,
|
|
142
|
+
route: options.route ?? stub?.initialURL ?? "/",
|
|
143
|
+
})
|
|
144
|
+
const page = await createPage(url, { wait: "load", backend: options.backend })
|
|
145
|
+
try {
|
|
146
|
+
await page.evaluate(READY)
|
|
147
|
+
} catch (error) {
|
|
148
|
+
const problems = page.problems()
|
|
149
|
+
await page.close().catch(() => {})
|
|
150
|
+
throw new Error(
|
|
151
|
+
`@hamur/test-utils: mountSuspended(${stub ? "a route stub" : JSON.stringify(component)}) did not mount: ${message(error)}${problems.length ? `\n${problems.join("\n")}` : ""}`,
|
|
152
|
+
{ cause: error },
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
const root = `[id=${JSON.stringify(server.rootId)}]`
|
|
156
|
+
const call = (script: string) => page.evaluate(`window.__hamur_test_mount.${script}`)
|
|
157
|
+
const mounted: MountedOf<P> = {
|
|
158
|
+
page,
|
|
159
|
+
text: (selector) => page.text(selector ?? root),
|
|
160
|
+
html: (selector) =>
|
|
161
|
+
selector === undefined
|
|
162
|
+
? page.evaluate<string>(`document.querySelector(${JSON.stringify(root)})?.innerHTML ?? ""`)
|
|
163
|
+
: page.html(selector),
|
|
164
|
+
async click(selector) {
|
|
165
|
+
await page.click(selector)
|
|
166
|
+
await page.evaluate(FRAMES)
|
|
167
|
+
},
|
|
168
|
+
async setProps(next) {
|
|
169
|
+
await call(`setProps(${JSON.stringify(jsonProps(next))})`)
|
|
170
|
+
},
|
|
171
|
+
async unmount() {
|
|
172
|
+
try {
|
|
173
|
+
await call("unmount()")
|
|
174
|
+
} finally {
|
|
175
|
+
await page.close()
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
}
|
|
179
|
+
return mounted
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function render<P>(component: unknown, options?: RenderSuspendedOptionsOf<P>): Promise<RenderedComponent> {
|
|
183
|
+
return renderComponent(component, options as unknown as RenderComponentOptions | undefined)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void> {
|
|
187
|
+
return mockComponentFile(path, replacement)
|
|
188
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url"
|
|
2
|
+
import type { ComponentProps, ComponentType } from "preact"
|
|
3
|
+
import type { RenderedComponent, RouteStub } from "hamur/test"
|
|
4
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal"
|
|
5
|
+
import {
|
|
6
|
+
mockComponent as mockComponentOf,
|
|
7
|
+
mount,
|
|
8
|
+
render,
|
|
9
|
+
type MountedOf,
|
|
10
|
+
type MountOptionsOf,
|
|
11
|
+
type RenderSuspendedOptionsOf,
|
|
12
|
+
} from "../mount.ts"
|
|
13
|
+
|
|
14
|
+
export type PropsOf<C> = LibraryProps<C, C extends ComponentType<any> ? ComponentProps<C> : Record<string, unknown>>
|
|
15
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>
|
|
16
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>
|
|
17
|
+
|
|
18
|
+
const ENTRY = fileURLToPath(new URL("./mount-entry.ts", import.meta.url))
|
|
19
|
+
|
|
20
|
+
export function mountSuspended<C = unknown>(
|
|
21
|
+
component: string | RouteStub,
|
|
22
|
+
options?: MountOptions<C>,
|
|
23
|
+
): Promise<Mounted<C>> {
|
|
24
|
+
return mount("preact", ENTRY, component, options)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function renderSuspended<C>(
|
|
28
|
+
component: C | string | RouteStub,
|
|
29
|
+
options?: RenderSuspendedOptionsOf<PropsOf<C>>,
|
|
30
|
+
): Promise<RenderedComponent> {
|
|
31
|
+
return render(component, options)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void> {
|
|
35
|
+
return mockComponentOf(path, replacement)
|
|
36
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createElement, type ComponentType } from "preact"
|
|
2
|
+
import { useEffect, useState } from "preact/hooks"
|
|
3
|
+
|
|
4
|
+
interface Shown {
|
|
5
|
+
component: ComponentType<Record<string, unknown>> | undefined
|
|
6
|
+
props: Record<string, unknown>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let shown: Shown = { component: undefined, props: {} }
|
|
10
|
+
const listeners = new Set<(next: Shown) => void>()
|
|
11
|
+
|
|
12
|
+
function change(next: Shown): void {
|
|
13
|
+
shown = next
|
|
14
|
+
for (const listener of listeners) listener(next)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default function HamurTestMount() {
|
|
18
|
+
const [current, setCurrent] = useState(shown)
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
const listener = (next: Shown) => setCurrent(next)
|
|
21
|
+
listeners.add(listener)
|
|
22
|
+
setCurrent(shown)
|
|
23
|
+
return () => void listeners.delete(listener)
|
|
24
|
+
}, [])
|
|
25
|
+
return current.component ? createElement(current.component, current.props) : null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function show(component: unknown, props: Record<string, unknown>): void {
|
|
29
|
+
change({ component: component as Shown["component"], props })
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function setProps(props: Record<string, unknown>): void {
|
|
33
|
+
change({ ...shown, props: { ...shown.props, ...props } })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function unmount(): void {
|
|
37
|
+
change({ component: undefined, props: {} })
|
|
38
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url"
|
|
2
|
+
import type { ComponentProps, JSXElementConstructor } from "react"
|
|
3
|
+
import type { RenderedComponent, RouteStub } from "hamur/test"
|
|
4
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal"
|
|
5
|
+
import {
|
|
6
|
+
mockComponent as mockComponentOf,
|
|
7
|
+
mount,
|
|
8
|
+
render,
|
|
9
|
+
type MountedOf,
|
|
10
|
+
type MountOptionsOf,
|
|
11
|
+
type RenderSuspendedOptionsOf,
|
|
12
|
+
} from "../mount.ts"
|
|
13
|
+
|
|
14
|
+
export type PropsOf<C> = LibraryProps<
|
|
15
|
+
C,
|
|
16
|
+
C extends JSXElementConstructor<any> ? ComponentProps<C> : Record<string, unknown>
|
|
17
|
+
>
|
|
18
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>
|
|
19
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>
|
|
20
|
+
|
|
21
|
+
const ENTRY = fileURLToPath(new URL("./mount-entry.ts", import.meta.url))
|
|
22
|
+
|
|
23
|
+
export function mountSuspended<C = unknown>(
|
|
24
|
+
component: string | RouteStub,
|
|
25
|
+
options?: MountOptions<C>,
|
|
26
|
+
): Promise<Mounted<C>> {
|
|
27
|
+
return mount("react", ENTRY, component, options)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function renderSuspended<C>(
|
|
31
|
+
component: C | string | RouteStub,
|
|
32
|
+
options?: RenderSuspendedOptionsOf<PropsOf<C>>,
|
|
33
|
+
): Promise<RenderedComponent> {
|
|
34
|
+
return render(component, options)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void> {
|
|
38
|
+
return mockComponentOf(path, replacement)
|
|
39
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createElement, useSyncExternalStore, type ComponentType } from "react"
|
|
2
|
+
|
|
3
|
+
interface Shown {
|
|
4
|
+
component: ComponentType<Record<string, unknown>> | undefined
|
|
5
|
+
props: Record<string, unknown>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let shown: Shown = { component: undefined, props: {} }
|
|
9
|
+
const listeners = new Set<() => void>()
|
|
10
|
+
const subscribe = (listener: () => void) => {
|
|
11
|
+
listeners.add(listener)
|
|
12
|
+
return () => void listeners.delete(listener)
|
|
13
|
+
}
|
|
14
|
+
const read = () => shown
|
|
15
|
+
|
|
16
|
+
function change(next: Shown): void {
|
|
17
|
+
shown = next
|
|
18
|
+
for (const listener of listeners) listener()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default function HamurTestMount() {
|
|
22
|
+
const { component, props } = useSyncExternalStore(subscribe, read, read)
|
|
23
|
+
return component ? createElement(component, props) : null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function show(component: unknown, props: Record<string, unknown>): void {
|
|
27
|
+
change({ component: component as Shown["component"], props })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function setProps(props: Record<string, unknown>): void {
|
|
31
|
+
change({ ...shown, props: { ...shown.props, ...props } })
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function unmount(): void {
|
|
35
|
+
change({ component: undefined, props: {} })
|
|
36
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url"
|
|
2
|
+
import type { Component } from "solid-js"
|
|
3
|
+
import type { ComponentProps } from "@solidjs/web"
|
|
4
|
+
import type { RenderedComponent, RouteStub } from "hamur/test"
|
|
5
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal"
|
|
6
|
+
import {
|
|
7
|
+
mockComponent as mockComponentOf,
|
|
8
|
+
mount,
|
|
9
|
+
render,
|
|
10
|
+
type MountedOf,
|
|
11
|
+
type MountOptionsOf,
|
|
12
|
+
type RenderSuspendedOptionsOf,
|
|
13
|
+
} from "../mount.ts"
|
|
14
|
+
|
|
15
|
+
export type PropsOf<C> = LibraryProps<C, C extends Component<any> ? ComponentProps<C> : Record<string, unknown>>
|
|
16
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>
|
|
17
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>
|
|
18
|
+
|
|
19
|
+
const ENTRY = fileURLToPath(new URL("./mount-entry.ts", import.meta.url))
|
|
20
|
+
|
|
21
|
+
export function mountSuspended<C = unknown>(
|
|
22
|
+
component: string | RouteStub,
|
|
23
|
+
options?: MountOptions<C>,
|
|
24
|
+
): Promise<Mounted<C>> {
|
|
25
|
+
return mount("solid", ENTRY, component, options)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function renderSuspended<C>(
|
|
29
|
+
component: C | string | RouteStub,
|
|
30
|
+
options?: RenderSuspendedOptionsOf<PropsOf<C>>,
|
|
31
|
+
): Promise<RenderedComponent> {
|
|
32
|
+
return render(component, options)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void> {
|
|
36
|
+
return mockComponentOf(path, replacement)
|
|
37
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createComponent, createMemo, createStore, untrack, type Component } from "solid-js"
|
|
2
|
+
import type { JSX } from "@solidjs/web"
|
|
3
|
+
|
|
4
|
+
interface Shown {
|
|
5
|
+
component: Component<Record<string, unknown>> | undefined
|
|
6
|
+
props: Record<string, unknown>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const [shown, setShown] = createStore<Shown>({ component: undefined, props: {} })
|
|
10
|
+
|
|
11
|
+
export default function HamurTestMount(): JSX.Element {
|
|
12
|
+
const component = createMemo(() => shown.component)
|
|
13
|
+
return (() => {
|
|
14
|
+
const View = component()
|
|
15
|
+
return View ? untrack(() => createComponent(View, shown.props)) : null
|
|
16
|
+
}) as unknown as JSX.Element
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function show(component: unknown, props: Record<string, unknown>): void {
|
|
20
|
+
setShown((draft) => {
|
|
21
|
+
draft.props = props
|
|
22
|
+
draft.component = component as Shown["component"]
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function setProps(props: Record<string, unknown>): void {
|
|
27
|
+
setShown((draft) => {
|
|
28
|
+
Object.assign(draft.props, props)
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function unmount(): void {
|
|
33
|
+
setShown((draft) => {
|
|
34
|
+
draft.component = undefined
|
|
35
|
+
})
|
|
36
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url"
|
|
2
|
+
import type { Component, ComponentProps } from "svelte"
|
|
3
|
+
import type { RenderedComponent, RouteStub } from "hamur/test"
|
|
4
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal"
|
|
5
|
+
import {
|
|
6
|
+
mockComponent as mockComponentOf,
|
|
7
|
+
mount,
|
|
8
|
+
render,
|
|
9
|
+
type MountedOf,
|
|
10
|
+
type MountOptionsOf,
|
|
11
|
+
type RenderSuspendedOptionsOf,
|
|
12
|
+
} from "../mount.ts"
|
|
13
|
+
|
|
14
|
+
export type PropsOf<C> = LibraryProps<C, C extends Component<any, any> ? ComponentProps<C> : Record<string, unknown>>
|
|
15
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>
|
|
16
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>
|
|
17
|
+
|
|
18
|
+
const ENTRY = fileURLToPath(new URL("./mount-entry.svelte", import.meta.url))
|
|
19
|
+
|
|
20
|
+
export function mountSuspended<C = unknown>(
|
|
21
|
+
component: string | RouteStub,
|
|
22
|
+
options?: MountOptions<C>,
|
|
23
|
+
): Promise<Mounted<C>> {
|
|
24
|
+
return mount("svelte", ENTRY, component, options)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function renderSuspended<C>(
|
|
28
|
+
component: C | string | RouteStub,
|
|
29
|
+
options?: RenderSuspendedOptionsOf<PropsOf<C>>,
|
|
30
|
+
): Promise<RenderedComponent> {
|
|
31
|
+
return render(component, options)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void> {
|
|
35
|
+
return mockComponentOf(path, replacement)
|
|
36
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script module>
|
|
2
|
+
const shown = $state({ component: undefined, props: {} })
|
|
3
|
+
|
|
4
|
+
export function show(component, props) {
|
|
5
|
+
Object.assign(shown.props, props)
|
|
6
|
+
shown.component = component
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function setProps(props) {
|
|
10
|
+
Object.assign(shown.props, props)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function unmount() {
|
|
14
|
+
shown.component = undefined
|
|
15
|
+
}
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
<script>
|
|
19
|
+
const View = $derived(shown.component)
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
{#if View}
|
|
23
|
+
<View {...shown.props} />
|
|
24
|
+
{/if}
|
package/src/vue/index.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url"
|
|
2
|
+
import type { RenderedComponent, RouteStub } from "hamur/test"
|
|
3
|
+
import type { PropsOf as LibraryProps } from "hamur/test/internal"
|
|
4
|
+
import {
|
|
5
|
+
mockComponent as mockComponentOf,
|
|
6
|
+
mount,
|
|
7
|
+
render,
|
|
8
|
+
type MountedOf,
|
|
9
|
+
type MountOptionsOf,
|
|
10
|
+
type RenderSuspendedOptionsOf,
|
|
11
|
+
} from "../mount.ts"
|
|
12
|
+
|
|
13
|
+
type ComponentProps<C> = C extends abstract new (...args: never) => { $props: infer P }
|
|
14
|
+
? P
|
|
15
|
+
: C extends (...args: infer A) => unknown
|
|
16
|
+
? A extends [(infer P)?, ...unknown[]]
|
|
17
|
+
? P
|
|
18
|
+
: Record<string, unknown>
|
|
19
|
+
: Record<string, unknown>
|
|
20
|
+
|
|
21
|
+
export type PropsOf<C> = LibraryProps<C, ComponentProps<C>>
|
|
22
|
+
export type MountOptions<C> = MountOptionsOf<PropsOf<C>>
|
|
23
|
+
export type Mounted<C> = MountedOf<PropsOf<C>>
|
|
24
|
+
|
|
25
|
+
const ENTRY = fileURLToPath(new URL("./mount-entry.ts", import.meta.url))
|
|
26
|
+
|
|
27
|
+
export function mountSuspended<C = unknown>(
|
|
28
|
+
component: string | RouteStub,
|
|
29
|
+
options?: MountOptions<C>,
|
|
30
|
+
): Promise<Mounted<C>> {
|
|
31
|
+
return mount("vue", ENTRY, component, options)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function renderSuspended<C>(
|
|
35
|
+
component: C | string | RouteStub,
|
|
36
|
+
options?: RenderSuspendedOptionsOf<PropsOf<C>>,
|
|
37
|
+
): Promise<RenderedComponent> {
|
|
38
|
+
return render(component, options)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function mockComponent(path: string, replacement: string | (() => unknown)): Promise<void> {
|
|
42
|
+
return mockComponentOf(path, replacement)
|
|
43
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { defineComponent, h, reactive, shallowRef, type Component } from "vue"
|
|
2
|
+
|
|
3
|
+
const target = shallowRef<Component | undefined>()
|
|
4
|
+
const props = reactive<Record<string, unknown>>({})
|
|
5
|
+
|
|
6
|
+
export default defineComponent({
|
|
7
|
+
name: "HamurTestMount",
|
|
8
|
+
inheritAttrs: false,
|
|
9
|
+
setup() {
|
|
10
|
+
return () => (target.value ? h(target.value, { ...props }) : null)
|
|
11
|
+
},
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
export function show(component: unknown, next: Record<string, unknown>): void {
|
|
15
|
+
Object.assign(props, next)
|
|
16
|
+
target.value = component as Component
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function setProps(next: Record<string, unknown>): void {
|
|
20
|
+
Object.assign(props, next)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function unmount(): void {
|
|
24
|
+
target.value = undefined
|
|
25
|
+
}
|