@object-ui/react 0.3.1 → 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 (69) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +13 -0
  3. package/dist/LazyPluginLoader.d.ts +82 -0
  4. package/dist/LazyPluginLoader.js +124 -0
  5. package/dist/SchemaRenderer.js +44 -12
  6. package/dist/components/form/FieldFactory.js +44 -40
  7. package/dist/components/form/FormRenderer.js +45 -3
  8. package/dist/context/ActionContext.d.ts +76 -0
  9. package/dist/context/ActionContext.js +139 -0
  10. package/dist/context/SchemaRendererContext.d.ts +2 -1
  11. package/dist/context/SchemaRendererContext.js +4 -2
  12. package/dist/context/ThemeContext.d.ts +94 -0
  13. package/dist/context/ThemeContext.js +215 -0
  14. package/dist/context/index.d.ts +2 -0
  15. package/dist/context/index.js +2 -0
  16. package/dist/hooks/index.d.ts +5 -0
  17. package/dist/hooks/index.js +5 -0
  18. package/dist/hooks/useActionRunner.d.ts +21 -7
  19. package/dist/hooks/useActionRunner.js +27 -7
  20. package/dist/hooks/useDiscovery.d.ts +78 -0
  21. package/dist/hooks/useDiscovery.js +96 -0
  22. package/dist/hooks/useDynamicApp.d.ts +70 -0
  23. package/dist/hooks/useDynamicApp.js +92 -0
  24. package/dist/hooks/useNavigationOverlay.d.ts +78 -0
  25. package/dist/hooks/useNavigationOverlay.js +112 -0
  26. package/dist/hooks/usePageVariables.d.ts +67 -0
  27. package/dist/hooks/usePageVariables.js +109 -0
  28. package/dist/hooks/useViewData.d.ts +73 -0
  29. package/dist/hooks/useViewData.js +135 -0
  30. package/dist/index.d.ts +2 -0
  31. package/dist/index.js +3 -0
  32. package/dist/utils/i18n.d.ts +10 -0
  33. package/dist/utils/i18n.js +12 -0
  34. package/package.json +16 -6
  35. package/src/LazyPluginLoader.tsx +174 -0
  36. package/src/SchemaRenderer.tsx +64 -12
  37. package/src/__tests__/LazyPluginLoader.test.tsx +176 -0
  38. package/src/__tests__/ThemeProvider.test.tsx +451 -0
  39. package/src/__tests__/useNavigationOverlay.test.ts +416 -0
  40. package/src/__tests__/usePageVariables.test.tsx +334 -0
  41. package/src/__tests__/useViewData.test.tsx +422 -0
  42. package/src/components/form/FieldFactory.tsx +51 -46
  43. package/src/components/form/FormRenderer.test.tsx +145 -0
  44. package/src/components/form/FormRenderer.tsx +52 -3
  45. package/src/context/ActionContext.tsx +213 -0
  46. package/src/context/SchemaRendererContext.tsx +5 -2
  47. package/src/context/ThemeContext.tsx +325 -0
  48. package/src/context/__tests__/ActionContext.test.tsx +174 -0
  49. package/src/context/index.ts +2 -0
  50. package/src/hooks/__tests__/useActionRunner.test.ts +153 -0
  51. package/src/hooks/__tests__/useDiscovery.test.tsx +149 -0
  52. package/src/hooks/__tests__/useDynamicApp.test.ts +161 -0
  53. package/src/hooks/__tests__/useExpression.test.ts +86 -0
  54. package/src/hooks/index.ts +5 -0
  55. package/src/hooks/useActionRunner.ts +49 -11
  56. package/src/hooks/useDiscovery.ts +141 -0
  57. package/src/hooks/useDynamicApp.ts +142 -0
  58. package/src/hooks/useNavigationOverlay.ts +180 -0
  59. package/src/hooks/usePageVariables.tsx +157 -0
  60. package/src/hooks/useViewData.ts +201 -0
  61. package/src/index.ts +22 -0
  62. package/src/utils/i18n.ts +10 -0
  63. package/tsconfig.json +1 -0
  64. package/tsconfig.tsbuildinfo +1 -1
  65. package/vitest.config.ts +2 -0
  66. package/dist/components/form/FormRenderer.test.d.ts +0 -8
  67. package/dist/components/form/FormRenderer.test.js +0 -534
  68. package/dist/index.test.d.ts +0 -8
  69. package/dist/index.test.js +0 -13
