@object-ui/react 0.5.0 → 2.0.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 (65) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +13 -0
  3. package/dist/LazyPluginLoader.d.ts +44 -0
  4. package/dist/LazyPluginLoader.js +92 -3
  5. package/dist/components/form/FieldFactory.js +44 -40
  6. package/dist/components/form/FormRenderer.js +45 -3
  7. package/dist/context/ActionContext.d.ts +76 -0
  8. package/dist/context/ActionContext.js +139 -0
  9. package/dist/context/ThemeContext.d.ts +94 -0
  10. package/dist/context/ThemeContext.js +215 -0
  11. package/dist/context/index.d.ts +2 -0
  12. package/dist/context/index.js +2 -0
  13. package/dist/hooks/index.d.ts +5 -0
  14. package/dist/hooks/index.js +5 -0
  15. package/dist/hooks/useActionRunner.d.ts +21 -7
  16. package/dist/hooks/useActionRunner.js +27 -7
  17. package/dist/hooks/useDiscovery.d.ts +78 -0
  18. package/dist/hooks/useDiscovery.js +96 -0
  19. package/dist/hooks/useDynamicApp.d.ts +70 -0
  20. package/dist/hooks/useDynamicApp.js +92 -0
  21. package/dist/hooks/useNavigationOverlay.d.ts +78 -0
  22. package/dist/hooks/useNavigationOverlay.js +112 -0
  23. package/dist/hooks/usePageVariables.d.ts +67 -0
  24. package/dist/hooks/usePageVariables.js +109 -0
  25. package/dist/hooks/useViewData.d.ts +73 -0
  26. package/dist/hooks/useViewData.js +135 -0
  27. package/dist/index.d.ts +1 -0
  28. package/dist/index.js +2 -0
  29. package/dist/utils/i18n.d.ts +10 -0
  30. package/dist/utils/i18n.js +12 -0
  31. package/package.json +6 -4
  32. package/src/LazyPluginLoader.tsx +129 -7
  33. package/src/__tests__/LazyPluginLoader.test.tsx +97 -1
  34. package/src/__tests__/ThemeProvider.test.tsx +451 -0
  35. package/src/__tests__/useNavigationOverlay.test.ts +416 -0
  36. package/src/__tests__/usePageVariables.test.tsx +334 -0
  37. package/src/__tests__/useViewData.test.tsx +422 -0
  38. package/src/components/form/FieldFactory.tsx +51 -46
  39. package/src/components/form/FormRenderer.test.tsx +145 -0
  40. package/src/components/form/FormRenderer.tsx +52 -3
  41. package/src/context/ActionContext.tsx +213 -0
  42. package/src/context/ThemeContext.tsx +325 -0
  43. package/src/context/__tests__/ActionContext.test.tsx +174 -0
  44. package/src/context/index.ts +2 -0
  45. package/src/hooks/__tests__/useActionRunner.test.ts +153 -0
  46. package/src/hooks/__tests__/useDiscovery.test.tsx +149 -0
  47. package/src/hooks/__tests__/useDynamicApp.test.ts +161 -0
  48. package/src/hooks/__tests__/useExpression.test.ts +86 -0
  49. package/src/hooks/index.ts +5 -0
  50. package/src/hooks/useActionRunner.ts +49 -11
  51. package/src/hooks/useDiscovery.ts +141 -0
  52. package/src/hooks/useDynamicApp.ts +142 -0
  53. package/src/hooks/useNavigationOverlay.ts +180 -0
  54. package/src/hooks/usePageVariables.tsx +157 -0
  55. package/src/hooks/useViewData.ts +201 -0
  56. package/src/index.ts +21 -0
  57. package/src/utils/i18n.ts +10 -0
  58. package/tsconfig.json +1 -0
  59. package/tsconfig.tsbuildinfo +1 -1
  60. package/dist/__tests__/LazyPluginLoader.test.d.ts +0 -8
  61. package/dist/__tests__/LazyPluginLoader.test.js +0 -49
  62. package/dist/components/form/FormRenderer.test.d.ts +0 -8
  63. package/dist/components/form/FormRenderer.test.js +0 -534
  64. package/dist/index.test.d.ts +0 -8
  65. package/dist/index.test.js +0 -13
