@object-ui/react 2.0.0 → 3.0.1

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 (89) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +28 -0
  3. package/dist/SchemaRenderer.d.ts +20 -1
  4. package/dist/SchemaRenderer.js +76 -13
  5. package/dist/context/DndContext.d.ts +135 -0
  6. package/dist/context/DndContext.js +120 -0
  7. package/dist/context/NotificationContext.d.ts +113 -0
  8. package/dist/context/NotificationContext.js +130 -0
  9. package/dist/context/index.d.ts +2 -0
  10. package/dist/context/index.js +2 -0
  11. package/dist/hooks/index.d.ts +17 -0
  12. package/dist/hooks/index.js +17 -0
  13. package/dist/hooks/useAnimation.d.ts +46 -0
  14. package/dist/hooks/useAnimation.js +59 -0
  15. package/dist/hooks/useClientNotifications.d.ts +53 -0
  16. package/dist/hooks/useClientNotifications.js +190 -0
  17. package/dist/hooks/useColumnSummary.d.ts +38 -0
  18. package/dist/hooks/useColumnSummary.js +54 -0
  19. package/dist/hooks/useCrudShortcuts.d.ts +39 -0
  20. package/dist/hooks/useCrudShortcuts.js +96 -0
  21. package/dist/hooks/useDensityMode.d.ts +48 -0
  22. package/dist/hooks/useDensityMode.js +60 -0
  23. package/dist/hooks/useETagCache.d.ts +75 -0
  24. package/dist/hooks/useETagCache.js +306 -0
  25. package/dist/hooks/useFocusManagement.d.ts +89 -0
  26. package/dist/hooks/useFocusManagement.js +188 -0
  27. package/dist/hooks/useFocusTrap.d.ts +41 -0
  28. package/dist/hooks/useFocusTrap.js +103 -0
  29. package/dist/hooks/useKeyboardShortcuts.d.ts +49 -0
  30. package/dist/hooks/useKeyboardShortcuts.js +86 -0
  31. package/dist/hooks/useOffline.d.ts +96 -0
  32. package/dist/hooks/useOffline.js +185 -0
  33. package/dist/hooks/usePageTransition.d.ts +60 -0
  34. package/dist/hooks/usePageTransition.js +111 -0
  35. package/dist/hooks/usePerformance.d.ts +87 -0
  36. package/dist/hooks/usePerformance.js +165 -0
  37. package/dist/hooks/usePerformanceBudget.d.ts +78 -0
  38. package/dist/hooks/usePerformanceBudget.js +209 -0
  39. package/dist/hooks/useReducedMotion.d.ts +28 -0
  40. package/dist/hooks/useReducedMotion.js +48 -0
  41. package/dist/hooks/useSchemaPersistence.d.ts +87 -0
  42. package/dist/hooks/useSchemaPersistence.js +173 -0
  43. package/dist/hooks/useViewSharing.d.ts +57 -0
  44. package/dist/hooks/useViewSharing.js +74 -0
  45. package/dist/hooks/useViewTransition.d.ts +54 -0
  46. package/dist/hooks/useViewTransition.js +83 -0
  47. package/package.json +7 -7
  48. package/src/SchemaRenderer.tsx +122 -13
  49. package/src/__tests__/SchemaRenderer.aria.test.tsx +118 -0
  50. package/src/__tests__/SchemaRenderer.errorBoundary.test.tsx +114 -0
  51. package/src/__tests__/SchemaRendererProvider.smoke.test.tsx +132 -0
  52. package/src/__tests__/useOffline.test.ts +198 -0
  53. package/src/__tests__/usePageTransition.test.ts +214 -0
  54. package/src/__tests__/usePerformance.test.ts +137 -0
  55. package/src/__tests__/view-switching.test.tsx +456 -0
  56. package/src/context/DndContext.tsx +260 -0
  57. package/src/context/NotificationContext.tsx +280 -0
  58. package/src/context/__tests__/DndContext.test.tsx +150 -0
  59. package/src/context/__tests__/NotificationContext.test.tsx +176 -0
  60. package/src/context/index.ts +2 -0
  61. package/src/hooks/__tests__/useAnimation.test.ts +82 -0
  62. package/src/hooks/__tests__/useColumnSummary.test.ts +93 -0
  63. package/src/hooks/__tests__/useDensityMode.test.ts +81 -0
  64. package/src/hooks/__tests__/useETagCache.test.ts +153 -0
  65. package/src/hooks/__tests__/useFocusTrap.test.ts +47 -0
  66. package/src/hooks/__tests__/useKeyboardShortcuts.test.ts +144 -0
  67. package/src/hooks/__tests__/usePerformanceBudget.test.ts +157 -0
  68. package/src/hooks/__tests__/useSchemaPersistence.test.ts +220 -0
  69. package/src/hooks/__tests__/useViewSharing.test.ts +121 -0
  70. package/src/hooks/__tests__/useViewTransition.test.ts +61 -0
  71. package/src/hooks/index.ts +17 -1
  72. package/src/hooks/useAnimation.ts +117 -0
  73. package/src/hooks/useClientNotifications.ts +251 -0
  74. package/src/hooks/useColumnSummary.ts +87 -0
  75. package/src/hooks/useCrudShortcuts.ts +126 -0
  76. package/src/hooks/useDensityMode.ts +102 -0
  77. package/src/hooks/useETagCache.ts +404 -0
  78. package/src/hooks/useFocusManagement.ts +248 -0
  79. package/src/hooks/useFocusTrap.ts +138 -0
  80. package/src/hooks/useKeyboardShortcuts.ts +119 -0
  81. package/src/hooks/useOffline.ts +301 -0
  82. package/src/hooks/usePageTransition.ts +187 -0
  83. package/src/hooks/usePerformance.ts +246 -0
  84. package/src/hooks/usePerformanceBudget.ts +313 -0
  85. package/src/hooks/useReducedMotion.ts +53 -0
  86. package/src/hooks/useSchemaPersistence.ts +222 -0
  87. package/src/hooks/useViewSharing.ts +140 -0
  88. package/src/hooks/useViewTransition.ts +144 -0
  89. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,132 @@
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
+ /**
10
+ * Smoke tests for SchemaRendererProvider.
11
+ *
12
+ * These tests ensure that every registered plugin component that calls
13
+ * useSchemaContext() can render without throwing when wrapped in a
14
+ * SchemaRendererProvider. This catches the class of errors reported in
15
+ * Storybook ("useSchemaContext must be used within a SchemaRendererProvider").
16
+ */
17
+
18
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
19
+ import { render, screen } from '@testing-library/react';
20
+ import React from 'react';
21
+ import { ComponentRegistry } from '@object-ui/core';
22
+ import { SchemaRenderer } from '../SchemaRenderer';
23
+ import { SchemaRendererProvider, useSchemaContext } from '../context/SchemaRendererContext';
24
+
25
+ // Suppress console.error from React error boundary during tests
26
+ const originalConsoleError = console.error;
27
+ beforeEach(() => {
28
+ console.error = vi.fn();
29
+ });
30
+ afterEach(() => {
31
+ console.error = originalConsoleError;
32
+ });
33
+
34
+ // Helper: a component that calls useSchemaContext (mimics plugin pattern)
35
+ const ContextConsumer: React.FC<any> = ({ schema }) => {
36
+ const { dataSource } = useSchemaContext();
37
+ return <div data-testid="ctx-consumer">dataSource: {JSON.stringify(dataSource)}</div>;
38
+ };
39
+
40
+ describe('useSchemaContext provider requirement', () => {
41
+ it('should throw when used outside SchemaRendererProvider', () => {
42
+ expect(() => render(<ContextConsumer schema={{}} />)).toThrow(
43
+ 'useSchemaContext must be used within a SchemaRendererProvider'
44
+ );
45
+ });
46
+
47
+ it('should not throw when used inside SchemaRendererProvider', () => {
48
+ render(
49
+ <SchemaRendererProvider dataSource={{ test: true }}>
50
+ <ContextConsumer schema={{}} />
51
+ </SchemaRendererProvider>
52
+ );
53
+ expect(screen.getByTestId('ctx-consumer')).toHaveTextContent('dataSource: {"test":true}');
54
+ });
55
+
56
+ it('should fall back to empty dataSource when provider has empty object', () => {
57
+ render(
58
+ <SchemaRendererProvider dataSource={{}}>
59
+ <ContextConsumer schema={{}} />
60
+ </SchemaRendererProvider>
61
+ );
62
+ expect(screen.getByTestId('ctx-consumer')).toHaveTextContent('dataSource: {}');
63
+ });
64
+ });
65
+
66
+ describe('SchemaRenderer + SchemaRendererProvider integration', () => {
67
+ beforeEach(() => {
68
+ ComponentRegistry.register('test-ctx-consumer', ContextConsumer);
69
+ });
70
+
71
+ afterEach(() => {
72
+ ComponentRegistry.unregister?.('test-ctx-consumer');
73
+ });
74
+
75
+ it('should render a component that calls useSchemaContext without error when provider wraps the tree', () => {
76
+ render(
77
+ <SchemaRendererProvider dataSource={{ foo: 'bar' }}>
78
+ <SchemaRenderer schema={{ type: 'test-ctx-consumer' }} />
79
+ </SchemaRendererProvider>
80
+ );
81
+ expect(screen.getByTestId('ctx-consumer')).toHaveTextContent('dataSource: {"foo":"bar"}');
82
+ });
83
+
84
+ it('should show error boundary fallback (not crash) when provider is missing', () => {
85
+ // Without a provider, the SchemaErrorBoundary catches the throw
86
+ render(
87
+ <SchemaRenderer schema={{ type: 'test-ctx-consumer' }} />
88
+ );
89
+ expect(screen.getByRole('alert')).toBeInTheDocument();
90
+ expect(screen.getByText(/failed to render/i)).toBeInTheDocument();
91
+ });
92
+ });
93
+
94
+ describe('Plugin component types render inside provider', () => {
95
+ // This test ensures all registered plugin types that use useSchemaContext
96
+ // can at least mount without throwing inside a SchemaRendererProvider.
97
+ const pluginTypes = [
98
+ 'kanban',
99
+ 'object-kanban',
100
+ 'timeline',
101
+ 'object-timeline',
102
+ 'object-grid',
103
+ 'object-calendar',
104
+ 'object-map',
105
+ 'chart',
106
+ 'object-gantt',
107
+ ];
108
+
109
+ for (const type of pluginTypes) {
110
+ it(`type="${type}" should not throw inside SchemaRendererProvider`, () => {
111
+ const component = ComponentRegistry.get(type);
112
+ if (!component) {
113
+ // Component not registered in test environment — skip
114
+ return;
115
+ }
116
+
117
+ // Render via SchemaRenderer inside provider
118
+ const { container } = render(
119
+ <SchemaRendererProvider dataSource={{}}>
120
+ <SchemaRenderer schema={{ type }} />
121
+ </SchemaRendererProvider>
122
+ );
123
+
124
+ // Should NOT show the error boundary alert
125
+ const alerts = container.querySelectorAll('[role="alert"]');
126
+ const providerErrors = Array.from(alerts).filter((el) =>
127
+ el.textContent?.includes('useSchemaContext must be used within')
128
+ );
129
+ expect(providerErrors).toHaveLength(0);
130
+ });
131
+ }
132
+ });
@@ -0,0 +1,198 @@
1
+ /**
2
+ * ObjectUI — useOffline 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, act } from '@testing-library/react';
11
+ import { useOffline } from '../hooks/useOffline';
12
+
13
+ // ---------------------------------------------------------------------------
14
+ // Helpers
15
+ // ---------------------------------------------------------------------------
16
+
17
+ const QUEUE_KEY = 'objectui-offline-queue';
18
+
19
+ function mockOnlineStatus(online: boolean) {
20
+ Object.defineProperty(navigator, 'onLine', {
21
+ value: online,
22
+ configurable: true,
23
+ writable: true,
24
+ });
25
+ }
26
+
27
+ function triggerOnline() {
28
+ mockOnlineStatus(true);
29
+ window.dispatchEvent(new Event('online'));
30
+ }
31
+
32
+ function triggerOffline() {
33
+ mockOnlineStatus(false);
34
+ window.dispatchEvent(new Event('offline'));
35
+ }
36
+
37
+ // ---------------------------------------------------------------------------
38
+ // Tests
39
+ // ---------------------------------------------------------------------------
40
+
41
+ describe('useOffline', () => {
42
+ beforeEach(() => {
43
+ mockOnlineStatus(true);
44
+ localStorage.removeItem(QUEUE_KEY);
45
+ });
46
+
47
+ afterEach(() => {
48
+ vi.restoreAllMocks();
49
+ });
50
+
51
+ it('should default to online with empty queue', () => {
52
+ const { result } = renderHook(() => useOffline());
53
+ expect(result.current.isOnline).toBe(true);
54
+ expect(result.current.enabled).toBe(true);
55
+ expect(result.current.syncState).toBe('idle');
56
+ expect(result.current.pendingCount).toBe(0);
57
+ expect(result.current.showIndicator).toBe(false);
58
+ expect(result.current.strategy).toBe('network_first');
59
+ });
60
+
61
+ it('should detect offline status', () => {
62
+ const { result } = renderHook(() => useOffline());
63
+ expect(result.current.isOnline).toBe(true);
64
+
65
+ act(() => {
66
+ triggerOffline();
67
+ });
68
+
69
+ expect(result.current.isOnline).toBe(false);
70
+ expect(result.current.showIndicator).toBe(true);
71
+ });
72
+
73
+ it('should detect coming back online', () => {
74
+ mockOnlineStatus(false);
75
+ const { result } = renderHook(() => useOffline());
76
+ expect(result.current.isOnline).toBe(false);
77
+
78
+ act(() => {
79
+ triggerOnline();
80
+ });
81
+
82
+ expect(result.current.isOnline).toBe(true);
83
+ expect(result.current.showIndicator).toBe(false);
84
+ });
85
+
86
+ it('should queue mutations', () => {
87
+ const { result } = renderHook(() => useOffline());
88
+
89
+ act(() => {
90
+ result.current.queueMutation({
91
+ operation: 'create',
92
+ resource: 'users',
93
+ data: { name: 'Alice' },
94
+ });
95
+ });
96
+
97
+ expect(result.current.pendingCount).toBe(1);
98
+
99
+ act(() => {
100
+ result.current.queueMutation({
101
+ operation: 'update',
102
+ resource: 'users',
103
+ data: { id: '1', name: 'Bob' },
104
+ });
105
+ });
106
+
107
+ expect(result.current.pendingCount).toBe(2);
108
+ });
109
+
110
+ it('should clear the mutation queue', () => {
111
+ const { result } = renderHook(() => useOffline());
112
+
113
+ act(() => {
114
+ result.current.queueMutation({ operation: 'create', resource: 'users' });
115
+ result.current.queueMutation({ operation: 'delete', resource: 'users' });
116
+ });
117
+
118
+ expect(result.current.pendingCount).toBe(2);
119
+
120
+ act(() => {
121
+ result.current.clearQueue();
122
+ });
123
+
124
+ expect(result.current.pendingCount).toBe(0);
125
+ });
126
+
127
+ it('should enforce queueMaxSize', () => {
128
+ const { result } = renderHook(() => useOffline({ queueMaxSize: 2 }));
129
+
130
+ act(() => {
131
+ result.current.queueMutation({ operation: 'create', resource: 'a' });
132
+ result.current.queueMutation({ operation: 'create', resource: 'b' });
133
+ result.current.queueMutation({ operation: 'create', resource: 'c' });
134
+ });
135
+
136
+ expect(result.current.pendingCount).toBe(2);
137
+ });
138
+
139
+ it('should respect custom config values', () => {
140
+ const { result } = renderHook(() =>
141
+ useOffline({
142
+ strategy: 'cache_first',
143
+ offlineIndicator: false,
144
+ offlineMessage: 'Custom message',
145
+ }),
146
+ );
147
+
148
+ expect(result.current.strategy).toBe('cache_first');
149
+ expect(result.current.offlineMessage).toBe('Custom message');
150
+
151
+ act(() => {
152
+ triggerOffline();
153
+ });
154
+
155
+ // offlineIndicator is false, so showIndicator should be false even when offline
156
+ expect(result.current.showIndicator).toBe(false);
157
+ });
158
+
159
+ it('should not queue mutations when disabled', () => {
160
+ const { result } = renderHook(() => useOffline({ enabled: false }));
161
+
162
+ act(() => {
163
+ result.current.queueMutation({ operation: 'create', resource: 'users' });
164
+ });
165
+
166
+ expect(result.current.pendingCount).toBe(0);
167
+ expect(result.current.enabled).toBe(false);
168
+ });
169
+
170
+ it('should sync mutations', async () => {
171
+ const { result } = renderHook(() => useOffline());
172
+
173
+ act(() => {
174
+ result.current.queueMutation({ operation: 'create', resource: 'users' });
175
+ });
176
+
177
+ expect(result.current.pendingCount).toBe(1);
178
+
179
+ await act(async () => {
180
+ await result.current.sync();
181
+ });
182
+
183
+ expect(result.current.pendingCount).toBe(0);
184
+ expect(result.current.syncState).toBe('idle');
185
+ });
186
+
187
+ it('should persist queue to localStorage', () => {
188
+ const { result } = renderHook(() => useOffline());
189
+
190
+ act(() => {
191
+ result.current.queueMutation({ operation: 'create', resource: 'users' });
192
+ });
193
+
194
+ const stored = JSON.parse(localStorage.getItem(QUEUE_KEY) || '[]');
195
+ expect(stored).toHaveLength(1);
196
+ expect(stored[0].resource).toBe('users');
197
+ });
198
+ });
@@ -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,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
+ });