@voltro/testing 0.51.0 → 0.53.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.
package/dist/client.d.ts CHANGED
@@ -1,9 +1,13 @@
1
1
  import { Effect } from 'effect';
2
2
  import { Exit } from 'effect/Exit';
3
+ import { FieldErrors } from '@voltro/client';
4
+ import { FormBinding } from '@voltro/client';
5
+ import { FormStateSnapshot } from '@voltro/client';
3
6
  import { ReactElement } from 'react';
4
7
  import { ReactNode } from 'react';
5
8
  import { RunForkOptions } from 'effect/Runtime';
6
9
  import { RuntimeFiber } from 'effect/Fiber';
10
+ import { UseFormBindingOptions } from '@voltro/client';
7
11
 
8
12
  /**
9
13
  * The runtime the hooks get. EXPORTED so it can be guarded.
@@ -33,6 +37,23 @@ export declare const FAKE_RUNTIME: {
33
37
  dispose: () => Effect.Effect<void, never, never>;
34
38
  };
35
39
 
40
+ export declare interface FormBindingHarness<Input extends Record<string, unknown>, Output> {
41
+ /** The live binding, as of the latest render. */
42
+ readonly binding: () => FormBinding<Input, Output>;
43
+ /** Set one field (dotted paths reach nested values), wrapped in `act`. */
44
+ readonly fill: (path: string, value: unknown) => void;
45
+ /** Blur one field — what reveals its error under the default timing. */
46
+ readonly blur: (path: string) => void;
47
+ /** Submit; resolves the output or `undefined` when blocked. */
48
+ readonly submit: () => Promise<Output | undefined>;
49
+ /** The VISIBLE errors right now. */
50
+ readonly errors: () => FieldErrors;
51
+ readonly state: () => FormStateSnapshot;
52
+ /** The underlying fake api — recorded `calls`, `setSubscription`, … */
53
+ readonly client: VoltroTestClient;
54
+ readonly unmount: () => void;
55
+ }
56
+
36
57
  declare type Handler = (input: never) => unknown;
37
58
 
38
59
  export declare const makeVoltroTestClient: (config?: VoltroTestClientConfig) => VoltroTestClient;
@@ -44,6 +65,40 @@ export declare interface RecordedCall {
44
65
  readonly input: unknown;
45
66
  }
46
67
 
