@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,451 @@
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 React from 'react';
10
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
11
+ import { renderHook, act } from '@testing-library/react';
12
+ import type { Theme } from '@object-ui/types';
13
+ import {
14
+ ThemeProvider,
15
+ useTheme,
16
+ useOptionalTheme,
17
+ } from '../context/ThemeContext';
18
+
19
+ // ============================================================================
20
+ // Fixtures
21
+ // ============================================================================
22
+
23
+ const lightTheme: Theme = {
24
+ name: 'light',
25
+ label: 'Light',
26
+ mode: 'light',
27
+ colors: {
28
+ primary: '#3B82F6',
29
+ secondary: '#64748B',
30
+ background: '#FFFFFF',
31
+ text: '#0F172A',
32
+ error: '#EF4444',
33
+ },
34
+ typography: {
35
+ fontFamily: { base: 'Inter, sans-serif' },
36
+ fontSize: { base: '1rem' },
37
+ },
38
+ borderRadius: { base: '0.25rem', lg: '0.5rem' },
39
+ };
40
+
41
+ const darkTheme: Theme = {
42
+ name: 'dark',
43
+ label: 'Dark',
44
+ mode: 'dark',
45
+ colors: {
46
+ primary: '#60A5FA',
47
+ secondary: '#94A3B8',
48
+ background: '#0F172A',
49
+ text: '#F1F5F9',
50
+ error: '#FCA5A5',
51
+ },
52
+ };
53
+
54
+ const childTheme: Theme = {
55
+ name: 'corporate',
56
+ label: 'Corporate',
57
+ colors: { primary: '#1E40AF' },
58
+ extends: 'light',
59
+ };
60
+
61
+ function createWrapper(props: Omit<React.ComponentProps<typeof ThemeProvider>, 'children'>) {
62
+ return function Wrapper({ children }: { children: React.ReactNode }) {
63
+ return <ThemeProvider {...props}>{children}</ThemeProvider>;
64
+ };
65
+ }
66
+
67
+ // ============================================================================
68
+ // Setup
69
+ // ============================================================================
70
+
71
+ beforeEach(() => {
72
+ // Clean DOM state
73
+ document.documentElement.style.cssText = '';
74
+ document.documentElement.className = '';
75
+ localStorage.clear();
76
+ });
77
+
78
+ afterEach(() => {
79
+ document.documentElement.style.cssText = '';
80
+ document.documentElement.className = '';
81
+ localStorage.clear();
82
+ });
83
+
84
+ // ============================================================================
85
+ // useTheme — basic context
86
+ // ============================================================================
87
+
88
+ describe('useTheme', () => {
89
+ it('should throw when used outside ThemeProvider', () => {
90
+ expect(() => {
91
+ renderHook(() => useTheme());
92
+ }).toThrow('useTheme must be used within a <ThemeProvider>');
93
+ });
94
+
95
+ it('should return theme context when inside ThemeProvider', () => {
96
+ const { result } = renderHook(() => useTheme(), {
97
+ wrapper: createWrapper({ themes: [lightTheme] }),
98
+ });
99
+
100
+ expect(result.current.theme).toBeTruthy();
101
+ expect(result.current.theme?.name).toBe('light');
102
+ expect(result.current.activeTheme).toBe('light');
103
+ });
104
+
105
+ it('should default to first theme when no defaultTheme', () => {
106
+ const { result } = renderHook(() => useTheme(), {
107
+ wrapper: createWrapper({ themes: [darkTheme, lightTheme] }),
108
+ });
109
+
110
+ expect(result.current.activeTheme).toBe('dark');
111
+ });
112
+
113
+ it('should use defaultTheme when provided', () => {
114
+ const { result } = renderHook(() => useTheme(), {
115
+ wrapper: createWrapper({ themes: [lightTheme, darkTheme], defaultTheme: 'dark' }),
116
+ });
117
+
118
+ expect(result.current.activeTheme).toBe('dark');
119
+ });
120
+
121
+ it('should return empty state when no themes', () => {
122
+ const { result } = renderHook(() => useTheme(), {
123
+ wrapper: createWrapper({ themes: [] }),
124
+ });
125
+
126
+ expect(result.current.theme).toBeNull();
127
+ expect(result.current.activeTheme).toBeNull();
128
+ expect(result.current.cssVars).toEqual({});
129
+ });
130
+ });
131
+
132
+ // ============================================================================
133
+ // useOptionalTheme
134
+ // ============================================================================
135
+
136
+ describe('useOptionalTheme', () => {
137
+ it('should return null outside ThemeProvider', () => {
138
+ const { result } = renderHook(() => useOptionalTheme());
139
+ expect(result.current).toBeNull();
140
+ });
141
+
142
+ it('should return context inside ThemeProvider', () => {
143
+ const { result } = renderHook(() => useOptionalTheme(), {
144
+ wrapper: createWrapper({ themes: [lightTheme] }),
145
+ });
146
+
147
+ expect(result.current).toBeTruthy();
148
+ expect(result.current?.theme?.name).toBe('light');
149
+ });
150
+ });
151
+
152
+ // ============================================================================
153
+ // Theme switching
154
+ // ============================================================================
155
+
156
+ describe('Theme switching', () => {
157
+ it('should switch theme via setTheme', () => {
158
+ const { result } = renderHook(() => useTheme(), {
159
+ wrapper: createWrapper({ themes: [lightTheme, darkTheme], defaultTheme: 'light' }),
160
+ });
161
+
162
+ expect(result.current.activeTheme).toBe('light');
163
+
164
+ act(() => {
165
+ result.current.setTheme('dark');
166
+ });
167
+
168
+ expect(result.current.activeTheme).toBe('dark');
169
+ expect(result.current.theme?.name).toBe('dark');
170
+ });
171
+
172
+ it('should not switch to nonexistent theme', () => {
173
+ const { result } = renderHook(() => useTheme(), {
174
+ wrapper: createWrapper({ themes: [lightTheme], defaultTheme: 'light' }),
175
+ });
176
+
177
+ act(() => {
178
+ result.current.setTheme('nonexistent');
179
+ });
180
+
181
+ expect(result.current.activeTheme).toBe('light');
182
+ });
183
+
184
+ it('should update CSS variables when theme changes', () => {
185
+ const { result } = renderHook(() => useTheme(), {
186
+ wrapper: createWrapper({ themes: [lightTheme, darkTheme], defaultTheme: 'light' }),
187
+ });
188
+
189
+ const lightPrimary = result.current.cssVars['--primary'];
190
+
191
+ act(() => {
192
+ result.current.setTheme('dark');
193
+ });
194
+
195
+ const darkPrimary = result.current.cssVars['--primary'];
196
+ expect(darkPrimary).not.toBe(lightPrimary);
197
+ });
198
+ });
199
+
200
+ // ============================================================================
201
+ // Mode management
202
+ // ============================================================================
203
+
204
+ describe('Mode management', () => {
205
+ it('should default mode to auto', () => {
206
+ const { result } = renderHook(() => useTheme(), {
207
+ wrapper: createWrapper({ themes: [lightTheme] }),
208
+ });
209
+
210
+ expect(result.current.mode).toBe('auto');
211
+ });
212
+
213
+ it('should use defaultMode when provided', () => {
214
+ const { result } = renderHook(() => useTheme(), {
215
+ wrapper: createWrapper({ themes: [lightTheme], defaultMode: 'dark' }),
216
+ });
217
+
218
+ expect(result.current.mode).toBe('dark');
219
+ expect(result.current.resolvedMode).toBe('dark');
220
+ });
221
+
222
+ it('should switch mode via setMode', () => {
223
+ const { result } = renderHook(() => useTheme(), {
224
+ wrapper: createWrapper({ themes: [lightTheme], defaultMode: 'light' }),
225
+ });
226
+
227
+ expect(result.current.resolvedMode).toBe('light');
228
+
229
+ act(() => {
230
+ result.current.setMode('dark');
231
+ });
232
+
233
+ expect(result.current.mode).toBe('dark');
234
+ expect(result.current.resolvedMode).toBe('dark');
235
+ });
236
+
237
+ it('should resolve auto mode based on system preference', () => {
238
+ // Mock system dark mode
239
+ const original = window.matchMedia;
240
+ window.matchMedia = vi.fn().mockImplementation((query: string) => ({
241
+ matches: query === '(prefers-color-scheme: dark)',
242
+ media: query,
243
+ addEventListener: vi.fn(),
244
+ removeEventListener: vi.fn(),
245
+ }));
246
+
247
+ const { result } = renderHook(() => useTheme(), {
248
+ wrapper: createWrapper({ themes: [lightTheme], defaultMode: 'auto' }),
249
+ });
250
+
251
+ expect(result.current.resolvedMode).toBe('dark');
252
+
253
+ window.matchMedia = original;
254
+ });
255
+ });
256
+
257
+ // ============================================================================
258
+ // CSS variable injection
259
+ // ============================================================================
260
+
261
+ describe('CSS variable injection', () => {
262
+ it('should inject CSS variables into documentElement', () => {
263
+ renderHook(() => useTheme(), {
264
+ wrapper: createWrapper({ themes: [lightTheme] }),
265
+ });
266
+
267
+ const root = document.documentElement;
268
+ expect(root.style.getPropertyValue('--primary')).toBeTruthy();
269
+ });
270
+
271
+ it('should inject mode class on documentElement', () => {
272
+ renderHook(() => useTheme(), {
273
+ wrapper: createWrapper({ themes: [lightTheme], defaultMode: 'dark' }),
274
+ });
275
+
276
+ expect(document.documentElement.classList.contains('dark')).toBe(true);
277
+ });
278
+
279
+ it('should inject into custom target element', () => {
280
+ const target = document.createElement('div');
281
+ document.body.appendChild(target);
282
+
283
+ renderHook(() => useTheme(), {
284
+ wrapper: createWrapper({ themes: [lightTheme], target }),
285
+ });
286
+
287
+ expect(target.style.getPropertyValue('--primary')).toBeTruthy();
288
+
289
+ document.body.removeChild(target);
290
+ });
291
+
292
+ it('should clean up CSS variables on unmount', () => {
293
+ const { unmount } = renderHook(() => useTheme(), {
294
+ wrapper: createWrapper({ themes: [lightTheme] }),
295
+ });
296
+
297
+ const root = document.documentElement;
298
+ expect(root.style.getPropertyValue('--primary')).toBeTruthy();
299
+
300
+ unmount();
301
+
302
+ expect(root.style.getPropertyValue('--primary')).toBe('');
303
+ });
304
+
305
+ it('should include typography and radius variables', () => {
306
+ renderHook(() => useTheme(), {
307
+ wrapper: createWrapper({ themes: [lightTheme] }),
308
+ });
309
+
310
+ const root = document.documentElement;
311
+ expect(root.style.getPropertyValue('--font-sans')).toBe('Inter, sans-serif');
312
+ expect(root.style.getPropertyValue('--radius')).toBe('0.25rem');
313
+ expect(root.style.getPropertyValue('--radius-lg')).toBe('0.5rem');
314
+ });
315
+ });
316
+
317
+ // ============================================================================
318
+ // Theme inheritance
319
+ // ============================================================================
320
+
321
+ describe('Theme inheritance', () => {
322
+ it('should resolve theme.extends', () => {
323
+ const { result } = renderHook(() => useTheme(), {
324
+ wrapper: createWrapper({
325
+ themes: [lightTheme, childTheme],
326
+ defaultTheme: 'corporate',
327
+ }),
328
+ });
329
+
330
+ // Child overrides primary
331
+ expect(result.current.theme?.colors.primary).toBe('#1E40AF');
332
+ // Inherits from parent
333
+ expect(result.current.theme?.colors.secondary).toBe('#64748B');
334
+ expect(result.current.theme?.colors.background).toBe('#FFFFFF');
335
+ expect(result.current.theme?.typography?.fontFamily?.base).toBe('Inter, sans-serif');
336
+ });
337
+ });
338
+
339
+ // ============================================================================
340
+ // Persistence
341
+ // ============================================================================
342
+
343
+ describe('Persistence', () => {
344
+ it('should persist theme name to localStorage', () => {
345
+ renderHook(() => useTheme(), {
346
+ wrapper: createWrapper({
347
+ themes: [lightTheme, darkTheme],
348
+ defaultTheme: 'light',
349
+ persist: true,
350
+ }),
351
+ });
352
+
353
+ expect(localStorage.getItem('objectui-theme-name')).toBe('light');
354
+ });
355
+
356
+ it('should persist mode to localStorage', () => {
357
+ renderHook(() => useTheme(), {
358
+ wrapper: createWrapper({
359
+ themes: [lightTheme],
360
+ defaultMode: 'dark',
361
+ persist: true,
362
+ }),
363
+ });
364
+
365
+ expect(localStorage.getItem('objectui-theme-mode')).toBe('dark');
366
+ });
367
+
368
+ it('should restore theme from localStorage', () => {
369
+ localStorage.setItem('objectui-theme-name', 'dark');
370
+ localStorage.setItem('objectui-theme-mode', 'dark');
371
+
372
+ const { result } = renderHook(() => useTheme(), {
373
+ wrapper: createWrapper({
374
+ themes: [lightTheme, darkTheme],
375
+ defaultTheme: 'light',
376
+ defaultMode: 'light',
377
+ persist: true,
378
+ }),
379
+ });
380
+
381
+ expect(result.current.activeTheme).toBe('dark');
382
+ expect(result.current.mode).toBe('dark');
383
+ });
384
+
385
+ it('should use custom storageKey', () => {
386
+ renderHook(() => useTheme(), {
387
+ wrapper: createWrapper({
388
+ themes: [lightTheme],
389
+ persist: true,
390
+ storageKey: 'myapp-theme',
391
+ }),
392
+ });
393
+
394
+ expect(localStorage.getItem('myapp-theme-name')).toBe('light');
395
+ expect(localStorage.getItem('myapp-theme-mode')).toBeTruthy();
396
+ });
397
+
398
+ it('should not persist when persist=false', () => {
399
+ renderHook(() => useTheme(), {
400
+ wrapper: createWrapper({
401
+ themes: [lightTheme],
402
+ persist: false,
403
+ }),
404
+ });
405
+
406
+ expect(localStorage.getItem('objectui-theme-name')).toBeNull();
407
+ });
408
+
409
+ it('should update persistence when theme changes', () => {
410
+ const { result } = renderHook(() => useTheme(), {
411
+ wrapper: createWrapper({
412
+ themes: [lightTheme, darkTheme],
413
+ defaultTheme: 'light',
414
+ persist: true,
415
+ }),
416
+ });
417
+
418
+ act(() => {
419
+ result.current.setTheme('dark');
420
+ });
421
+
422
+ expect(localStorage.getItem('objectui-theme-name')).toBe('dark');
423
+ });
424
+ });
425
+
426
+ // ============================================================================
427
+ // Generated CSS vars object
428
+ // ============================================================================
429
+
430
+ describe('cssVars output', () => {
431
+ it('should expose cssVars in the context', () => {
432
+ const { result } = renderHook(() => useTheme(), {
433
+ wrapper: createWrapper({ themes: [lightTheme] }),
434
+ });
435
+
436
+ expect(result.current.cssVars).toBeDefined();
437
+ expect(result.current.cssVars['--primary']).toBeTruthy();
438
+ expect(result.current.cssVars['--foreground']).toBeTruthy();
439
+ expect(result.current.cssVars['--font-sans']).toBe('Inter, sans-serif');
440
+ });
441
+
442
+ it('should provide themes list', () => {
443
+ const { result } = renderHook(() => useTheme(), {
444
+ wrapper: createWrapper({ themes: [lightTheme, darkTheme] }),
445
+ });
446
+
447
+ expect(result.current.themes).toHaveLength(2);
448
+ expect(result.current.themes[0].name).toBe('light');
449
+ expect(result.current.themes[1].name).toBe('dark');
450
+ });
451
+ });