@@ -0,0 +1,174 @@
1
+ /**
2
+ * Tests for ActionContext — ActionProvider, useAction, useHasActionProvider
3
+ */
4
+
5
+ import { describe, it, expect, vi } from 'vitest';
6
+ import { renderHook, waitFor, act } from '@testing-library/react';
7
+ import React from 'react';
8
+ import { ActionProvider, useAction, useHasActionProvider } from '../ActionContext';
9
+
10
+ describe('ActionContext', () => {
11
+ describe('useAction without ActionProvider', () => {
12
+ it('falls back to local ActionRunner with default state', () => {
13
+ const { result } = renderHook(() => useAction());
14
+
15
+ expect(result.current.loading).toBe(false);
16
+ expect(result.current.error).toBeNull();
17
+ expect(result.current.result).toBeNull();
18
+ expect(typeof result.current.execute).toBe('function');
19
+ expect(typeof result.current.executeChain).toBe('function');
20
+ expect(typeof result.current.updateContext).toBe('function');
21
+ expect(result.current.runner).toBeDefined();
22
+ });
23
+ });
24
+
25
+ describe('useHasActionProvider', () => {
26
+ it('returns false without provider', () => {
27
+ const { result } = renderHook(() => useHasActionProvider());
28
+
29
+ expect(result.current).toBe(false);
30
+ });
31
+
32
+ it('returns true with provider', () => {
33
+ const wrapper = ({ children }: { children: React.ReactNode }) =>
34
+ React.createElement(ActionProvider, { context: {} }, children);
35
+
36
+ const { result } = renderHook(() => useHasActionProvider(), { wrapper });
37
+
38
+ expect(result.current).toBe(true);
39
+ });
40
+ });
41
+
42
+ describe('ActionProvider basic', () => {
43
+ it('children can access execute function', () => {
44
+ const wrapper = ({ children }: { children: React.ReactNode }) =>
45
+ React.createElement(ActionProvider, { context: {} }, children);
46
+
47
+ const { result } = renderHook(() => useAction(), { wrapper });
48
+
49
+ expect(typeof result.current.execute).toBe('function');
50
+ expect(typeof result.current.executeChain).toBe('function');
51
+ expect(typeof result.current.updateContext).toBe('function');
52
+ expect(result.current.loading).toBe(false);
53
+ expect(result.current.error).toBeNull();
54
+ expect(result.current.result).toBeNull();
55
+ });
56
+ });
57
+
58
+ describe('ActionProvider execute', () => {
59
+ it('executes a toast action and returns result', async () => {
60
+ const wrapper = ({ children }: { children: React.ReactNode }) =>
61
+ React.createElement(ActionProvider, { context: {} }, children);
62
+
63
+ const { result } = renderHook(() => useAction(), { wrapper });
64
+
65
+ let actionResult: any;
66
+ await act(async () => {
67
+ actionResult = await result.current.execute({
68
+ type: 'script',
69
+ execute: 'true',
70
+ toast: { showOnSuccess: true },
71
+ successMessage: 'Done!',
72
+ });
73
+ });
74
+
75
+ expect(actionResult.success).toBe(true);
76
+
77
+ await waitFor(() => {
78
+ expect(result.current.loading).toBe(false);
79
+ });
80
+
81
+ expect(result.current.result).toEqual(actionResult);
82
+ expect(result.current.error).toBeNull();
83
+ });
84
+ });
85
+
86
+ describe('ActionProvider execute error', () => {
87
+ it('executes an action that fails and sets error state', async () => {
88
+ const wrapper = ({ children }: { children: React.ReactNode }) =>
89
+ React.createElement(ActionProvider, { context: {} }, children);
90
+
91
+ const { result } = renderHook(() => useAction(), { wrapper });
92
+
93
+ let actionResult: any;
94
+ await act(async () => {
95
+ actionResult = await result.current.execute({
96
+ onClick: vi.fn().mockRejectedValue(new Error('boom')),
97
+ });
98
+ });
99
+
100
+ await waitFor(() => {
101
+ expect(result.current.loading).toBe(false);
102
+ });
103
+
104
+ expect(actionResult.success).toBe(false);
105
+ expect(result.current.error).toBeTruthy();
106
+ expect(result.current.result?.success).toBe(false);
107
+ });
108
+ });
109
+
110
+ describe('ActionProvider executeChain', () => {
111
+ it('executes a chain of actions', async () => {
112
+ const wrapper = ({ children }: { children: React.ReactNode }) =>
113
+ React.createElement(ActionProvider, { context: {} }, children);
114
+
115
+ const { result } = renderHook(() => useAction(), { wrapper });
116
+
117
+ let chainResult: any;
118
+ await act(async () => {
119
+ chainResult = await result.current.executeChain([
120
+ { type: 'script', execute: 'true' },
121
+ { type: 'script', execute: 'true' },
122
+ ]);
123
+ });
124
+
125
+ await waitFor(() => {
126
+ expect(result.current.loading).toBe(false);
127
+ });
128
+
129
+ expect(chainResult.success).toBe(true);
130
+ expect(result.current.result).toEqual(chainResult);
131
+ expect(result.current.error).toBeNull();
132
+ });
133
+ });
134
+
135
+ describe('ActionProvider updateContext', () => {
136
+ it('updateContext is callable', () => {
137
+ const wrapper = ({ children }: { children: React.ReactNode }) =>
138
+ React.createElement(ActionProvider, { context: {} }, children);
139
+
140
+ const { result } = renderHook(() => useAction(), { wrapper });
141
+
142
+ expect(() => {
143
+ result.current.updateContext({ record: { id: 1 } });
144
+ }).not.toThrow();
145
+ });
146
+ });
147
+
148
+ describe('ActionProvider with handlers', () => {
149
+ it('invokes onToast handler when a toast action is executed', async () => {
150
+ const onToast = vi.fn();
151
+ const onNavigate = vi.fn();
152
+
153
+ const wrapper = ({ children }: { children: React.ReactNode }) =>
154
+ React.createElement(
155
+ ActionProvider,
156
+ { context: {}, onToast, onNavigate },
157
+ children,
158
+ );
159
+
160
+ const { result } = renderHook(() => useAction(), { wrapper });
161
+
162
+ await act(async () => {
163
+ await result.current.execute({
164
+ type: 'script',
165
+ execute: 'true',
166
+ toast: { showOnSuccess: true },
167
+ successMessage: 'Saved!',
168
+ });
169
+ });
170
+
171
+ expect(onToast).toHaveBeenCalledWith('Saved!', expect.objectContaining({ type: 'success' }));
172
+ });
173
+ });
174
+ });
@@ -7,3 +7,5 @@
7
7
  */
