@object-ui/react 3.3.0 → 3.3.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.
- package/CHANGELOG.md +9 -0
- package/README.md +21 -1
- package/dist/context/AppShellContext.d.ts +26 -0
- package/dist/context/AppShellContext.js +67 -0
- package/dist/context/index.d.ts +1 -7
- package/dist/context/index.js +1 -7
- package/package.json +32 -9
- package/.turbo/turbo-build.log +0 -4
- package/src/LazyPluginLoader.tsx +0 -174
- package/src/SchemaRenderer.tsx +0 -266
- package/src/__tests__/LazyPluginLoader.test.tsx +0 -176
- package/src/__tests__/SchemaRenderer.aria.test.tsx +0 -118
- package/src/__tests__/SchemaRenderer.debug.test.tsx +0 -69
- package/src/__tests__/SchemaRenderer.errorBoundary.test.tsx +0 -114
- package/src/__tests__/SchemaRenderer.expressions.test.tsx +0 -131
- package/src/__tests__/SchemaRendererProvider.smoke.test.tsx +0 -132
- package/src/__tests__/ThemeProvider.test.tsx +0 -451
- package/src/__tests__/useNavigationOverlay.test.ts +0 -578
- package/src/__tests__/useOffline.test.ts +0 -198
- package/src/__tests__/usePageTransition.test.ts +0 -214
- package/src/__tests__/usePageVariables.test.tsx +0 -334
- package/src/__tests__/usePerformance.test.ts +0 -137
- package/src/__tests__/useViewData.test.tsx +0 -422
- package/src/__tests__/view-switching.test.tsx +0 -456
- package/src/components/form/FieldFactory.tsx +0 -516
- package/src/components/form/FormRenderer.test.tsx +0 -744
- package/src/components/form/FormRenderer.tsx +0 -258
- package/src/components/form/README.md +0 -352
- package/src/components/form/index.ts +0 -12
- package/src/context/ActionContext.tsx +0 -213
- package/src/context/DndContext.tsx +0 -260
- package/src/context/NotificationContext.tsx +0 -280
- package/src/context/SchemaRendererContext.tsx +0 -48
- package/src/context/ThemeContext.tsx +0 -325
- package/src/context/__tests__/ActionContext.test.tsx +0 -174
- package/src/context/__tests__/DndContext.test.tsx +0 -150
- package/src/context/__tests__/NotificationContext.test.tsx +0 -176
- package/src/context/__tests__/useDataScope.test.tsx +0 -91
- package/src/context/index.ts +0 -13
- package/src/hooks/__tests__/useActionEngine.test.ts +0 -170
- package/src/hooks/__tests__/useActionRunner.test.ts +0 -153
- package/src/hooks/__tests__/useAnimation.test.ts +0 -82
- package/src/hooks/__tests__/useColumnSummary.test.ts +0 -93
- package/src/hooks/__tests__/useCrudShortcuts.test.ts +0 -85
- package/src/hooks/__tests__/useDataRefresh.test.ts +0 -115
- package/src/hooks/__tests__/useDebugMode.test.ts +0 -83
- package/src/hooks/__tests__/useDensityMode.test.ts +0 -81
- package/src/hooks/__tests__/useDiscovery.test.tsx +0 -247
- package/src/hooks/__tests__/useDynamicApp.test.ts +0 -161
- package/src/hooks/__tests__/useETagCache.test.ts +0 -153
- package/src/hooks/__tests__/useExpression.test.ts +0 -86
- package/src/hooks/__tests__/useFocusTrap.test.ts +0 -294
- package/src/hooks/__tests__/useKeyboardShortcuts.test.ts +0 -144
- package/src/hooks/__tests__/usePageTransition.test.ts +0 -158
- package/src/hooks/__tests__/usePerformanceBudget.test.ts +0 -157
- package/src/hooks/__tests__/useReducedMotion.test.ts +0 -104
- package/src/hooks/__tests__/useSchemaPersistence.test.ts +0 -220
- package/src/hooks/__tests__/useViewSharing.test.ts +0 -121
- package/src/hooks/__tests__/useViewTransition.test.ts +0 -61
- package/src/hooks/index.ts +0 -36
- package/src/hooks/useActionEngine.ts +0 -84
- package/src/hooks/useActionRunner.ts +0 -119
- package/src/hooks/useAnimation.ts +0 -117
- package/src/hooks/useClientNotifications.ts +0 -251
- package/src/hooks/useColumnSummary.ts +0 -87
- package/src/hooks/useCrudShortcuts.ts +0 -126
- package/src/hooks/useDataRefresh.ts +0 -58
- package/src/hooks/useDebugMode.ts +0 -95
- package/src/hooks/useDensityMode.ts +0 -102
- package/src/hooks/useDiscovery.ts +0 -168
- package/src/hooks/useDynamicApp.ts +0 -142
- package/src/hooks/useETagCache.ts +0 -404
- package/src/hooks/useExpression.ts +0 -56
- package/src/hooks/useFocusManagement.ts +0 -248
- package/src/hooks/useFocusTrap.ts +0 -138
- package/src/hooks/useGlobalUndo.ts +0 -98
- package/src/hooks/useKeyboardShortcuts.ts +0 -119
- package/src/hooks/useNavigationOverlay.ts +0 -190
- package/src/hooks/useOffline.ts +0 -301
- package/src/hooks/usePageTransition.ts +0 -187
- package/src/hooks/usePageVariables.tsx +0 -157
- package/src/hooks/usePerformance.ts +0 -246
- package/src/hooks/usePerformanceBudget.ts +0 -313
- package/src/hooks/useReducedMotion.ts +0 -53
- package/src/hooks/useSchemaPersistence.ts +0 -222
- package/src/hooks/useViewData.ts +0 -201
- package/src/hooks/useViewSharing.ts +0 -140
- package/src/hooks/useViewTransition.ts +0 -144
- package/src/index.ts +0 -41
- package/src/spec-bridge/SpecBridge.ts +0 -64
- package/src/spec-bridge/__tests__/P1SpecBridge.test.ts +0 -855
- package/src/spec-bridge/__tests__/SpecBridge.test.ts +0 -550
- package/src/spec-bridge/bridges/dashboard.ts +0 -180
- package/src/spec-bridge/bridges/form-view.ts +0 -109
- package/src/spec-bridge/bridges/list-view.ts +0 -187
- package/src/spec-bridge/bridges/page.ts +0 -136
- package/src/spec-bridge/index.ts +0 -14
- package/src/spec-bridge/types.ts +0 -38
- package/src/utils/i18n.ts +0 -10
- package/tsconfig.json +0 -17
- package/tsconfig.tsbuildinfo +0 -1
- package/vitest.config.ts +0 -2
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for usePageTransition hook
|
|
3
|
-
*/
|
|
4
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
5
|
-
import { renderHook } from '@testing-library/react';
|
|
6
|
-
import { usePageTransition } from '../usePageTransition';
|
|
7
|
-
|
|
8
|
-
describe('usePageTransition', () => {
|
|
9
|
-
|
|
10
|
-
it('returns inactive result when type is none (default)', () => {
|
|
11
|
-
const { result } = renderHook(() => usePageTransition());
|
|
12
|
-
expect(result.current.isActive).toBe(false);
|
|
13
|
-
expect(result.current.enterClassName).toBe('');
|
|
14
|
-
expect(result.current.exitClassName).toBe('');
|
|
15
|
-
expect(result.current.type).toBe('none');
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('returns active result for fade transition', () => {
|
|
19
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade' }));
|
|
20
|
-
expect(result.current.isActive).toBe(true);
|
|
21
|
-
expect(result.current.enterClassName).toContain('fade-in');
|
|
22
|
-
expect(result.current.exitClassName).toContain('fade-out');
|
|
23
|
-
expect(result.current.type).toBe('fade');
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('returns correct classes for slide_up transition', () => {
|
|
27
|
-
const { result } = renderHook(() => usePageTransition({ type: 'slide_up' }));
|
|
28
|
-
expect(result.current.isActive).toBe(true);
|
|
29
|
-
expect(result.current.enterClassName).toContain('slide-in-from-bottom');
|
|
30
|
-
expect(result.current.exitClassName).toContain('slide-out-to-top');
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('returns correct classes for slide_down transition', () => {
|
|
34
|
-
const { result } = renderHook(() => usePageTransition({ type: 'slide_down' }));
|
|
35
|
-
expect(result.current.enterClassName).toContain('slide-in-from-top');
|
|
36
|
-
expect(result.current.exitClassName).toContain('slide-out-to-bottom');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('returns correct classes for slide_left transition', () => {
|
|
40
|
-
const { result } = renderHook(() => usePageTransition({ type: 'slide_left' }));
|
|
41
|
-
expect(result.current.enterClassName).toContain('slide-in-from-right');
|
|
42
|
-
expect(result.current.exitClassName).toContain('slide-out-to-left');
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('returns correct classes for slide_right transition', () => {
|
|
46
|
-
const { result } = renderHook(() => usePageTransition({ type: 'slide_right' }));
|
|
47
|
-
expect(result.current.enterClassName).toContain('slide-in-from-left');
|
|
48
|
-
expect(result.current.exitClassName).toContain('slide-out-to-right');
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('returns correct classes for scale transition', () => {
|
|
52
|
-
const { result } = renderHook(() => usePageTransition({ type: 'scale' }));
|
|
53
|
-
expect(result.current.enterClassName).toContain('zoom-in');
|
|
54
|
-
expect(result.current.exitClassName).toContain('zoom-out');
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('returns correct classes for rotate transition', () => {
|
|
58
|
-
const { result } = renderHook(() => usePageTransition({ type: 'rotate' }));
|
|
59
|
-
expect(result.current.enterClassName).toContain('spin-in');
|
|
60
|
-
expect(result.current.exitClassName).toContain('spin-out');
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('returns correct classes for flip transition', () => {
|
|
64
|
-
const { result } = renderHook(() => usePageTransition({ type: 'flip' }));
|
|
65
|
-
expect(result.current.enterClassName).toContain('zoom-in');
|
|
66
|
-
expect(result.current.exitClassName).toContain('zoom-out');
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('uses default duration of 300ms', () => {
|
|
70
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade' }));
|
|
71
|
-
expect(result.current.duration).toBe(300);
|
|
72
|
-
expect(result.current.enterStyle.animationDuration).toBe('300ms');
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('accepts custom duration', () => {
|
|
76
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade', duration: 500 }));
|
|
77
|
-
expect(result.current.duration).toBe(500);
|
|
78
|
-
expect(result.current.enterStyle.animationDuration).toBe('500ms');
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('uses ease_in_out easing by default', () => {
|
|
82
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade' }));
|
|
83
|
-
expect(result.current.enterStyle.animationTimingFunction).toBe('ease-in-out');
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('accepts custom easing', () => {
|
|
87
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade', easing: 'spring' }));
|
|
88
|
-
expect(result.current.enterStyle.animationTimingFunction).toBe('cubic-bezier(0.34, 1.56, 0.64, 1)');
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('sets animationFillMode to both', () => {
|
|
92
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade' }));
|
|
93
|
-
expect(result.current.enterStyle.animationFillMode).toBe('both');
|
|
94
|
-
expect(result.current.exitStyle.animationFillMode).toBe('both');
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('applies crossFade positioning', () => {
|
|
98
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade', crossFade: true }));
|
|
99
|
-
expect(result.current.enterStyle.position).toBe('absolute');
|
|
100
|
-
expect(result.current.enterStyle.inset).toBe('0');
|
|
101
|
-
expect(result.current.exitStyle.position).toBe('absolute');
|
|
102
|
-
expect(result.current.exitStyle.inset).toBe('0');
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('does not apply crossFade positioning when disabled', () => {
|
|
106
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade', crossFade: false }));
|
|
107
|
-
expect(result.current.enterStyle.position).toBeUndefined();
|
|
108
|
-
expect(result.current.exitStyle.position).toBeUndefined();
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('returns inactive when reduced motion is preferred', () => {
|
|
112
|
-
// Mock window.matchMedia to simulate reduced motion preference
|
|
113
|
-
const original = window.matchMedia;
|
|
114
|
-
window.matchMedia = vi.fn((query: string) => ({
|
|
115
|
-
matches: query === '(prefers-reduced-motion: reduce)',
|
|
116
|
-
media: query,
|
|
117
|
-
onchange: null,
|
|
118
|
-
addEventListener: vi.fn(),
|
|
119
|
-
removeEventListener: vi.fn(),
|
|
120
|
-
addListener: vi.fn(),
|
|
121
|
-
removeListener: vi.fn(),
|
|
122
|
-
dispatchEvent: vi.fn(),
|
|
123
|
-
})) as unknown as typeof window.matchMedia;
|
|
124
|
-
|
|
125
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade' }));
|
|
126
|
-
expect(result.current.isActive).toBe(false);
|
|
127
|
-
expect(result.current.enterClassName).toBe('');
|
|
128
|
-
expect(result.current.exitClassName).toBe('');
|
|
129
|
-
|
|
130
|
-
window.matchMedia = original;
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('returns empty styles when inactive', () => {
|
|
134
|
-
const { result } = renderHook(() => usePageTransition({ type: 'none' }));
|
|
135
|
-
expect(result.current.enterStyle).toEqual({});
|
|
136
|
-
expect(result.current.exitStyle).toEqual({});
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('accepts linear easing', () => {
|
|
140
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade', easing: 'linear' }));
|
|
141
|
-
expect(result.current.enterStyle.animationTimingFunction).toBe('linear');
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('accepts ease easing', () => {
|
|
145
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade', easing: 'ease' }));
|
|
146
|
-
expect(result.current.enterStyle.animationTimingFunction).toBe('ease');
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('accepts ease_in easing', () => {
|
|
150
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade', easing: 'ease_in' }));
|
|
151
|
-
expect(result.current.enterStyle.animationTimingFunction).toBe('ease-in');
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('accepts ease_out easing', () => {
|
|
155
|
-
const { result } = renderHook(() => usePageTransition({ type: 'fade', easing: 'ease_out' }));
|
|
156
|
-
expect(result.current.enterStyle.animationTimingFunction).toBe('ease-out');
|
|
157
|
-
});
|
|
158
|
-
});
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for usePerformanceBudget hook
|
|
3
|
-
*/
|
|
4
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
5
|
-
import { renderHook, act } from '@testing-library/react';
|
|
6
|
-
import { usePerformanceBudget } from '../usePerformanceBudget';
|
|
7
|
-
|
|
8
|
-
describe('usePerformanceBudget', () => {
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
vi.useFakeTimers();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
afterEach(() => {
|
|
14
|
-
vi.useRealTimers();
|
|
15
|
-
vi.restoreAllMocks();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('returns default budget config when no config provided', () => {
|
|
19
|
-
const { result } = renderHook(() => usePerformanceBudget());
|
|
20
|
-
|
|
21
|
-
// Default snapshot values
|
|
22
|
-
expect(result.current.snapshot).toEqual({
|
|
23
|
-
lcp: null,
|
|
24
|
-
fcp: null,
|
|
25
|
-
memoryMB: null,
|
|
26
|
-
});
|
|
27
|
-
expect(result.current.clearViolations).toBeTypeOf('function');
|
|
28
|
-
expect(result.current.checkMetric).toBeTypeOf('function');
|
|
29
|
-
expect(result.current.trackRender).toBeTypeOf('function');
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('isWithinBudget is true initially (no violations)', () => {
|
|
33
|
-
const { result } = renderHook(() => usePerformanceBudget());
|
|
34
|
-
|
|
35
|
-
expect(result.current.isWithinBudget).toBe(true);
|
|
36
|
-
expect(result.current.violations).toEqual([]);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('trackRender records a violation when render exceeds maxRenderTime', () => {
|
|
40
|
-
const { result } = renderHook(() =>
|
|
41
|
-
usePerformanceBudget({ maxRenderTime: 16, warnOnViolation: false }),
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
act(() => {
|
|
45
|
-
result.current.trackRender(50);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
expect(result.current.violations).toHaveLength(1);
|
|
49
|
-
expect(result.current.violations[0].metric).toBe('renderTime');
|
|
50
|
-
expect(result.current.violations[0].actual).toBe(50);
|
|
51
|
-
expect(result.current.violations[0].budget).toBe(16);
|
|
52
|
-
expect(result.current.isWithinBudget).toBe(false);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('trackRender does NOT record when render is within budget', () => {
|
|
56
|
-
const { result } = renderHook(() =>
|
|
57
|
-
usePerformanceBudget({ maxRenderTime: 16, warnOnViolation: false }),
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
let withinBudget: boolean = false;
|
|
61
|
-
act(() => {
|
|
62
|
-
withinBudget = result.current.trackRender(10);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
expect(withinBudget).toBe(true);
|
|
66
|
-
expect(result.current.violations).toHaveLength(0);
|
|
67
|
-
expect(result.current.isWithinBudget).toBe(true);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('checkMetric returns false for metrics exceeding budget', () => {
|
|
71
|
-
const { result } = renderHook(() =>
|
|
72
|
-
usePerformanceBudget({ maxLCP: 600, warnOnViolation: false }),
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
let withinBudget: boolean = true;
|
|
76
|
-
act(() => {
|
|
77
|
-
withinBudget = result.current.checkMetric('lcp', 800);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
expect(withinBudget).toBe(false);
|
|
81
|
-
expect(result.current.violations).toHaveLength(1);
|
|
82
|
-
expect(result.current.violations[0].metric).toBe('lcp');
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('checkMetric returns true for metrics within budget', () => {
|
|
86
|
-
const { result } = renderHook(() =>
|
|
87
|
-
usePerformanceBudget({ maxLCP: 600, warnOnViolation: false }),
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
let withinBudget: boolean = false;
|
|
91
|
-
act(() => {
|
|
92
|
-
withinBudget = result.current.checkMetric('lcp', 400);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
expect(withinBudget).toBe(true);
|
|
96
|
-
expect(result.current.violations).toHaveLength(0);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('clearViolations removes all violations', () => {
|
|
100
|
-
const { result } = renderHook(() =>
|
|
101
|
-
usePerformanceBudget({ maxRenderTime: 16, warnOnViolation: false }),
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
act(() => {
|
|
105
|
-
result.current.trackRender(50);
|
|
106
|
-
});
|
|
107
|
-
expect(result.current.violations).toHaveLength(1);
|
|
108
|
-
|
|
109
|
-
act(() => {
|
|
110
|
-
result.current.clearViolations();
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
expect(result.current.violations).toHaveLength(0);
|
|
114
|
-
expect(result.current.isWithinBudget).toBe(true);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('deduplication: same metric type only recorded once', () => {
|
|
118
|
-
const { result } = renderHook(() =>
|
|
119
|
-
usePerformanceBudget({ maxRenderTime: 16, warnOnViolation: false }),
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
act(() => {
|
|
123
|
-
result.current.trackRender(50);
|
|
124
|
-
});
|
|
125
|
-
act(() => {
|
|
126
|
-
result.current.trackRender(100);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
// Only one violation for 'renderTime' due to deduplication
|
|
130
|
-
expect(result.current.violations).toHaveLength(1);
|
|
131
|
-
expect(result.current.violations[0].metric).toBe('renderTime');
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it('onViolation callback is called on budget violation', () => {
|
|
135
|
-
const onViolation = vi.fn();
|
|
136
|
-
const { result } = renderHook(() =>
|
|
137
|
-
usePerformanceBudget({
|
|
138
|
-
maxRenderTime: 16,
|
|
139
|
-
warnOnViolation: false,
|
|
140
|
-
onViolation,
|
|
141
|
-
}),
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
act(() => {
|
|
145
|
-
result.current.trackRender(50);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
expect(onViolation).toHaveBeenCalledTimes(1);
|
|
149
|
-
expect(onViolation).toHaveBeenCalledWith(
|
|
150
|
-
expect.objectContaining({
|
|
151
|
-
metric: 'renderTime',
|
|
152
|
-
budget: 16,
|
|
153
|
-
actual: 50,
|
|
154
|
-
}),
|
|
155
|
-
);
|
|
156
|
-
});
|
|
157
|
-
});
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for useReducedMotion hook
|
|
3
|
-
*/
|
|
4
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
5
|
-
import { renderHook, act } from '@testing-library/react';
|
|
6
|
-
import { useReducedMotion } from '../useReducedMotion';
|
|
7
|
-
|
|
8
|
-
describe('useReducedMotion', () => {
|
|
9
|
-
let originalMatchMedia: typeof window.matchMedia;
|
|
10
|
-
let listeners: Map<string, ((event: MediaQueryListEvent) => void)[]>;
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
originalMatchMedia = window.matchMedia;
|
|
14
|
-
listeners = new Map();
|
|
15
|
-
|
|
16
|
-
window.matchMedia = vi.fn((query: string) => {
|
|
17
|
-
const mediaQueryList = {
|
|
18
|
-
matches: false,
|
|
19
|
-
media: query,
|
|
20
|
-
onchange: null,
|
|
21
|
-
addEventListener: vi.fn((event: string, handler: (event: MediaQueryListEvent) => void) => {
|
|
22
|
-
const key = `${query}:${event}`;
|
|
23
|
-
if (!listeners.has(key)) {
|
|
24
|
-
listeners.set(key, []);
|
|
25
|
-
}
|
|
26
|
-
listeners.get(key)!.push(handler);
|
|
27
|
-
}),
|
|
28
|
-
removeEventListener: vi.fn(),
|
|
29
|
-
addListener: vi.fn(),
|
|
30
|
-
removeListener: vi.fn(),
|
|
31
|
-
dispatchEvent: vi.fn(),
|
|
32
|
-
} as unknown as MediaQueryList;
|
|
33
|
-
return mediaQueryList;
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
afterEach(() => {
|
|
38
|
-
window.matchMedia = originalMatchMedia;
|
|
39
|
-
listeners.clear();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('returns false when prefers-reduced-motion is not set', () => {
|
|
43
|
-
const { result } = renderHook(() => useReducedMotion());
|
|
44
|
-
expect(result.current).toBe(false);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('returns true when prefers-reduced-motion is set', () => {
|
|
48
|
-
window.matchMedia = vi.fn((query: string) => ({
|
|
49
|
-
matches: true,
|
|
50
|
-
media: query,
|
|
51
|
-
onchange: null,
|
|
52
|
-
addEventListener: vi.fn(),
|
|
53
|
-
removeEventListener: vi.fn(),
|
|
54
|
-
addListener: vi.fn(),
|
|
55
|
-
removeListener: vi.fn(),
|
|
56
|
-
dispatchEvent: vi.fn(),
|
|
57
|
-
})) as unknown as typeof window.matchMedia;
|
|
58
|
-
|
|
59
|
-
const { result } = renderHook(() => useReducedMotion());
|
|
60
|
-
expect(result.current).toBe(true);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('registers a change event listener', () => {
|
|
64
|
-
renderHook(() => useReducedMotion());
|
|
65
|
-
|
|
66
|
-
const key = '(prefers-reduced-motion: reduce):change';
|
|
67
|
-
expect(listeners.has(key)).toBe(true);
|
|
68
|
-
expect(listeners.get(key)!.length).toBe(1);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('updates when media query changes', () => {
|
|
72
|
-
const { result } = renderHook(() => useReducedMotion());
|
|
73
|
-
|
|
74
|
-
expect(result.current).toBe(false);
|
|
75
|
-
|
|
76
|
-
const key = '(prefers-reduced-motion: reduce):change';
|
|
77
|
-
const handler = listeners.get(key)![0];
|
|
78
|
-
|
|
79
|
-
act(() => {
|
|
80
|
-
handler({ matches: true } as MediaQueryListEvent);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
expect(result.current).toBe(true);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('cleans up event listener on unmount', () => {
|
|
87
|
-
const removeEventListener = vi.fn();
|
|
88
|
-
window.matchMedia = vi.fn((query: string) => ({
|
|
89
|
-
matches: false,
|
|
90
|
-
media: query,
|
|
91
|
-
onchange: null,
|
|
92
|
-
addEventListener: vi.fn(),
|
|
93
|
-
removeEventListener,
|
|
94
|
-
addListener: vi.fn(),
|
|
95
|
-
removeListener: vi.fn(),
|
|
96
|
-
dispatchEvent: vi.fn(),
|
|
97
|
-
})) as unknown as typeof window.matchMedia;
|
|
98
|
-
|
|
99
|
-
const { unmount } = renderHook(() => useReducedMotion());
|
|
100
|
-
unmount();
|
|
101
|
-
|
|
102
|
-
expect(removeEventListener).toHaveBeenCalledWith('change', expect.any(Function));
|
|
103
|
-
});
|
|
104
|
-
});
|
|
@@ -1,220 +0,0 @@
|
|
|
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, vi, beforeEach, afterEach } from 'vitest';
|
|
10
|
-
import { renderHook, act } from '@testing-library/react';
|
|
11
|
-
import {
|
|
12
|
-
useSchemaPersistence,
|
|
13
|
-
createLocalStorageAdapter,
|
|
14
|
-
} from '../useSchemaPersistence';
|
|
15
|
-
import type { SchemaPersistenceAdapter } from '../useSchemaPersistence';
|
|
16
|
-
|
|
17
|
-
// Mock localStorage
|
|
18
|
-
const localStorageMock = (() => {
|
|
19
|
-
let store: Record<string, string> = {};
|
|
20
|
-
return {
|
|
21
|
-
getItem: (key: string) => store[key] ?? null,
|
|
22
|
-
setItem: (key: string, value: string) => { store[key] = value; },
|
|
23
|
-
removeItem: (key: string) => { delete store[key]; },
|
|
24
|
-
clear: () => { store = {}; },
|
|
25
|
-
};
|
|
26
|
-
})();
|
|
27
|
-
|
|
28
|
-
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
|
|
29
|
-
|
|
30
|
-
describe('useSchemaPersistence', () => {
|
|
31
|
-
beforeEach(() => {
|
|
32
|
-
localStorageMock.clear();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('should initialize with default state', () => {
|
|
36
|
-
const { result } = renderHook(() => useSchemaPersistence());
|
|
37
|
-
expect(result.current.loading).toBe(false);
|
|
38
|
-
expect(result.current.error).toBeNull();
|
|
39
|
-
expect(result.current.isDirty).toBe(false);
|
|
40
|
-
expect(result.current.lastSavedAt).toBeNull();
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should save and load a schema with default localStorage adapter', async () => {
|
|
44
|
-
const { result } = renderHook(() => useSchemaPersistence());
|
|
45
|
-
const testSchema = { type: 'page', title: 'My Page', regions: [] };
|
|
46
|
-
|
|
47
|
-
let savedId: string | null = null;
|
|
48
|
-
await act(async () => {
|
|
49
|
-
savedId = await result.current.save('test-page', testSchema);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
expect(savedId).toBe('test-page');
|
|
53
|
-
expect(result.current.isDirty).toBe(false);
|
|
54
|
-
expect(result.current.lastSavedAt).not.toBeNull();
|
|
55
|
-
|
|
56
|
-
let loaded: Record<string, unknown> | null = null;
|
|
57
|
-
await act(async () => {
|
|
58
|
-
loaded = await result.current.load('test-page');
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
expect(loaded).toEqual(testSchema);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should list saved schemas', async () => {
|
|
65
|
-
const { result } = renderHook(() => useSchemaPersistence());
|
|
66
|
-
|
|
67
|
-
await act(async () => {
|
|
68
|
-
await result.current.save('schema-1', { type: 'page' });
|
|
69
|
-
await result.current.save('schema-2', { type: 'form' });
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
let items: Array<{ id: string }> = [];
|
|
73
|
-
await act(async () => {
|
|
74
|
-
items = await result.current.list();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
expect(items).toHaveLength(2);
|
|
78
|
-
expect(items.map((i) => i.id)).toEqual(['schema-1', 'schema-2']);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should delete a schema', async () => {
|
|
82
|
-
const { result } = renderHook(() => useSchemaPersistence());
|
|
83
|
-
|
|
84
|
-
await act(async () => {
|
|
85
|
-
await result.current.save('to-delete', { type: 'grid' });
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
let success = false;
|
|
89
|
-
await act(async () => {
|
|
90
|
-
success = await result.current.remove('to-delete');
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
expect(success).toBe(true);
|
|
94
|
-
|
|
95
|
-
let loaded: Record<string, unknown> | null = null;
|
|
96
|
-
await act(async () => {
|
|
97
|
-
loaded = await result.current.load('to-delete');
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
expect(loaded).toBeNull();
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should track dirty state', () => {
|
|
104
|
-
const { result } = renderHook(() => useSchemaPersistence());
|
|
105
|
-
|
|
106
|
-
expect(result.current.isDirty).toBe(false);
|
|
107
|
-
|
|
108
|
-
act(() => {
|
|
109
|
-
result.current.markDirty();
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
expect(result.current.isDirty).toBe(true);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('should clear dirty state after save', async () => {
|
|
116
|
-
const { result } = renderHook(() => useSchemaPersistence());
|
|
117
|
-
|
|
118
|
-
act(() => {
|
|
119
|
-
result.current.markDirty();
|
|
120
|
-
});
|
|
121
|
-
expect(result.current.isDirty).toBe(true);
|
|
122
|
-
|
|
123
|
-
await act(async () => {
|
|
124
|
-
await result.current.save('test', { type: 'page' });
|
|
125
|
-
});
|
|
126
|
-
expect(result.current.isDirty).toBe(false);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('should handle save errors gracefully', async () => {
|
|
130
|
-
const failingAdapter: SchemaPersistenceAdapter = {
|
|
131
|
-
save: () => Promise.reject(new Error('Network error')),
|
|
132
|
-
load: () => Promise.resolve(null),
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
const { result } = renderHook(() => useSchemaPersistence(failingAdapter));
|
|
136
|
-
|
|
137
|
-
await act(async () => {
|
|
138
|
-
await result.current.save('test', { type: 'page' });
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
expect(result.current.error).not.toBeNull();
|
|
142
|
-
expect(result.current.error?.message).toBe('Network error');
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it('should clear errors', async () => {
|
|
146
|
-
const failingAdapter: SchemaPersistenceAdapter = {
|
|
147
|
-
save: () => Promise.reject(new Error('fail')),
|
|
148
|
-
load: () => Promise.resolve(null),
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
const { result } = renderHook(() => useSchemaPersistence(failingAdapter));
|
|
152
|
-
|
|
153
|
-
await act(async () => {
|
|
154
|
-
await result.current.save('test', { type: 'page' });
|
|
155
|
-
});
|
|
156
|
-
expect(result.current.error).not.toBeNull();
|
|
157
|
-
|
|
158
|
-
act(() => {
|
|
159
|
-
result.current.clearError();
|
|
160
|
-
});
|
|
161
|
-
expect(result.current.error).toBeNull();
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it('should work with custom adapter', async () => {
|
|
165
|
-
const mockStore: Record<string, Record<string, unknown>> = {};
|
|
166
|
-
const customAdapter: SchemaPersistenceAdapter = {
|
|
167
|
-
save: async (id, schema) => { mockStore[id] = schema; return id; },
|
|
168
|
-
load: async (id) => mockStore[id] ?? null,
|
|
169
|
-
list: async () => Object.keys(mockStore).map((id) => ({ id })),
|
|
170
|
-
delete: async (id) => { delete mockStore[id]; },
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
const { result } = renderHook(() => useSchemaPersistence(customAdapter));
|
|
174
|
-
|
|
175
|
-
await act(async () => {
|
|
176
|
-
await result.current.save('custom-1', { type: 'dashboard' });
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
let loaded: Record<string, unknown> | null = null;
|
|
180
|
-
await act(async () => {
|
|
181
|
-
loaded = await result.current.load('custom-1');
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
expect(loaded).toEqual({ type: 'dashboard' });
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
describe('createLocalStorageAdapter', () => {
|
|
189
|
-
beforeEach(() => {
|
|
190
|
-
localStorageMock.clear();
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it('should create an adapter with custom prefix', async () => {
|
|
194
|
-
const adapter = createLocalStorageAdapter('my-prefix');
|
|
195
|
-
|
|
196
|
-
await adapter.save('test', { hello: 'world' });
|
|
197
|
-
const result = await adapter.load('test');
|
|
198
|
-
expect(result).toEqual({ hello: 'world' });
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('should maintain an index of saved schemas', async () => {
|
|
202
|
-
const adapter = createLocalStorageAdapter('test-prefix');
|
|
203
|
-
|
|
204
|
-
await adapter.save('a', { type: 'a' });
|
|
205
|
-
await adapter.save('b', { type: 'b' });
|
|
206
|
-
|
|
207
|
-
const items = await adapter.list!();
|
|
208
|
-
expect(items).toHaveLength(2);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it('should not duplicate IDs in index', async () => {
|
|
212
|
-
const adapter = createLocalStorageAdapter('test-prefix');
|
|
213
|
-
|
|
214
|
-
await adapter.save('same-id', { v: 1 });
|
|
215
|
-
await adapter.save('same-id', { v: 2 });
|
|
216
|
-
|
|
217
|
-
const items = await adapter.list!();
|
|
218
|
-
expect(items).toHaveLength(1);
|
|
219
|
-
});
|
|
220
|
-
});
|