@object-ui/react 2.0.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 +19 -0
- package/dist/SchemaRenderer.d.ts +20 -1
- package/dist/SchemaRenderer.js +76 -13
- 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/index.d.ts +2 -0
- package/dist/context/index.js +2 -0
- package/dist/hooks/index.d.ts +17 -0
- package/dist/hooks/index.js +17 -0
- 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/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/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/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/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/package.json +7 -7
- package/src/SchemaRenderer.tsx +122 -13
- 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__/useOffline.test.ts +198 -0
- package/src/__tests__/usePageTransition.test.ts +214 -0
- package/src/__tests__/usePerformance.test.ts +137 -0
- package/src/__tests__/view-switching.test.tsx +456 -0
- package/src/context/DndContext.tsx +260 -0
- package/src/context/NotificationContext.tsx +280 -0
- package/src/context/__tests__/DndContext.test.tsx +150 -0
- package/src/context/__tests__/NotificationContext.test.tsx +176 -0
- package/src/context/index.ts +2 -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__/useETagCache.test.ts +153 -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 +17 -1
- 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/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/useOffline.ts +301 -0
- package/src/hooks/usePageTransition.ts +187 -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/useViewSharing.ts +140 -0
- package/src/hooks/useViewTransition.ts +144 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useColumnSummary hook
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect } from 'vitest';
|
|
5
|
+
import { renderHook } from '@testing-library/react';
|
|
6
|
+
import { useColumnSummary } from '../useColumnSummary';
|
|
7
|
+
|
|
8
|
+
const sampleData = [
|
|
9
|
+
{ name: 'Alice', amount: 100, category: 'A' },
|
|
10
|
+
{ name: 'Bob', amount: 200, category: 'B' },
|
|
11
|
+
{ name: 'Charlie', amount: 300, category: 'A' },
|
|
12
|
+
{ name: 'Diana', amount: 400, category: 'C' },
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
describe('useColumnSummary', () => {
|
|
16
|
+
it('computes sum correctly', () => {
|
|
17
|
+
const { result } = renderHook(() =>
|
|
18
|
+
useColumnSummary(sampleData, { field: 'amount', function: 'sum' })
|
|
19
|
+
);
|
|
20
|
+
expect(result.current.value).toBe(1000);
|
|
21
|
+
expect(result.current.label).toBe('SUM');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('computes avg correctly', () => {
|
|
25
|
+
const { result } = renderHook(() =>
|
|
26
|
+
useColumnSummary(sampleData, { field: 'amount', function: 'avg' })
|
|
27
|
+
);
|
|
28
|
+
expect(result.current.value).toBe(250);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('computes count correctly', () => {
|
|
32
|
+
const { result } = renderHook(() =>
|
|
33
|
+
useColumnSummary(sampleData, { field: 'amount', function: 'count' })
|
|
34
|
+
);
|
|
35
|
+
expect(result.current.value).toBe(4);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('computes min correctly', () => {
|
|
39
|
+
const { result } = renderHook(() =>
|
|
40
|
+
useColumnSummary(sampleData, { field: 'amount', function: 'min' })
|
|
41
|
+
);
|
|
42
|
+
expect(result.current.value).toBe(100);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('computes max correctly', () => {
|
|
46
|
+
const { result } = renderHook(() =>
|
|
47
|
+
useColumnSummary(sampleData, { field: 'amount', function: 'max' })
|
|
48
|
+
);
|
|
49
|
+
expect(result.current.value).toBe(400);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('computes distinct correctly', () => {
|
|
53
|
+
const { result } = renderHook(() =>
|
|
54
|
+
useColumnSummary(sampleData, { field: 'category', function: 'distinct' })
|
|
55
|
+
);
|
|
56
|
+
expect(result.current.value).toBe(3); // A, B, C
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('uses custom label', () => {
|
|
60
|
+
const { result } = renderHook(() =>
|
|
61
|
+
useColumnSummary(sampleData, { field: 'amount', function: 'sum', label: 'Total Revenue' })
|
|
62
|
+
);
|
|
63
|
+
expect(result.current.label).toBe('Total Revenue');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('returns formatted string', () => {
|
|
67
|
+
const { result } = renderHook(() =>
|
|
68
|
+
useColumnSummary(sampleData, { field: 'amount', function: 'sum' }, 'en-US')
|
|
69
|
+
);
|
|
70
|
+
expect(result.current.formatted).toBe('1,000');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('handles empty data for sum', () => {
|
|
74
|
+
const { result } = renderHook(() =>
|
|
75
|
+
useColumnSummary([], { field: 'amount', function: 'sum' })
|
|
76
|
+
);
|
|
77
|
+
expect(result.current.value).toBe(0);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('handles empty data for avg', () => {
|
|
81
|
+
const { result } = renderHook(() =>
|
|
82
|
+
useColumnSummary([], { field: 'amount', function: 'avg' })
|
|
83
|
+
);
|
|
84
|
+
expect(result.current.value).toBe(0);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('handles empty data for min', () => {
|
|
88
|
+
const { result } = renderHook(() =>
|
|
89
|
+
useColumnSummary([], { field: 'amount', function: 'min' })
|
|
90
|
+
);
|
|
91
|
+
expect(result.current.value).toBe(0);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useDensityMode hook
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect } from 'vitest';
|
|
5
|
+
import { renderHook, act } from '@testing-library/react';
|
|
6
|
+
import { useDensityMode } from '../useDensityMode';
|
|
7
|
+
|
|
8
|
+
describe('useDensityMode', () => {
|
|
9
|
+
it('defaults to comfortable mode', () => {
|
|
10
|
+
const { result } = renderHook(() => useDensityMode());
|
|
11
|
+
expect(result.current.mode).toBe('comfortable');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('accepts initial mode', () => {
|
|
15
|
+
const { result } = renderHook(() => useDensityMode('compact'));
|
|
16
|
+
expect(result.current.mode).toBe('compact');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('provides correct row height for compact', () => {
|
|
20
|
+
const { result } = renderHook(() => useDensityMode('compact'));
|
|
21
|
+
expect(result.current.rowHeight).toBe(32);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('provides correct row height for comfortable', () => {
|
|
25
|
+
const { result } = renderHook(() => useDensityMode('comfortable'));
|
|
26
|
+
expect(result.current.rowHeight).toBe(40);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('provides correct row height for spacious', () => {
|
|
30
|
+
const { result } = renderHook(() => useDensityMode('spacious'));
|
|
31
|
+
expect(result.current.rowHeight).toBe(52);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('provides padding classes', () => {
|
|
35
|
+
const { result } = renderHook(() => useDensityMode('compact'));
|
|
36
|
+
expect(result.current.paddingClass).toContain('py-1');
|
|
37
|
+
expect(result.current.paddingClass).toContain('px-2');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('provides font size classes', () => {
|
|
41
|
+
const { result } = renderHook(() => useDensityMode('compact'));
|
|
42
|
+
expect(result.current.fontSizeClass).toBe('text-xs');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('cycles through modes', () => {
|
|
46
|
+
const { result } = renderHook(() => useDensityMode('compact'));
|
|
47
|
+
|
|
48
|
+
act(() => {
|
|
49
|
+
result.current.cycle();
|
|
50
|
+
});
|
|
51
|
+
expect(result.current.mode).toBe('comfortable');
|
|
52
|
+
|
|
53
|
+
act(() => {
|
|
54
|
+
result.current.cycle();
|
|
55
|
+
});
|
|
56
|
+
expect(result.current.mode).toBe('spacious');
|
|
57
|
+
|
|
58
|
+
act(() => {
|
|
59
|
+
result.current.cycle();
|
|
60
|
+
});
|
|
61
|
+
expect(result.current.mode).toBe('compact');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('allows setting mode directly', () => {
|
|
65
|
+
const { result } = renderHook(() => useDensityMode());
|
|
66
|
+
|
|
67
|
+
act(() => {
|
|
68
|
+
result.current.setMode('spacious');
|
|
69
|
+
});
|
|
70
|
+
expect(result.current.mode).toBe('spacious');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('accepts custom row heights', () => {
|
|
74
|
+
const { result } = renderHook(() =>
|
|
75
|
+
useDensityMode('compact', {
|
|
76
|
+
rowHeights: { compact: 24, comfortable: 36, spacious: 48 },
|
|
77
|
+
})
|
|
78
|
+
);
|
|
79
|
+
expect(result.current.rowHeight).toBe(24);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useETagCache hook
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
5
|
+
import { renderHook, act } from '@testing-library/react';
|
|
6
|
+
import { useETagCache } from '../useETagCache';
|
|
7
|
+
|
|
8
|
+
describe('useETagCache', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
vi.restoreAllMocks();
|
|
11
|
+
// Clear the module-scoped memory cache between tests
|
|
12
|
+
const { result } = renderHook(() => useETagCache());
|
|
13
|
+
act(() => {
|
|
14
|
+
result.current.clearCache();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
vi.restoreAllMocks();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('returns initial state with cacheSize: 0', () => {
|
|
23
|
+
const { result } = renderHook(() => useETagCache());
|
|
24
|
+
|
|
25
|
+
expect(result.current.cacheSize).toBe(0);
|
|
26
|
+
expect(result.current.serviceWorkerActive).toBe(false);
|
|
27
|
+
expect(result.current.fetchWithETag).toBeTypeOf('function');
|
|
28
|
+
expect(result.current.invalidate).toBeTypeOf('function');
|
|
29
|
+
expect(result.current.clearCache).toBeTypeOf('function');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('fetchWithETag makes a network request and caches the response', async () => {
|
|
33
|
+
const mockData = { id: 1, name: 'Test' };
|
|
34
|
+
const mockHeaders = new Headers({ etag: '"abc123"' });
|
|
35
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce(
|
|
36
|
+
new Response(JSON.stringify(mockData), {
|
|
37
|
+
status: 200,
|
|
38
|
+
headers: mockHeaders,
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const { result } = renderHook(() => useETagCache());
|
|
43
|
+
|
|
44
|
+
let response: { data: unknown; fromCache: boolean; etag?: string } | undefined;
|
|
45
|
+
await act(async () => {
|
|
46
|
+
response = await result.current.fetchWithETag('/api/test');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
expect(response!.data).toEqual(mockData);
|
|
50
|
+
expect(response!.fromCache).toBe(false);
|
|
51
|
+
expect(response!.etag).toBe('"abc123"');
|
|
52
|
+
expect(result.current.cacheSize).toBe(1);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('fetchWithETag sends If-None-Match header on subsequent requests', async () => {
|
|
56
|
+
const mockData = { id: 1 };
|
|
57
|
+
const fetchSpy = vi.spyOn(globalThis, 'fetch');
|
|
58
|
+
|
|
59
|
+
// First request — returns data with ETag
|
|
60
|
+
fetchSpy.mockResolvedValueOnce(
|
|
61
|
+
new Response(JSON.stringify(mockData), {
|
|
62
|
+
status: 200,
|
|
63
|
+
headers: new Headers({ etag: '"etag-1"' }),
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const { result } = renderHook(() => useETagCache());
|
|
68
|
+
|
|
69
|
+
await act(async () => {
|
|
70
|
+
await result.current.fetchWithETag('/api/data');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Second request — server returns 304
|
|
74
|
+
fetchSpy.mockResolvedValueOnce(
|
|
75
|
+
new Response(null, { status: 304 }),
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
let response: { data: unknown; fromCache: boolean; etag?: string } | undefined;
|
|
79
|
+
await act(async () => {
|
|
80
|
+
response = await result.current.fetchWithETag('/api/data');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Verify If-None-Match was sent
|
|
84
|
+
const secondCallArgs = fetchSpy.mock.calls[1];
|
|
85
|
+
const headers = secondCallArgs[1]?.headers as Headers;
|
|
86
|
+
expect(headers.get('If-None-Match')).toBe('"etag-1"');
|
|
87
|
+
expect(response!.fromCache).toBe(true);
|
|
88
|
+
expect(response!.data).toEqual(mockData);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('invalidate removes a specific cache entry', async () => {
|
|
92
|
+
const mockData = { id: 1 };
|
|
93
|
+
vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce(
|
|
94
|
+
new Response(JSON.stringify(mockData), {
|
|
95
|
+
status: 200,
|
|
96
|
+
headers: new Headers({ etag: '"etag-x"' }),
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const { result } = renderHook(() => useETagCache());
|
|
101
|
+
|
|
102
|
+
await act(async () => {
|
|
103
|
+
await result.current.fetchWithETag('/api/item');
|
|
104
|
+
});
|
|
105
|
+
expect(result.current.cacheSize).toBe(1);
|
|
106
|
+
|
|
107
|
+
act(() => {
|
|
108
|
+
result.current.invalidate('/api/item');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(result.current.cacheSize).toBe(0);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('clearCache removes all entries', async () => {
|
|
115
|
+
const fetchSpy = vi.spyOn(globalThis, 'fetch');
|
|
116
|
+
|
|
117
|
+
fetchSpy.mockResolvedValueOnce(
|
|
118
|
+
new Response(JSON.stringify({ a: 1 }), {
|
|
119
|
+
status: 200,
|
|
120
|
+
headers: new Headers({ etag: '"e1"' }),
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
fetchSpy.mockResolvedValueOnce(
|
|
124
|
+
new Response(JSON.stringify({ b: 2 }), {
|
|
125
|
+
status: 200,
|
|
126
|
+
headers: new Headers({ etag: '"e2"' }),
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
const { result } = renderHook(() => useETagCache());
|
|
131
|
+
|
|
132
|
+
await act(async () => {
|
|
133
|
+
await result.current.fetchWithETag('/api/a');
|
|
134
|
+
});
|
|
135
|
+
await act(async () => {
|
|
136
|
+
await result.current.fetchWithETag('/api/b');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(result.current.cacheSize).toBe(2);
|
|
140
|
+
|
|
141
|
+
act(() => {
|
|
142
|
+
result.current.clearCache();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
expect(result.current.cacheSize).toBe(0);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('serviceWorkerActive is false initially', () => {
|
|
149
|
+
const { result } = renderHook(() => useETagCache());
|
|
150
|
+
|
|
151
|
+
expect(result.current.serviceWorkerActive).toBe(false);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useFocusTrap hook
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
5
|
+
import { renderHook, act } from '@testing-library/react';
|
|
6
|
+
import { useFocusTrap } from '../useFocusTrap';
|
|
7
|
+
|
|
8
|
+
describe('useFocusTrap', () => {
|
|
9
|
+
it('returns a ref object', () => {
|
|
10
|
+
const { result } = renderHook(() => useFocusTrap());
|
|
11
|
+
expect(result.current).toBeDefined();
|
|
12
|
+
expect(result.current).toHaveProperty('current');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('returns a ref with null initial value', () => {
|
|
16
|
+
const { result } = renderHook(() => useFocusTrap());
|
|
17
|
+
expect(result.current.current).toBeNull();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('accepts disabled option', () => {
|
|
21
|
+
const { result } = renderHook(() =>
|
|
22
|
+
useFocusTrap({ enabled: false })
|
|
23
|
+
);
|
|
24
|
+
expect(result.current).toBeDefined();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('accepts all options without error', () => {
|
|
28
|
+
const onEscape = vi.fn();
|
|
29
|
+
const { result } = renderHook(() =>
|
|
30
|
+
useFocusTrap({
|
|
31
|
+
enabled: true,
|
|
32
|
+
autoFocus: true,
|
|
33
|
+
restoreFocus: true,
|
|
34
|
+
escapeDeactivates: true,
|
|
35
|
+
onEscape,
|
|
36
|
+
initialFocus: '.first-input',
|
|
37
|
+
})
|
|
38
|
+
);
|
|
39
|
+
expect(result.current).toBeDefined();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('defaults to enabled when no options provided', () => {
|
|
43
|
+
const { result } = renderHook(() => useFocusTrap());
|
|
44
|
+
// Should not throw
|
|
45
|
+
expect(result.current.current).toBeNull();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useKeyboardShortcuts hook
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
5
|
+
import { renderHook } from '@testing-library/react';
|
|
6
|
+
import { useKeyboardShortcuts, getShortcutDescriptions } from '../useKeyboardShortcuts';
|
|
7
|
+
import type { KeyboardShortcutDefinition } from '../useKeyboardShortcuts';
|
|
8
|
+
|
|
9
|
+
describe('useKeyboardShortcuts', () => {
|
|
10
|
+
it('registers shortcuts without error', () => {
|
|
11
|
+
const handler = vi.fn();
|
|
12
|
+
const shortcuts: KeyboardShortcutDefinition[] = [
|
|
13
|
+
{ key: 'k', ctrlOrMeta: true, handler, description: 'Search' },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
expect(() => {
|
|
17
|
+
renderHook(() => useKeyboardShortcuts(shortcuts));
|
|
18
|
+
}).not.toThrow();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('handles empty shortcuts array', () => {
|
|
22
|
+
expect(() => {
|
|
23
|
+
renderHook(() => useKeyboardShortcuts([]));
|
|
24
|
+
}).not.toThrow();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('respects enabled option', () => {
|
|
28
|
+
const handler = vi.fn();
|
|
29
|
+
const shortcuts: KeyboardShortcutDefinition[] = [
|
|
30
|
+
{ key: 'k', handler },
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
expect(() => {
|
|
34
|
+
renderHook(() => useKeyboardShortcuts(shortcuts, { enabled: false }));
|
|
35
|
+
}).not.toThrow();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('fires handler on matching keyboard event', () => {
|
|
39
|
+
const handler = vi.fn();
|
|
40
|
+
const shortcuts: KeyboardShortcutDefinition[] = [
|
|
41
|
+
{ key: 'Escape', handler },
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
renderHook(() => useKeyboardShortcuts(shortcuts));
|
|
45
|
+
|
|
46
|
+
// Simulate keydown
|
|
47
|
+
const event = new KeyboardEvent('keydown', {
|
|
48
|
+
key: 'Escape',
|
|
49
|
+
bubbles: true,
|
|
50
|
+
});
|
|
51
|
+
document.dispatchEvent(event);
|
|
52
|
+
|
|
53
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('does not fire handler for non-matching key', () => {
|
|
57
|
+
const handler = vi.fn();
|
|
58
|
+
const shortcuts: KeyboardShortcutDefinition[] = [
|
|
59
|
+
{ key: 'k', ctrlOrMeta: true, handler },
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
renderHook(() => useKeyboardShortcuts(shortcuts));
|
|
63
|
+
|
|
64
|
+
const event = new KeyboardEvent('keydown', {
|
|
65
|
+
key: 'k',
|
|
66
|
+
bubbles: true,
|
|
67
|
+
// No ctrl or meta
|
|
68
|
+
});
|
|
69
|
+
document.dispatchEvent(event);
|
|
70
|
+
|
|
71
|
+
expect(handler).not.toHaveBeenCalled();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('fires handler when modifier keys match', () => {
|
|
75
|
+
const handler = vi.fn();
|
|
76
|
+
const shortcuts: KeyboardShortcutDefinition[] = [
|
|
77
|
+
{ key: 'k', ctrlOrMeta: true, handler },
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
renderHook(() => useKeyboardShortcuts(shortcuts));
|
|
81
|
+
|
|
82
|
+
const event = new KeyboardEvent('keydown', {
|
|
83
|
+
key: 'k',
|
|
84
|
+
ctrlKey: true,
|
|
85
|
+
bubbles: true,
|
|
86
|
+
});
|
|
87
|
+
document.dispatchEvent(event);
|
|
88
|
+
|
|
89
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('skips disabled shortcuts', () => {
|
|
93
|
+
const handler = vi.fn();
|
|
94
|
+
const shortcuts: KeyboardShortcutDefinition[] = [
|
|
95
|
+
{ key: 'k', handler, enabled: false },
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
renderHook(() => useKeyboardShortcuts(shortcuts));
|
|
99
|
+
|
|
100
|
+
const event = new KeyboardEvent('keydown', {
|
|
101
|
+
key: 'k',
|
|
102
|
+
bubbles: true,
|
|
103
|
+
});
|
|
104
|
+
document.dispatchEvent(event);
|
|
105
|
+
|
|
106
|
+
expect(handler).not.toHaveBeenCalled();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('getShortcutDescriptions', () => {
|
|
111
|
+
it('returns formatted descriptions', () => {
|
|
112
|
+
const shortcuts: KeyboardShortcutDefinition[] = [
|
|
113
|
+
{ key: 'k', ctrlOrMeta: true, handler: vi.fn(), description: 'Search' },
|
|
114
|
+
{ key: 'n', ctrlOrMeta: true, shift: true, handler: vi.fn(), description: 'New' },
|
|
115
|
+
{ key: 'Escape', handler: vi.fn(), description: 'Close' },
|
|
116
|
+
];
|
|
117
|
+
|
|
118
|
+
const descriptions = getShortcutDescriptions(shortcuts);
|
|
119
|
+
|
|
120
|
+
expect(descriptions).toHaveLength(3);
|
|
121
|
+
expect(descriptions[0]).toEqual({ keys: '⌘/Ctrl + K', description: 'Search' });
|
|
122
|
+
expect(descriptions[1]).toEqual({ keys: '⌘/Ctrl + Shift + N', description: 'New' });
|
|
123
|
+
expect(descriptions[2]).toEqual({ keys: 'Escape', description: 'Close' });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('excludes shortcuts without descriptions', () => {
|
|
127
|
+
const shortcuts: KeyboardShortcutDefinition[] = [
|
|
128
|
+
{ key: 'k', handler: vi.fn() },
|
|
129
|
+
{ key: 'n', handler: vi.fn(), description: 'New' },
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
const descriptions = getShortcutDescriptions(shortcuts);
|
|
133
|
+
expect(descriptions).toHaveLength(1);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('excludes disabled shortcuts', () => {
|
|
137
|
+
const shortcuts: KeyboardShortcutDefinition[] = [
|
|
138
|
+
{ key: 'k', handler: vi.fn(), description: 'Search', enabled: false },
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
const descriptions = getShortcutDescriptions(shortcuts);
|
|
142
|
+
expect(descriptions).toHaveLength(0);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
});
|