68
+ /**
69
+ * Render `useFormBinding` against a fake api and hand back form-shaped
70
+ * controls:
71
+ *
72
+ * const form = await renderFormBinding('users.create', {
73
+ * binding: { schema: UsersCreateInput },
74
+ * mutation: (input) => {
75
+ * if ((input as { email: string }).email === 'taken@x.io') {
76
+ * throw new ValidationError({ field: 'email', message: 'validation.emailTaken' })
77
+ * }
78
+ * return { id: 'user_1' }
79
+ * },
80
+ * })
81
+ * form.fill('email', 'taken@x.io')
82
+ * await form.submit()
83
+ * expect(form.errors()['email']).toBe('validation.emailTaken')
84
+ */
85
+ export declare const renderFormBinding: <Input extends Record<string, unknown> = Record<string, unknown>, Output = unknown>(tag: string, options?: RenderFormBindingOptions<Input>) => Promise<FormBindingHarness<Input, Output>>;
86
+
87
+ export declare interface RenderFormBindingOptions<Input extends Record<string, unknown>> {
88
+ /** The api name the binding asks for. Default `'app'`. */
89
+ readonly apiName?: string;
90
+ /** The mutation handler for THIS tag. Return the output; throw a
91
+ * `ValidationError` / `ValidationErrors` to exercise server field-error
92
+ * routing; throw anything else for the loud-failure path. Default: echo
93
+ * `{ ok: true }`. */
94
+ readonly mutation?: (input: unknown) => unknown | Promise<unknown>;
95
+ /** Extra fake-api config (other mutations, subscriptions, actions). */
96
+ readonly client?: Omit<VoltroTestClientConfig, 'apiName'>;
97
+ /** Options for the binding itself — `schema` is usually the one you want
98
+ * (the fake api carries no descriptors). */
99
+ readonly binding?: UseFormBindingOptions<Input>;
100
+ }
101
+
47
102
  /**
48
103
  * The parts of a snapshot that are NOT the data.
49
104
  *
package/dist/client.js CHANGED
@@ -1,22 +1,61 @@
1
1
  import { Effect as e } from "effect";
2
- import { createElement as t } from "react";
3
- import { FrameworkRuntimesContext as n, RpcErrorBus as r } from "@voltro/client";
4
- //#region src/client.tsx
5
- var i = {
2
+ import { act as t, createElement as n } from "react";
3
+ import { FrameworkRuntimesContext as r, RpcErrorBus as i, useFormBinding as a } from "@voltro/client";
4
+ //#region src/formBindingHarness.tsx
5
+ var o = async (e, r = {}) => {
6
+ if (globalThis.document === void 0) throw Error("renderFormBinding needs a DOM to render hooks — add `// @vitest-environment jsdom` to the spec.");
7
+ let { createRoot: i } = await import("react-dom/client"), o = r.apiName ?? "app", s = c({
8
+ apiName: o,
9
+ ...r.client ?? {},
10
+ mutations: {
11
+ [e]: r.mutation ?? (() => ({ ok: !0 })),
12
+ ...r.client?.mutations ?? {}
13
+ }
14
+ }), l, u = () => (l = a(o, e, r.binding ?? {}), null), d = i(globalThis.document.createElement("div"));
15
+ return await t(async () => {
16
+ d.render(n(s.Provider, null, n(u)));
17
+ }), {
18
+ binding: () => l,
19
+ fill: (e, n) => {
20
+ t(() => {
21
+ l.field(e).setValue(n);
22
+ });
23
+ },
24
+ blur: (e) => {
25
+ t(() => {
26
+ l.field(e).onBlur();
27
+ });
28
+ },
29
+ submit: async () => {
30
+ let e;
31
+ return await t(async () => {
32
+ e = await l.submit();
33
+ }), e;
34
+ },
35
+ errors: () => l.errors,
36
+ state: () => l.state,
37
+ client: s,
38
+ unmount: () => {
39
+ t(() => {
40
+ d.unmount();
41
+ });
42
+ }
43
+ };
44
+ }, s = {
6
45
  runPromise: e.runPromise,
7
46
  runPromiseExit: e.runPromiseExit,
8
47
  runFork: e.runFork,
9
48
  dispose: () => e.void
10
- }, a = (a = {}) => {
11
- let o = a.apiName ?? "app", s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), u = [];
12
- for (let [e, t] of Object.entries(a.subscriptions ?? {})) s.set(e, {
13
- data: t,
49
+ }, c = (t = {}) => {
50
+ let a = t.apiName ?? "app", o = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map(), u = [];
51
+ for (let [e, n] of Object.entries(t.subscriptions ?? {})) o.set(e, {
52
+ data: n,
14
53
  error: void 0,
15
54
  revision: 0,
16
55
  pendingPatches: 0,
17
56
  emittedAt: void 0
18
57
  });
19
- let d = (e) => s.get(e) ?? {
58
+ let d = (e) => o.get(e) ?? {
20
59
  data: void 0,
21
60
  error: void 0,
22
61
  revision: -1,
@@ -62,27 +101,27 @@ var i = {
62
101
  catch: (e) => e
63
102
  });
64
103
  };
65
- for (let [e, t] of Object.entries(a.mutations ?? {})) g(e, "mutation", t);
66
- for (let [e, t] of Object.entries(a.actions ?? {})) g(e, "action", t);
104
+ for (let [e, n] of Object.entries(t.mutations ?? {})) g(e, "mutation", n);
105
+ for (let [e, n] of Object.entries(t.actions ?? {})) g(e, "action", n);
67
106
  let _ = {
68
- runtime: i,
107
+ runtime: s,
69
108
  cache: m,
70
109
  client: h,
71
110
  descriptors: {},
72
111
  inspectBaseUrl: "http://test.local",
73
- errorBus: new r()
112
+ errorBus: new i()
74
113
  }, v = {
75
114
  get: (e) => {
76
- if (e !== o) throw Error(`makeVoltroTestClient: component asked for api '${e}' but the harness was built for '${o}'. Pass { apiName: '${e}' }.`);
115
+ if (e !== a) throw Error(`makeVoltroTestClient: component asked for api '${e}' but the harness was built for '${a}'. Pass { apiName: '${e}' }.`);
77
116
  return _;
78
117
  },
79
- names: [o]
118
+ names: [a]
80
119
  };
81
120
  return {
82
- Provider: ({ children: e }) => t(n.Provider, { value: v }, e),
121
+ Provider: ({ children: e }) => n(r.Provider, { value: v }, e),
83
122
  setSubscription: (e, t, n) => {
84
123
  let r = d(e);
85
- s.set(e, {
124
+ o.set(e, {
86
125
  data: t,
87
126
  error: void 0,
88
127
  revision: r.revision + 1,
@@ -91,7 +130,7 @@ var i = {
91
130
  }), f(e);
92
131
  },
93
132
  failSubscription: (e, t) => {
94
- s.set(e, {
133
+ o.set(e, {
95
134
  data: void 0,
96
135
  error: t,
97
136
  revision: -1,
@@ -100,10 +139,10 @@ var i = {
100
139
  }), f(e);
101
140
  },
102
141
  resetSubscription: (e) => {
103
- s.delete(e), f(e);
142
+ o.delete(e), f(e);
104
143
  },
105
144
  calls: u
106
145
  };
107
146
  };
108
147
  //#endregion
109
- export { i as FAKE_RUNTIME, a as makeVoltroTestClient };
148
+ export { s as FAKE_RUNTIME, c as makeVoltroTestClient, o as renderFormBinding };