8
8
 
9
9
  export * from './SchemaRendererContext';
10
+ export * from './ActionContext';
11
+ export * from './ThemeContext';
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Tests for useActionRunner hook
3
+ */
4
+
5
+ import { describe, it, expect, vi } from 'vitest';
6
+ import { renderHook, waitFor, act } from '@testing-library/react';
7
+ import { useActionRunner } from '../useActionRunner';
8
+
9
+ describe('useActionRunner', () => {
10
+ it('returns initial state with loading=false, error=null, result=null', () => {
11
+ const { result } = renderHook(() => useActionRunner());
12
+
13
+ expect(result.current.loading).toBe(false);
14
+ expect(result.current.error).toBeNull();
15
+ expect(result.current.result).toBeNull();
16
+ });
17
+
18
+ it('exposes runner, execute, and updateContext', () => {
19
+ const { result } = renderHook(() => useActionRunner());
20
+
21
+ expect(result.current.runner).toBeDefined();
22
+ expect(typeof result.current.execute).toBe('function');
23
+ expect(typeof result.current.updateContext).toBe('function');
24
+ });
25
+
26
+ it('handles options with context key format', () => {
27
+ const { result } = renderHook(() =>
28
+ useActionRunner({ context: { data: { name: 'test' } } }),
29
+ );
30
+
31
+ expect(result.current.runner).toBeDefined();
32
+ expect(result.current.loading).toBe(false);
33
+ });
34
+
35
+ it('handles plain context object format (backwards compat)', () => {
36
+ const { result } = renderHook(() =>
37
+ useActionRunner({ data: { name: 'test' } }),
38
+ );
39
+
40
+ expect(result.current.runner).toBeDefined();
41
+ expect(result.current.loading).toBe(false);
42
+ });
43
+
44
+ it('sets loading=true during execution and loading=false after', async () => {
45
+ let resolveAction: () => void;
46
+ const pendingAction = new Promise<void>((resolve) => {
47
+ resolveAction = resolve;
48
+ });
49
+
50
+ const { result } = renderHook(() => useActionRunner());
51
+
52
+ let executePromise: Promise<unknown>;
53
+ act(() => {
54
+ executePromise = result.current.execute({
55
+ onClick: () => pendingAction,
56
+ });
57
+ });
58
+
59
+ await waitFor(() => {
60
+ expect(result.current.loading).toBe(true);
61
+ });
62
+
63
+ await act(async () => {
64
+ resolveAction!();
65
+ await executePromise;
66
+ });
67
+
68
+ expect(result.current.loading).toBe(false);
69
+ });
70
+
71
+ it('sets result on successful action execution', async () => {
72
+ const onNavigate = vi.fn();
73
+ const { result } = renderHook(() =>
74
+ useActionRunner({ context: {}, onNavigate }),
75
+ );
76
+
77
+ await act(async () => {
78
+ await result.current.execute({
79
+ type: 'navigation',
80
+ navigate: { to: '/dashboard' },
81
+ });
82
+ });
83
+
84
+ expect(result.current.result).toEqual({ success: true });
85
+ expect(result.current.error).toBeNull();
86
+ expect(result.current.loading).toBe(false);
87
+ expect(onNavigate).toHaveBeenCalledWith(
88
+ '/dashboard',
89
+ expect.objectContaining({ external: false }),
90
+ );
91
+ });
92
+
93
+ it('sets error on failed action execution (catch path)', async () => {
94
+ const { result } = renderHook(() => useActionRunner());
95
+
96
+ await act(async () => {
97
+ await result.current.execute({
98
+ onClick: () => {
99
+ throw new Error('Something went wrong');
100
+ },
101
+ });
102
+ });
103
+
104
+ expect(result.current.error).toBe('Something went wrong');
105
+ expect(result.current.result).toEqual({
106
+ success: false,
107
+ error: 'Something went wrong',
108
+ });
109
+ expect(result.current.loading).toBe(false);
110
+ });
111
+
112
+ it('updateContext calls runner.updateContext', () => {
113
+ const { result } = renderHook(() => useActionRunner());
114
+
115
+ const spy = vi.spyOn(result.current.runner, 'updateContext');
116
+
117
+ act(() => {
118
+ result.current.updateContext({ data: { id: 42 } });
119
+ });
120
+
121
+ expect(spy).toHaveBeenCalledWith({ data: { id: 42 } });
122
+ });
123
+
124
+ it('accepts handler options (onToast, onNavigate, etc.)', async () => {
125
+ const onToast = vi.fn();
126
+ const onNavigate = vi.fn();
127
+
128
+ const { result } = renderHook(() =>
129
+ useActionRunner({
130
+ context: {},
131
+ onToast,
132
+ onNavigate,
133
+ }),
134
+ );
135
+
136
+ await act(async () => {
137
+ await result.current.execute({
138
+ type: 'navigation',
139
+ navigate: { to: '/settings' },
140
+ successMessage: 'Navigated!',
141
+ });
142
+ });
143
+
144
+ expect(onNavigate).toHaveBeenCalledWith(
145
+ '/settings',
146
+ expect.objectContaining({ external: false }),
147
+ );
148
+ expect(onToast).toHaveBeenCalledWith(
149
+ 'Navigated!',
150
+ expect.objectContaining({ type: 'success' }),
151
+ );
152
+ });
153
+ });
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Tests for useDiscovery hook
3
+ */
4
+
5
+ import React from 'react';
6
+ import { describe, it, expect, vi } from 'vitest';
7
+ import { renderHook, waitFor } from '@testing-library/react';
8
+ import { useDiscovery } from '../useDiscovery';
9
+ import { SchemaRendererContext } from '../../context/SchemaRendererContext';
10
+
11
+ function createWrapper(dataSource: any) {
12
+ return ({ children }: { children: React.ReactNode }) =>
13
+ React.createElement(
14
+ SchemaRendererContext.Provider,
15
+ { value: { dataSource } },
16
+ children,
17
+ );
18
+ }
19
+
20
+ describe('useDiscovery', () => {
21
+ it('returns isLoading false and null discovery when no context is provided', async () => {
22
+ const { result } = renderHook(() => useDiscovery());
23
+
24
+ await waitFor(() => {
25
+ expect(result.current.isLoading).toBe(false);
26
+ });
27
+
28
+ expect(result.current.discovery).toBeNull();
29
+ expect(result.current.error).toBeNull();
30
+ });
31
+
32
+ it('returns isLoading false and null discovery when dataSource has no getDiscovery method', async () => {
33
+ const dataSource = { someOtherMethod: vi.fn() };
34
+
35
+ const { result } = renderHook(() => useDiscovery(), {
36
+ wrapper: createWrapper(dataSource),
37
+ });
38
+
39
+ await waitFor(() => {
40
+ expect(result.current.isLoading).toBe(false);
41
+ });
42
+
43
+ expect(result.current.discovery).toBeNull();
44
+ expect(result.current.error).toBeNull();
45
+ });
46
+
47
+ it('successfully fetches discovery data from dataSource.getDiscovery()', async () => {
48
+ const discoveryData = {
49
+ name: 'test-server',
50
+ version: '1.0.0',
51
+ services: {
52
+ auth: { enabled: true, status: 'available' as const },
53
+ data: { enabled: true, status: 'available' as const },
54
+ },
55
+ };
56
+
57
+ const dataSource = {
58
+ getDiscovery: vi.fn().mockResolvedValue(discoveryData),
59
+ };
60
+
61
+ const { result } = renderHook(() => useDiscovery(), {
62
+ wrapper: createWrapper(dataSource),
63
+ });
64
+
65
+ expect(result.current.isLoading).toBe(true);
66
+
67
+ await waitFor(() => {
68
+ expect(result.current.isLoading).toBe(false);
69
+ });
70
+
71
+ expect(result.current.discovery).toEqual(discoveryData);
72
+ expect(result.current.error).toBeNull();
73
+ expect(dataSource.getDiscovery).toHaveBeenCalled();
74
+ });
75
+
76
+ it('handles errors from getDiscovery gracefully', async () => {
77
+ const dataSource = {
78
+ getDiscovery: vi.fn().mockRejectedValue(new Error('Network error')),
79
+ };
80
+
81
+ const { result } = renderHook(() => useDiscovery(), {
82
+ wrapper: createWrapper(dataSource),
83
+ });
84
+
85
+ await waitFor(() => {
86
+ expect(result.current.isLoading).toBe(false);
87
+ });
88
+
89
+ expect(result.current.discovery).toBeNull();
90
+ expect(result.current.error?.message).toBe('Network error');
91
+ });
92
+
93
+ it('isAuthEnabled defaults to true when no discovery', async () => {
94
+ const { result } = renderHook(() => useDiscovery());
95
+
96
+ await waitFor(() => {
97
+ expect(result.current.isLoading).toBe(false);
98
+ });
99
+
100
+ expect(result.current.isAuthEnabled).toBe(true);
101
+ });
102
+
103
+ it('isAuthEnabled reflects the auth service enabled state', async () => {
104
+ const discoveryData = {
105
+ services: {
106
+ auth: { enabled: false },
107
+ },
108
+ };
109
+
110
+ const dataSource = {
111
+ getDiscovery: vi.fn().mockResolvedValue(discoveryData),
112
+ };
113
+
114
+ const { result } = renderHook(() => useDiscovery(), {
115
+ wrapper: createWrapper(dataSource),
116
+ });
117
+
118
+ await waitFor(() => {
119
+ expect(result.current.isLoading).toBe(false);
120
+ });
121
+
122
+ expect(result.current.isAuthEnabled).toBe(false);
123
+ });
124
+
125
+ it('cleans up on unmount (cancelled flag)', async () => {
126
+ let resolveDiscovery: (value: any) => void;
127
+ const discoveryPromise = new Promise((resolve) => {
128
+ resolveDiscovery = resolve;
129
+ });
130
+
131
+ const dataSource = {
132
+ getDiscovery: vi.fn().mockReturnValue(discoveryPromise),
133
+ };
134
+
135
+ const { result, unmount } = renderHook(() => useDiscovery(), {
136
+ wrapper: createWrapper(dataSource),
137
+ });
138
+
139
+ expect(result.current.isLoading).toBe(true);
140
+
141
+ unmount();
142
+
143
+ // Resolve after unmount — state should not update
144
+ resolveDiscovery!({ name: 'late-response' });
145
+
146
+ // Discovery should remain null since the component was unmounted
147
+ expect(result.current.discovery).toBeNull();
148
+ });
149
+ });
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Tests for useDynamicApp hook
3
+ */
4
+
5
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
6
+ import { renderHook, waitFor, act } from '@testing-library/react';
7
+ import { useDynamicApp } from '../useDynamicApp';
8
+
9
+ describe('useDynamicApp', () => {
10
+ const staticConfig = {
11
+ name: 'crm',
12
+ label: 'CRM App',
13
+ objects: ['contact', 'account'],
14
+ };
15
+
16
+ it('returns static config when no adapter is provided', () => {
17
+ const { result } = renderHook(() =>
18
+ useDynamicApp({ appId: 'crm', staticConfig }),
19
+ );
20
+
21
+ expect(result.current.config).toEqual(staticConfig);
22
+ expect(result.current.isLoading).toBe(false);
23
+ expect(result.current.isServerConfig).toBe(false);
24
+ });
25
+
26
+ it('returns null config when neither adapter nor static config provided', () => {
27
+ const { result } = renderHook(() =>
28
+ useDynamicApp({ appId: 'crm' }),
29
+ );
30
+
31
+ expect(result.current.config).toBeNull();
32
+ expect(result.current.isLoading).toBe(false);
33
+ });
34
+
35
+ it('loads config from server adapter', async () => {
36
+ const serverConfig = { name: 'crm', label: 'CRM (Server)', objects: ['contact'] };
37
+ const adapter = {
38
+ getApp: vi.fn().mockResolvedValue(serverConfig),
39
+ };
40
+
41
+ const { result } = renderHook(() =>
42
+ useDynamicApp({ appId: 'crm', staticConfig, adapter }),
43
+ );
44
+
45
+ // Initially loading
46
+ expect(result.current.isLoading).toBe(true);
47
+
48
+ await waitFor(() => {
49
+ expect(result.current.isLoading).toBe(false);
50
+ });
51
+
52
+ expect(result.current.config).toEqual(serverConfig);
53
+ expect(result.current.isServerConfig).toBe(true);
54
+ expect(adapter.getApp).toHaveBeenCalledWith('crm');
55
+ });
56
+
57
+ it('falls back to static config when server returns null', async () => {
58
+ const adapter = {
59
+ getApp: vi.fn().mockResolvedValue(null),
60
+ };
61
+
62
+ const { result } = renderHook(() =>
63
+ useDynamicApp({ appId: 'crm', staticConfig, adapter }),
64
+ );
65
+
66
+ await waitFor(() => {
67
+ expect(result.current.isLoading).toBe(false);
68
+ });
69
+
70
+ expect(result.current.config).toEqual(staticConfig);
71
+ expect(result.current.isServerConfig).toBe(false);
72
+ });
73
+
74
+ it('falls back to static config on server error', async () => {
75
+ const adapter = {
76
+ getApp: vi.fn().mockRejectedValue(new Error('Network error')),
77
+ };
78
+
79
+ const { result } = renderHook(() =>
80
+ useDynamicApp({ appId: 'crm', staticConfig, adapter }),
81
+ );
82
+
83
+ await waitFor(() => {
84
+ expect(result.current.isLoading).toBe(false);
85
+ });
86
+
87
+ expect(result.current.config).toEqual(staticConfig);
88
+ expect(result.current.isServerConfig).toBe(false);
89
+ expect(result.current.error?.message).toBe('Network error');
90
+ });
91
+
92
+ it('skips loading when enabled is false', () => {
93
+ const adapter = {
94
+ getApp: vi.fn(),
95
+ };
96
+
97
+ const { result } = renderHook(() =>
98
+ useDynamicApp({ appId: 'crm', staticConfig, adapter, enabled: false }),
99
+ );
100
+
101
+ expect(result.current.config).toEqual(staticConfig);
102
+ expect(result.current.isLoading).toBe(false);
103
+ expect(adapter.getApp).not.toHaveBeenCalled();
104
+ });
105
+
106
+ it('refresh invalidates cache and re-fetches', async () => {
107
+ let callCount = 0;
108
+ const adapter = {
109
+ getApp: vi.fn().mockImplementation(async () => {
110
+ callCount++;
111
+ return { name: 'crm', version: callCount };
112
+ }),
113
+ invalidateCache: vi.fn(),
114
+ };
115
+
116
+ const { result } = renderHook(() =>
117
+ useDynamicApp({ appId: 'crm', adapter }),
118
+ );
119
+
120
+ await waitFor(() => {
121
+ expect(result.current.isLoading).toBe(false);
122
+ });
123
+
124
+ expect((result.current.config as any).version).toBe(1);
125
+
126
+ await act(async () => {
127
+ await result.current.refresh();
128
+ });
129
+
130
+ expect(adapter.invalidateCache).toHaveBeenCalledWith('app:crm');
131
+ expect((result.current.config as any).version).toBe(2);
132
+ });
133
+
134
+ it('reloads when appId changes', async () => {
135
+ const adapter = {
136
+ getApp: vi.fn().mockImplementation(async (appId: string) => {
137
+ return { name: appId };
138
+ }),
139
+ };
140
+
141
+ const { result, rerender } = renderHook(
142
+ ({ appId }: { appId: string }) =>
143
+ useDynamicApp({ appId, adapter }),
144
+ { initialProps: { appId: 'crm' } },
145
+ );
146
+
147
+ await waitFor(() => {
148
+ expect(result.current.isLoading).toBe(false);
149
+ });
150
+
151
+ expect((result.current.config as any).name).toBe('crm');
152
+
153
+ rerender({ appId: 'erp' });
154
+
155
+ await waitFor(() => {
156
+ expect((result.current.config as any).name).toBe('erp');
157
+ });
158
+
159
+ expect(adapter.getApp).toHaveBeenCalledWith('erp');
160
+ });
161
+ });