@@ -0,0 +1,334 @@
1
+ /**
2
+ * ObjectUI
3
+ * Copyright (c) 2024-present ObjectStack Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ import { describe, it, expect } from 'vitest';
10
+ import { renderHook, act } from '@testing-library/react';
11
+ import React from 'react';
12
+ import {
13
+ PageVariablesProvider,
14
+ usePageVariables,
15
+ useHasPageVariables,
16
+ } from '../hooks/usePageVariables';
17
+ import type { PageVariable } from '@object-ui/types';
18
+
19
+ // ---------------------------------------------------------------------------
20
+ // Helper: wrap hook in PageVariablesProvider
21
+ // ---------------------------------------------------------------------------
22
+
23
+ function createWrapper(definitions?: PageVariable[]) {
24
+ return ({ children }: { children: React.ReactNode }) => (
25
+ <PageVariablesProvider definitions={definitions}>
26
+ {children}
27
+ </PageVariablesProvider>
28
+ );
29
+ }
30
+
31
+ // ---------------------------------------------------------------------------
32
+ // useHasPageVariables
33
+ // ---------------------------------------------------------------------------
34
+
35
+ describe('useHasPageVariables', () => {
36
+ it('should return false when outside provider', () => {
37
+ const { result } = renderHook(() => useHasPageVariables());
38
+ expect(result.current).toBe(false);
39
+ });
40
+
41
+ it('should return true when inside provider', () => {
42
+ const { result } = renderHook(() => useHasPageVariables(), {
43
+ wrapper: createWrapper([]),
44
+ });
45
+ expect(result.current).toBe(true);
46
+ });
47
+ });
48
+
49
+ // ---------------------------------------------------------------------------
50
+ // usePageVariables — outside provider (graceful fallback)
51
+ // ---------------------------------------------------------------------------
52
+
53
+ describe('usePageVariables — outside provider', () => {
54
+ it('should return empty variables', () => {
55
+ const { result } = renderHook(() => usePageVariables());
56
+ expect(result.current.variables).toEqual({});
57
+ });
58
+
59
+ it('should provide no-op setVariable', () => {
60
+ const { result } = renderHook(() => usePageVariables());
61
+ // Should not throw
62
+ act(() => {
63
+ result.current.setVariable('x', 1);
64
+ });
65
+ expect(result.current.variables).toEqual({});
66
+ });
67
+
68
+ it('should provide no-op setVariables', () => {
69
+ const { result } = renderHook(() => usePageVariables());
70
+ act(() => {
71
+ result.current.setVariables({ a: 1, b: 2 });
72
+ });
73
+ expect(result.current.variables).toEqual({});
74
+ });
75
+
76
+ it('should provide no-op resetVariables', () => {
77
+ const { result } = renderHook(() => usePageVariables());
78
+ act(() => {
79
+ result.current.resetVariables();
80
+ });
81
+ expect(result.current.variables).toEqual({});
82
+ });
83
+ });
84
+
85
+ // ---------------------------------------------------------------------------
86
+ // Initialization from definitions
87
+ // ---------------------------------------------------------------------------
88
+
89
+ describe('usePageVariables — initialization', () => {
90
+ it('should initialize string variable with default', () => {
91
+ const defs: PageVariable[] = [
92
+ { name: 'query', type: 'string', defaultValue: 'hello' },
93
+ ];
94
+ const { result } = renderHook(() => usePageVariables(), {
95
+ wrapper: createWrapper(defs),
96
+ });
97
+ expect(result.current.variables.query).toBe('hello');
98
+ });
99
+
100
+ it('should initialize number variable with default', () => {
101
+ const defs: PageVariable[] = [
102
+ { name: 'count', type: 'number', defaultValue: 42 },
103
+ ];
104
+ const { result } = renderHook(() => usePageVariables(), {
105
+ wrapper: createWrapper(defs),
106
+ });
107
+ expect(result.current.variables.count).toBe(42);
108
+ });
109
+
110
+ it('should initialize boolean variable with default', () => {
111
+ const defs: PageVariable[] = [
112
+ { name: 'isOpen', type: 'boolean', defaultValue: true },
113
+ ];
114
+ const { result } = renderHook(() => usePageVariables(), {
115
+ wrapper: createWrapper(defs),
116
+ });
117
+ expect(result.current.variables.isOpen).toBe(true);
118
+ });
119
+
120
+ it('should initialize object variable with default', () => {
121
+ const defs: PageVariable[] = [
122
+ { name: 'filter', type: 'object', defaultValue: { status: 'active' } },
123
+ ];
124
+ const { result } = renderHook(() => usePageVariables(), {
125
+ wrapper: createWrapper(defs),
126
+ });
127
+ expect(result.current.variables.filter).toEqual({ status: 'active' });
128
+ });
129
+
130
+ it('should initialize array variable with default', () => {
131
+ const defs: PageVariable[] = [
132
+ { name: 'tags', type: 'array', defaultValue: ['a', 'b'] },
133
+ ];
134
+ const { result } = renderHook(() => usePageVariables(), {
135
+ wrapper: createWrapper(defs),
136
+ });
137
+ expect(result.current.variables.tags).toEqual(['a', 'b']);
138
+ });
139
+
140
+ it('should use type-appropriate defaults when no defaultValue', () => {
141
+ const defs: PageVariable[] = [
142
+ { name: 'str', type: 'string' },
143
+ { name: 'num', type: 'number' },
144
+ { name: 'bool', type: 'boolean' },
145
+ { name: 'obj', type: 'object' },
146
+ { name: 'arr', type: 'array' },
147
+ ];
148
+ const { result } = renderHook(() => usePageVariables(), {
149
+ wrapper: createWrapper(defs),
150
+ });
151
+ expect(result.current.variables.str).toBe('');
152
+ expect(result.current.variables.num).toBe(0);
153
+ expect(result.current.variables.bool).toBe(false);
154
+ expect(result.current.variables.obj).toEqual({});
155
+ expect(result.current.variables.arr).toEqual([]);
156
+ });
157
+
158
+ it('should default to empty string when type is undefined', () => {
159
+ const defs: PageVariable[] = [{ name: 'unknown' }];
160
+ const { result } = renderHook(() => usePageVariables(), {
161
+ wrapper: createWrapper(defs),
162
+ });
163
+ expect(result.current.variables.unknown).toBe('');
164
+ });
165
+
166
+ it('should initialize multiple variables', () => {
167
+ const defs: PageVariable[] = [
168
+ { name: 'name', type: 'string', defaultValue: 'Alice' },
169
+ { name: 'age', type: 'number', defaultValue: 30 },
170
+ { name: 'active', type: 'boolean', defaultValue: true },
171
+ ];
172
+ const { result } = renderHook(() => usePageVariables(), {
173
+ wrapper: createWrapper(defs),
174
+ });
175
+ expect(result.current.variables).toEqual({
176
+ name: 'Alice',
177
+ age: 30,
178
+ active: true,
179
+ });
180
+ });
181
+
182
+ it('should return empty variables when definitions is empty array', () => {
183
+ const { result } = renderHook(() => usePageVariables(), {
184
+ wrapper: createWrapper([]),
185
+ });
186
+ expect(result.current.variables).toEqual({});
187
+ });
188
+
189
+ it('should return empty variables when definitions is undefined', () => {
190
+ const { result } = renderHook(() => usePageVariables(), {
191
+ wrapper: createWrapper(undefined),
192
+ });
193
+ expect(result.current.variables).toEqual({});
194
+ });
195
+ });
196
+
197
+ // ---------------------------------------------------------------------------
198
+ // setVariable
199
+ // ---------------------------------------------------------------------------
200
+
201
+ describe('usePageVariables — setVariable', () => {
202
+ it('should update a single variable', () => {
203
+ const defs: PageVariable[] = [
204
+ { name: 'selected', type: 'string', defaultValue: '' },
205
+ ];
206
+ const { result } = renderHook(() => usePageVariables(), {
207
+ wrapper: createWrapper(defs),
208
+ });
209
+
210
+ act(() => {
211
+ result.current.setVariable('selected', 'item-1');
212
+ });
213
+ expect(result.current.variables.selected).toBe('item-1');
214
+ });
215
+
216
+ it('should not overwrite other variables', () => {
217
+ const defs: PageVariable[] = [
218
+ { name: 'a', type: 'string', defaultValue: 'alpha' },
219
+ { name: 'b', type: 'string', defaultValue: 'beta' },
220
+ ];
221
+ const { result } = renderHook(() => usePageVariables(), {
222
+ wrapper: createWrapper(defs),
223
+ });
224
+
225
+ act(() => {
226
+ result.current.setVariable('a', 'updated');
227
+ });
228
+ expect(result.current.variables.a).toBe('updated');
229
+ expect(result.current.variables.b).toBe('beta');
230
+ });
231
+
232
+ it('should allow setting a new key not in definitions', () => {
233
+ const defs: PageVariable[] = [
234
+ { name: 'known', type: 'string', defaultValue: 'x' },
235
+ ];
236
+ const { result } = renderHook(() => usePageVariables(), {
237
+ wrapper: createWrapper(defs),
238
+ });
239
+
240
+ act(() => {
241
+ result.current.setVariable('dynamic', 'value');
242
+ });
243
+ expect(result.current.variables.dynamic).toBe('value');
244
+ expect(result.current.variables.known).toBe('x');
245
+ });
246
+ });
247
+
248
+ // ---------------------------------------------------------------------------
249
+ // setVariables (batch)
250
+ // ---------------------------------------------------------------------------
251
+
252
+ describe('usePageVariables — setVariables', () => {
253
+ it('should update multiple variables at once', () => {
254
+ const defs: PageVariable[] = [
255
+ { name: 'x', type: 'number', defaultValue: 0 },
256
+ { name: 'y', type: 'number', defaultValue: 0 },
257
+ { name: 'z', type: 'number', defaultValue: 0 },
258
+ ];
259
+ const { result } = renderHook(() => usePageVariables(), {
260
+ wrapper: createWrapper(defs),
261
+ });
262
+
263
+ act(() => {
264
+ result.current.setVariables({ x: 10, y: 20 });
265
+ });
266
+ expect(result.current.variables).toEqual({ x: 10, y: 20, z: 0 });
267
+ });
268
+
269
+ it('should merge with existing variables', () => {
270
+ const defs: PageVariable[] = [
271
+ { name: 'name', type: 'string', defaultValue: 'Alice' },
272
+ { name: 'age', type: 'number', defaultValue: 25 },
273
+ ];
274
+ const { result } = renderHook(() => usePageVariables(), {
275
+ wrapper: createWrapper(defs),
276
+ });
277
+
278
+ act(() => {
279
+ result.current.setVariable('name', 'Bob');
280
+ });
281
+ act(() => {
282
+ result.current.setVariables({ age: 30 });
283
+ });
284
+ expect(result.current.variables).toEqual({ name: 'Bob', age: 30 });
285
+ });
286
+ });
287
+
288
+ // ---------------------------------------------------------------------------
289
+ // resetVariables
290
+ // ---------------------------------------------------------------------------
291
+
292
+ describe('usePageVariables — resetVariables', () => {
293
+ it('should reset all variables to defaults', () => {
294
+ const defs: PageVariable[] = [
295
+ { name: 'a', type: 'string', defaultValue: 'original' },
296
+ { name: 'b', type: 'number', defaultValue: 5 },
297
+ ];
298
+ const { result } = renderHook(() => usePageVariables(), {
299
+ wrapper: createWrapper(defs),
300
+ });
301
+
302
+ // Mutate
303
+ act(() => {
304
+ result.current.setVariables({ a: 'changed', b: 99 });
305
+ });
306
+ expect(result.current.variables).toEqual({ a: 'changed', b: 99 });
307
+
308
+ // Reset
309
+ act(() => {
310
+ result.current.resetVariables();
311
+ });
312
+ expect(result.current.variables).toEqual({ a: 'original', b: 5 });
313
+ });
314
+
315
+ it('should reset dynamically added variables', () => {
316
+ const defs: PageVariable[] = [
317
+ { name: 'x', type: 'number', defaultValue: 1 },
318
+ ];
319
+ const { result } = renderHook(() => usePageVariables(), {
320
+ wrapper: createWrapper(defs),
321
+ });
322
+
323
+ act(() => {
324
+ result.current.setVariable('extra', 'gone');
325
+ });
326
+ expect(result.current.variables.extra).toBe('gone');
327
+
328
+ act(() => {
329
+ result.current.resetVariables();
330
+ });
331
+ // 'extra' was not in definitions, so it's removed on reset
332
+ expect(result.current.variables).toEqual({ x: 1 });
333
+ });
334
+ });