@object-ui/react 0.5.0 → 3.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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +32 -0
- package/dist/LazyPluginLoader.d.ts +44 -0
- package/dist/LazyPluginLoader.js +92 -3
- package/dist/SchemaRenderer.d.ts +20 -1
- package/dist/SchemaRenderer.js +76 -13
- package/dist/components/form/FieldFactory.js +44 -40
- package/dist/components/form/FormRenderer.js +45 -3
- package/dist/context/ActionContext.d.ts +76 -0
- package/dist/context/ActionContext.js +139 -0
- package/dist/context/DndContext.d.ts +135 -0
- package/dist/context/DndContext.js +120 -0
- package/dist/context/NotificationContext.d.ts +113 -0
- package/dist/context/NotificationContext.js +130 -0
- package/dist/context/ThemeContext.d.ts +94 -0
- package/dist/context/ThemeContext.js +215 -0
- package/dist/context/index.d.ts +4 -0
- package/dist/context/index.js +4 -0
- package/dist/hooks/index.d.ts +22 -0
- package/dist/hooks/index.js +22 -0
- package/dist/hooks/useActionRunner.d.ts +21 -7
- package/dist/hooks/useActionRunner.js +27 -7
- package/dist/hooks/useAnimation.d.ts +46 -0
- package/dist/hooks/useAnimation.js +59 -0
- package/dist/hooks/useClientNotifications.d.ts +53 -0
- package/dist/hooks/useClientNotifications.js +190 -0
- package/dist/hooks/useColumnSummary.d.ts +38 -0
- package/dist/hooks/useColumnSummary.js +54 -0
- package/dist/hooks/useCrudShortcuts.d.ts +39 -0
- package/dist/hooks/useCrudShortcuts.js +96 -0
- package/dist/hooks/useDensityMode.d.ts +48 -0
- package/dist/hooks/useDensityMode.js +60 -0
- package/dist/hooks/useDiscovery.d.ts +78 -0
- package/dist/hooks/useDiscovery.js +96 -0
- package/dist/hooks/useDynamicApp.d.ts +70 -0
- package/dist/hooks/useDynamicApp.js +92 -0
- package/dist/hooks/useETagCache.d.ts +75 -0
- package/dist/hooks/useETagCache.js +306 -0
- package/dist/hooks/useFocusManagement.d.ts +89 -0
- package/dist/hooks/useFocusManagement.js +188 -0
- package/dist/hooks/useFocusTrap.d.ts +41 -0
- package/dist/hooks/useFocusTrap.js +103 -0
- package/dist/hooks/useKeyboardShortcuts.d.ts +49 -0
- package/dist/hooks/useKeyboardShortcuts.js +86 -0
- package/dist/hooks/useNavigationOverlay.d.ts +78 -0
- package/dist/hooks/useNavigationOverlay.js +112 -0
- package/dist/hooks/useOffline.d.ts +96 -0
- package/dist/hooks/useOffline.js +185 -0
- package/dist/hooks/usePageTransition.d.ts +60 -0
- package/dist/hooks/usePageTransition.js +111 -0
- package/dist/hooks/usePageVariables.d.ts +67 -0
- package/dist/hooks/usePageVariables.js +109 -0
- package/dist/hooks/usePerformance.d.ts +87 -0
- package/dist/hooks/usePerformance.js +165 -0
- package/dist/hooks/usePerformanceBudget.d.ts +78 -0
- package/dist/hooks/usePerformanceBudget.js +209 -0
- package/dist/hooks/useReducedMotion.d.ts +28 -0
- package/dist/hooks/useReducedMotion.js +48 -0
- package/dist/hooks/useSchemaPersistence.d.ts +87 -0
- package/dist/hooks/useSchemaPersistence.js +173 -0
- package/dist/hooks/useViewData.d.ts +73 -0
- package/dist/hooks/useViewData.js +135 -0
- package/dist/hooks/useViewSharing.d.ts +57 -0
- package/dist/hooks/useViewSharing.js +74 -0
- package/dist/hooks/useViewTransition.d.ts +54 -0
- package/dist/hooks/useViewTransition.js +83 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/utils/i18n.d.ts +10 -0
- package/dist/utils/i18n.js +12 -0
- package/package.json +7 -5
- package/src/LazyPluginLoader.tsx +129 -7
- package/src/SchemaRenderer.tsx +122 -13
- package/src/__tests__/LazyPluginLoader.test.tsx +97 -1
- package/src/__tests__/SchemaRenderer.aria.test.tsx +118 -0
- package/src/__tests__/SchemaRenderer.errorBoundary.test.tsx +114 -0
- package/src/__tests__/SchemaRendererProvider.smoke.test.tsx +132 -0
- package/src/__tests__/ThemeProvider.test.tsx +451 -0
- package/src/__tests__/useNavigationOverlay.test.ts +416 -0
- package/src/__tests__/useOffline.test.ts +198 -0
- package/src/__tests__/usePageTransition.test.ts +214 -0
- package/src/__tests__/usePageVariables.test.tsx +334 -0
- package/src/__tests__/usePerformance.test.ts +137 -0
- package/src/__tests__/useViewData.test.tsx +422 -0
- package/src/__tests__/view-switching.test.tsx +456 -0
- package/src/components/form/FieldFactory.tsx +51 -46
- package/src/components/form/FormRenderer.test.tsx +145 -0
- package/src/components/form/FormRenderer.tsx +52 -3
- package/src/context/ActionContext.tsx +213 -0
- package/src/context/DndContext.tsx +260 -0
- package/src/context/NotificationContext.tsx +280 -0
- package/src/context/ThemeContext.tsx +325 -0
- package/src/context/__tests__/ActionContext.test.tsx +174 -0
- package/src/context/__tests__/DndContext.test.tsx +150 -0
- package/src/context/__tests__/NotificationContext.test.tsx +176 -0
- package/src/context/index.ts +4 -0
- package/src/hooks/__tests__/useActionRunner.test.ts +153 -0
- package/src/hooks/__tests__/useAnimation.test.ts +82 -0
- package/src/hooks/__tests__/useColumnSummary.test.ts +93 -0
- package/src/hooks/__tests__/useDensityMode.test.ts +81 -0
- package/src/hooks/__tests__/useDiscovery.test.tsx +149 -0
- package/src/hooks/__tests__/useDynamicApp.test.ts +161 -0
- package/src/hooks/__tests__/useETagCache.test.ts +153 -0
- package/src/hooks/__tests__/useExpression.test.ts +86 -0
- package/src/hooks/__tests__/useFocusTrap.test.ts +47 -0
- package/src/hooks/__tests__/useKeyboardShortcuts.test.ts +144 -0
- package/src/hooks/__tests__/usePerformanceBudget.test.ts +157 -0
- package/src/hooks/__tests__/useSchemaPersistence.test.ts +220 -0
- package/src/hooks/__tests__/useViewSharing.test.ts +121 -0
- package/src/hooks/__tests__/useViewTransition.test.ts +61 -0
- package/src/hooks/index.ts +22 -1
- package/src/hooks/useActionRunner.ts +49 -11
- package/src/hooks/useAnimation.ts +117 -0
- package/src/hooks/useClientNotifications.ts +251 -0
- package/src/hooks/useColumnSummary.ts +87 -0
- package/src/hooks/useCrudShortcuts.ts +126 -0
- package/src/hooks/useDensityMode.ts +102 -0
- package/src/hooks/useDiscovery.ts +141 -0
- package/src/hooks/useDynamicApp.ts +142 -0
- package/src/hooks/useETagCache.ts +404 -0
- package/src/hooks/useFocusManagement.ts +248 -0
- package/src/hooks/useFocusTrap.ts +138 -0
- package/src/hooks/useKeyboardShortcuts.ts +119 -0
- package/src/hooks/useNavigationOverlay.ts +180 -0
- package/src/hooks/useOffline.ts +301 -0
- package/src/hooks/usePageTransition.ts +187 -0
- package/src/hooks/usePageVariables.tsx +157 -0
- package/src/hooks/usePerformance.ts +246 -0
- package/src/hooks/usePerformanceBudget.ts +313 -0
- package/src/hooks/useReducedMotion.ts +53 -0
- package/src/hooks/useSchemaPersistence.ts +222 -0
- package/src/hooks/useViewData.ts +201 -0
- package/src/hooks/useViewSharing.ts +140 -0
- package/src/hooks/useViewTransition.ts +144 -0
- package/src/index.ts +21 -0
- package/src/utils/i18n.ts +10 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/__tests__/LazyPluginLoader.test.d.ts +0 -8
- package/dist/__tests__/LazyPluginLoader.test.js +0 -49
- package/dist/components/form/FormRenderer.test.d.ts +0 -8
- package/dist/components/form/FormRenderer.test.js +0 -534
- package/dist/index.test.d.ts +0 -8
- package/dist/index.test.js +0 -13
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI — usePageTransition Tests
|
|
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, vi, beforeEach, afterEach } from 'vitest';
|
|
10
|
+
import { renderHook } from '@testing-library/react';
|
|
11
|
+
import { usePageTransition } from '../hooks/usePageTransition';
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// matchMedia mock helper
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
function mockMatchMedia(reducedMotion: boolean) {
|
|
18
|
+
const listeners: Array<(e: MediaQueryListEvent) => void> = [];
|
|
19
|
+
const mql = {
|
|
20
|
+
matches: reducedMotion,
|
|
21
|
+
media: '(prefers-reduced-motion: reduce)',
|
|
22
|
+
addEventListener: (_: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
23
|
+
listeners.push(cb);
|
|
24
|
+
},
|
|
25
|
+
removeEventListener: (_: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
26
|
+
const idx = listeners.indexOf(cb);
|
|
27
|
+
if (idx >= 0) listeners.splice(idx, 1);
|
|
28
|
+
},
|
|
29
|
+
addListener: vi.fn(),
|
|
30
|
+
removeListener: vi.fn(),
|
|
31
|
+
onchange: null,
|
|
32
|
+
dispatchEvent: vi.fn(),
|
|
33
|
+
} as unknown as MediaQueryList;
|
|
34
|
+
|
|
35
|
+
Object.defineProperty(window, 'matchMedia', {
|
|
36
|
+
writable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
value: vi.fn().mockReturnValue(mql),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return { mql, listeners };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
// Tests
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
describe('usePageTransition', () => {
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
mockMatchMedia(false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
vi.restoreAllMocks();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should return inactive result for default (none) type', () => {
|
|
58
|
+
const { result } = renderHook(() => usePageTransition());
|
|
59
|
+
|
|
60
|
+
expect(result.current.isActive).toBe(false);
|
|
61
|
+
expect(result.current.type).toBe('none');
|
|
62
|
+
expect(result.current.enterClassName).toBe('');
|
|
63
|
+
expect(result.current.exitClassName).toBe('');
|
|
64
|
+
expect(result.current.enterStyle).toEqual({});
|
|
65
|
+
expect(result.current.exitStyle).toEqual({});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should generate fade transition classes', () => {
|
|
69
|
+
const { result } = renderHook(() =>
|
|
70
|
+
usePageTransition({ type: 'fade', duration: 200 }),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
expect(result.current.isActive).toBe(true);
|
|
74
|
+
expect(result.current.enterClassName).toContain('animate-in');
|
|
75
|
+
expect(result.current.enterClassName).toContain('fade-in');
|
|
76
|
+
expect(result.current.exitClassName).toContain('animate-out');
|
|
77
|
+
expect(result.current.exitClassName).toContain('fade-out');
|
|
78
|
+
expect(result.current.enterStyle.animationDuration).toBe('200ms');
|
|
79
|
+
expect(result.current.duration).toBe(200);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should generate slide_up transition classes', () => {
|
|
83
|
+
const { result } = renderHook(() =>
|
|
84
|
+
usePageTransition({ type: 'slide_up' }),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
expect(result.current.isActive).toBe(true);
|
|
88
|
+
expect(result.current.enterClassName).toContain('slide-in-from-bottom');
|
|
89
|
+
expect(result.current.exitClassName).toContain('slide-out-to-top');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should generate slide_down transition classes', () => {
|
|
93
|
+
const { result } = renderHook(() =>
|
|
94
|
+
usePageTransition({ type: 'slide_down' }),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
expect(result.current.enterClassName).toContain('slide-in-from-top');
|
|
98
|
+
expect(result.current.exitClassName).toContain('slide-out-to-bottom');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should generate slide_left transition classes', () => {
|
|
102
|
+
const { result } = renderHook(() =>
|
|
103
|
+
usePageTransition({ type: 'slide_left' }),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
expect(result.current.enterClassName).toContain('slide-in-from-right');
|
|
107
|
+
expect(result.current.exitClassName).toContain('slide-out-to-left');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should generate slide_right transition classes', () => {
|
|
111
|
+
const { result } = renderHook(() =>
|
|
112
|
+
usePageTransition({ type: 'slide_right' }),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
expect(result.current.enterClassName).toContain('slide-in-from-left');
|
|
116
|
+
expect(result.current.exitClassName).toContain('slide-out-to-right');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should generate scale transition classes', () => {
|
|
120
|
+
const { result } = renderHook(() =>
|
|
121
|
+
usePageTransition({ type: 'scale' }),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expect(result.current.enterClassName).toContain('zoom-in');
|
|
125
|
+
expect(result.current.exitClassName).toContain('zoom-out');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should generate rotate transition classes', () => {
|
|
129
|
+
const { result } = renderHook(() =>
|
|
130
|
+
usePageTransition({ type: 'rotate' }),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
expect(result.current.enterClassName).toContain('spin-in');
|
|
134
|
+
expect(result.current.exitClassName).toContain('spin-out');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('should generate flip transition classes', () => {
|
|
138
|
+
const { result } = renderHook(() =>
|
|
139
|
+
usePageTransition({ type: 'flip' }),
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(result.current.isActive).toBe(true);
|
|
143
|
+
expect(result.current.enterClassName).toContain('fade-in');
|
|
144
|
+
expect(result.current.exitClassName).toContain('fade-out');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should apply custom easing', () => {
|
|
148
|
+
const { result } = renderHook(() =>
|
|
149
|
+
usePageTransition({ type: 'fade', easing: 'spring' }),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
expect(result.current.enterStyle.animationTimingFunction).toBe(
|
|
153
|
+
'cubic-bezier(0.34, 1.56, 0.64, 1)',
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should apply custom duration', () => {
|
|
158
|
+
const { result } = renderHook(() =>
|
|
159
|
+
usePageTransition({ type: 'fade', duration: 500 }),
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
expect(result.current.enterStyle.animationDuration).toBe('500ms');
|
|
163
|
+
expect(result.current.duration).toBe(500);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('should use default duration of 300ms', () => {
|
|
167
|
+
const { result } = renderHook(() =>
|
|
168
|
+
usePageTransition({ type: 'fade' }),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
expect(result.current.duration).toBe(300);
|
|
172
|
+
expect(result.current.enterStyle.animationDuration).toBe('300ms');
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('should disable transitions when reduced motion is preferred', () => {
|
|
176
|
+
mockMatchMedia(true);
|
|
177
|
+
|
|
178
|
+
const { result } = renderHook(() =>
|
|
179
|
+
usePageTransition({ type: 'fade', duration: 200 }),
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
expect(result.current.isActive).toBe(false);
|
|
183
|
+
expect(result.current.enterClassName).toBe('');
|
|
184
|
+
expect(result.current.exitClassName).toBe('');
|
|
185
|
+
expect(result.current.enterStyle).toEqual({});
|
|
186
|
+
expect(result.current.exitStyle).toEqual({});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('should set animationFillMode to both', () => {
|
|
190
|
+
const { result } = renderHook(() =>
|
|
191
|
+
usePageTransition({ type: 'fade' }),
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
expect(result.current.enterStyle.animationFillMode).toBe('both');
|
|
195
|
+
expect(result.current.exitStyle.animationFillMode).toBe('both');
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('should support all easing values', () => {
|
|
199
|
+
const easings = [
|
|
200
|
+
{ input: 'linear' as const, expected: 'linear' },
|
|
201
|
+
{ input: 'ease' as const, expected: 'ease' },
|
|
202
|
+
{ input: 'ease_in' as const, expected: 'ease-in' },
|
|
203
|
+
{ input: 'ease_out' as const, expected: 'ease-out' },
|
|
204
|
+
{ input: 'ease_in_out' as const, expected: 'ease-in-out' },
|
|
205
|
+
];
|
|
206
|
+
|
|
207
|
+
for (const { input, expected } of easings) {
|
|
208
|
+
const { result } = renderHook(() =>
|
|
209
|
+
usePageTransition({ type: 'fade', easing: input }),
|
|
210
|
+
);
|
|
211
|
+
expect(result.current.enterStyle.animationTimingFunction).toBe(expected);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
});
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI — usePerformance Tests
|
|
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, vi, afterEach } from 'vitest';
|
|
10
|
+
import { renderHook, act } from '@testing-library/react';
|
|
11
|
+
import { usePerformance } from '../hooks/usePerformance';
|
|
12
|
+
|
|
13
|
+
describe('usePerformance', () => {
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
vi.restoreAllMocks();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should return default config values', () => {
|
|
19
|
+
const { result } = renderHook(() => usePerformance());
|
|
20
|
+
|
|
21
|
+
expect(result.current.config).toEqual({
|
|
22
|
+
lazyLoad: true,
|
|
23
|
+
cacheStrategy: 'stale-while-revalidate',
|
|
24
|
+
prefetch: false,
|
|
25
|
+
pageSize: 50,
|
|
26
|
+
debounceMs: 300,
|
|
27
|
+
virtualScroll: {
|
|
28
|
+
enabled: false,
|
|
29
|
+
itemHeight: 40,
|
|
30
|
+
overscan: 5,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should merge user config with defaults', () => {
|
|
36
|
+
const { result } = renderHook(() =>
|
|
37
|
+
usePerformance({
|
|
38
|
+
lazyLoad: false,
|
|
39
|
+
pageSize: 100,
|
|
40
|
+
virtualScroll: { enabled: true, itemHeight: 60 },
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
expect(result.current.config.lazyLoad).toBe(false);
|
|
45
|
+
expect(result.current.config.pageSize).toBe(100);
|
|
46
|
+
expect(result.current.config.virtualScroll.enabled).toBe(true);
|
|
47
|
+
expect(result.current.config.virtualScroll.itemHeight).toBe(60);
|
|
48
|
+
// Default overscan preserved
|
|
49
|
+
expect(result.current.config.virtualScroll.overscan).toBe(5);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should accept all cache strategy values', () => {
|
|
53
|
+
for (const strategy of [
|
|
54
|
+
'none',
|
|
55
|
+
'cache-first',
|
|
56
|
+
'network-first',
|
|
57
|
+
'stale-while-revalidate',
|
|
58
|
+
] as const) {
|
|
59
|
+
const { result } = renderHook(() =>
|
|
60
|
+
usePerformance({ cacheStrategy: strategy }),
|
|
61
|
+
);
|
|
62
|
+
expect(result.current.config.cacheStrategy).toBe(strategy);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should track render count via markRenderStart', () => {
|
|
67
|
+
const { result } = renderHook(() => usePerformance());
|
|
68
|
+
|
|
69
|
+
// Initial render count should be 0 (only increments when markRenderStart stop is called)
|
|
70
|
+
expect(result.current.metrics.renderCount).toBe(0);
|
|
71
|
+
|
|
72
|
+
// After calling markRenderStart and its stop function, count should increment
|
|
73
|
+
act(() => {
|
|
74
|
+
const stop = result.current.markRenderStart();
|
|
75
|
+
stop();
|
|
76
|
+
});
|
|
77
|
+
expect(result.current.metrics.renderCount).toBeGreaterThanOrEqual(1);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should provide markRenderStart that returns stop function', () => {
|
|
81
|
+
const { result } = renderHook(() => usePerformance());
|
|
82
|
+
|
|
83
|
+
let stop: () => void;
|
|
84
|
+
act(() => {
|
|
85
|
+
stop = result.current.markRenderStart();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// stop should be a function
|
|
89
|
+
expect(typeof stop!).toBe('function');
|
|
90
|
+
|
|
91
|
+
act(() => {
|
|
92
|
+
stop();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// After calling stop, lastRenderDuration should be a number
|
|
96
|
+
expect(result.current.metrics.lastRenderDuration).toBeTypeOf('number');
|
|
97
|
+
expect(result.current.metrics.lastRenderDuration!).toBeGreaterThanOrEqual(0);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should create a debounced function', async () => {
|
|
101
|
+
const { result } = renderHook(() => usePerformance({ debounceMs: 100 }));
|
|
102
|
+
|
|
103
|
+
const callback = vi.fn();
|
|
104
|
+
let debounced: typeof callback;
|
|
105
|
+
|
|
106
|
+
act(() => {
|
|
107
|
+
debounced = result.current.debounce(callback);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Call the debounced function multiple times in quick succession
|
|
111
|
+
act(() => {
|
|
112
|
+
debounced!('arg1');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Callback not called immediately
|
|
116
|
+
expect(callback).not.toHaveBeenCalled();
|
|
117
|
+
|
|
118
|
+
// Wait for debounce to fire
|
|
119
|
+
await act(async () => {
|
|
120
|
+
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Called once with the last arguments
|
|
124
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
125
|
+
expect(callback).toHaveBeenCalledWith('arg1');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should provide initial metrics with null values', () => {
|
|
129
|
+
const { result } = renderHook(() => usePerformance());
|
|
130
|
+
|
|
131
|
+
// FCP and LCP may be null in test environment
|
|
132
|
+
expect(result.current.metrics.lcp).toBeNull();
|
|
133
|
+
expect(result.current.metrics.fcp).toBeNull();
|
|
134
|
+
expect(result.current.metrics.tti).toBeNull();
|
|
135
|
+
expect(result.current.metrics.lastRenderDuration).toBeNull();
|
|
136
|
+
});
|
|
137
|
+
});
|