@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,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for ActionContext — ActionProvider, useAction, useHasActionProvider
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
6
|
+
import { renderHook, waitFor, act } from '@testing-library/react';
|
|
7
|
+
import React from 'react';
|
|
8
|
+
import { ActionProvider, useAction, useHasActionProvider } from '../ActionContext';
|
|
9
|
+
|
|
10
|
+
describe('ActionContext', () => {
|
|
11
|
+
describe('useAction without ActionProvider', () => {
|
|
12
|
+
it('falls back to local ActionRunner with default state', () => {
|
|
13
|
+
const { result } = renderHook(() => useAction());
|
|
14
|
+
|
|
15
|
+
expect(result.current.loading).toBe(false);
|
|
16
|
+
expect(result.current.error).toBeNull();
|
|
17
|
+
expect(result.current.result).toBeNull();
|
|
18
|
+
expect(typeof result.current.execute).toBe('function');
|
|
19
|
+
expect(typeof result.current.executeChain).toBe('function');
|
|
20
|
+
expect(typeof result.current.updateContext).toBe('function');
|
|
21
|
+
expect(result.current.runner).toBeDefined();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('useHasActionProvider', () => {
|
|
26
|
+
it('returns false without provider', () => {
|
|
27
|
+
const { result } = renderHook(() => useHasActionProvider());
|
|
28
|
+
|
|
29
|
+
expect(result.current).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('returns true with provider', () => {
|
|
33
|
+
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
34
|
+
React.createElement(ActionProvider, { context: {} }, children);
|
|
35
|
+
|
|
36
|
+
const { result } = renderHook(() => useHasActionProvider(), { wrapper });
|
|
37
|
+
|
|
38
|
+
expect(result.current).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('ActionProvider basic', () => {
|
|
43
|
+
it('children can access execute function', () => {
|
|
44
|
+
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
45
|
+
React.createElement(ActionProvider, { context: {} }, children);
|
|
46
|
+
|
|
47
|
+
const { result } = renderHook(() => useAction(), { wrapper });
|
|
48
|
+
|
|
49
|
+
expect(typeof result.current.execute).toBe('function');
|
|
50
|
+
expect(typeof result.current.executeChain).toBe('function');
|
|
51
|
+
expect(typeof result.current.updateContext).toBe('function');
|
|
52
|
+
expect(result.current.loading).toBe(false);
|
|
53
|
+
expect(result.current.error).toBeNull();
|
|
54
|
+
expect(result.current.result).toBeNull();
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('ActionProvider execute', () => {
|
|
59
|
+
it('executes a toast action and returns result', async () => {
|
|
60
|
+
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
61
|
+
React.createElement(ActionProvider, { context: {} }, children);
|
|
62
|
+
|
|
63
|
+
const { result } = renderHook(() => useAction(), { wrapper });
|
|
64
|
+
|
|
65
|
+
let actionResult: any;
|
|
66
|
+
await act(async () => {
|
|
67
|
+
actionResult = await result.current.execute({
|
|
68
|
+
type: 'script',
|
|
69
|
+
execute: 'true',
|
|
70
|
+
toast: { showOnSuccess: true },
|
|
71
|
+
successMessage: 'Done!',
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
expect(actionResult.success).toBe(true);
|
|
76
|
+
|
|
77
|
+
await waitFor(() => {
|
|
78
|
+
expect(result.current.loading).toBe(false);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
expect(result.current.result).toEqual(actionResult);
|
|
82
|
+
expect(result.current.error).toBeNull();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('ActionProvider execute error', () => {
|
|
87
|
+
it('executes an action that fails and sets error state', async () => {
|
|
88
|
+
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
89
|
+
React.createElement(ActionProvider, { context: {} }, children);
|
|
90
|
+
|
|
91
|
+
const { result } = renderHook(() => useAction(), { wrapper });
|
|
92
|
+
|
|
93
|
+
let actionResult: any;
|
|
94
|
+
await act(async () => {
|
|
95
|
+
actionResult = await result.current.execute({
|
|
96
|
+
onClick: vi.fn().mockRejectedValue(new Error('boom')),
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
await waitFor(() => {
|
|
101
|
+
expect(result.current.loading).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
expect(actionResult.success).toBe(false);
|
|
105
|
+
expect(result.current.error).toBeTruthy();
|
|
106
|
+
expect(result.current.result?.success).toBe(false);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('ActionProvider executeChain', () => {
|
|
111
|
+
it('executes a chain of actions', async () => {
|
|
112
|
+
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
113
|
+
React.createElement(ActionProvider, { context: {} }, children);
|
|
114
|
+
|
|
115
|
+
const { result } = renderHook(() => useAction(), { wrapper });
|
|
116
|
+
|
|
117
|
+
let chainResult: any;
|
|
118
|
+
await act(async () => {
|
|
119
|
+
chainResult = await result.current.executeChain([
|
|
120
|
+
{ type: 'script', execute: 'true' },
|
|
121
|
+
{ type: 'script', execute: 'true' },
|
|
122
|
+
]);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
await waitFor(() => {
|
|
126
|
+
expect(result.current.loading).toBe(false);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(chainResult.success).toBe(true);
|
|
130
|
+
expect(result.current.result).toEqual(chainResult);
|
|
131
|
+
expect(result.current.error).toBeNull();
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe('ActionProvider updateContext', () => {
|
|
136
|
+
it('updateContext is callable', () => {
|
|
137
|
+
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
138
|
+
React.createElement(ActionProvider, { context: {} }, children);
|
|
139
|
+
|
|
140
|
+
const { result } = renderHook(() => useAction(), { wrapper });
|
|
141
|
+
|
|
142
|
+
expect(() => {
|
|
143
|
+
result.current.updateContext({ record: { id: 1 } });
|
|
144
|
+
}).not.toThrow();
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe('ActionProvider with handlers', () => {
|
|
149
|
+
it('invokes onToast handler when a toast action is executed', async () => {
|
|
150
|
+
const onToast = vi.fn();
|
|
151
|
+
const onNavigate = vi.fn();
|
|
152
|
+
|
|
153
|
+
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
|
154
|
+
React.createElement(
|
|
155
|
+
ActionProvider,
|
|
156
|
+
{ context: {}, onToast, onNavigate },
|
|
157
|
+
children,
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const { result } = renderHook(() => useAction(), { wrapper });
|
|
161
|
+
|
|
162
|
+
await act(async () => {
|
|
163
|
+
await result.current.execute({
|
|
164
|
+
type: 'script',
|
|
165
|
+
execute: 'true',
|
|
166
|
+
toast: { showOnSuccess: true },
|
|
167
|
+
successMessage: 'Saved!',
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
expect(onToast).toHaveBeenCalledWith('Saved!', expect.objectContaining({ type: 'success' }));
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for DndProvider and useDnd
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
5
|
+
import { renderHook, act } from '@testing-library/react';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
import {
|
|
8
|
+
DndProvider,
|
|
9
|
+
useDnd,
|
|
10
|
+
useHasDndProvider,
|
|
11
|
+
} from '../DndContext';
|
|
12
|
+
|
|
13
|
+
const wrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
14
|
+
<DndProvider>{children}</DndProvider>
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
describe('DndProvider', () => {
|
|
18
|
+
it('provides DnD context', () => {
|
|
19
|
+
const { result } = renderHook(() => useDnd(), { wrapper });
|
|
20
|
+
expect(result.current.enabled).toBe(true);
|
|
21
|
+
expect(result.current.activeItem).toBeNull();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('starts drag operation', () => {
|
|
25
|
+
const onDragStart = vi.fn();
|
|
26
|
+
const customWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
27
|
+
<DndProvider onDragStart={onDragStart}>{children}</DndProvider>
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const { result } = renderHook(() => useDnd(), { wrapper: customWrapper });
|
|
31
|
+
|
|
32
|
+
act(() => {
|
|
33
|
+
result.current.startDrag({ id: 'item-1', type: 'card' });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
expect(result.current.activeItem).toEqual({ id: 'item-1', type: 'card' });
|
|
37
|
+
expect(onDragStart).toHaveBeenCalledTimes(1);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('ends drag operation', () => {
|
|
41
|
+
const onDrop = vi.fn();
|
|
42
|
+
const customWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
43
|
+
<DndProvider onDrop={onDrop}>{children}</DndProvider>
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const { result } = renderHook(() => useDnd(), { wrapper: customWrapper });
|
|
47
|
+
|
|
48
|
+
act(() => {
|
|
49
|
+
result.current.startDrag({ id: 'item-1', type: 'card' });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
act(() => {
|
|
53
|
+
result.current.endDrag('zone-1');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
expect(result.current.activeItem).toBeNull();
|
|
57
|
+
expect(onDrop).toHaveBeenCalledTimes(1);
|
|
58
|
+
expect(onDrop).toHaveBeenCalledWith(
|
|
59
|
+
expect.objectContaining({ item: { id: 'item-1', type: 'card' }, targetZoneId: 'zone-1' })
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('cancels drag operation', () => {
|
|
64
|
+
const onDragCancel = vi.fn();
|
|
65
|
+
const customWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
66
|
+
<DndProvider onDragCancel={onDragCancel}>{children}</DndProvider>
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const { result } = renderHook(() => useDnd(), { wrapper: customWrapper });
|
|
70
|
+
|
|
71
|
+
act(() => {
|
|
72
|
+
result.current.startDrag({ id: 'item-1', type: 'card' });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
act(() => {
|
|
76
|
+
result.current.cancelDrag();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
expect(result.current.activeItem).toBeNull();
|
|
80
|
+
expect(onDragCancel).toHaveBeenCalledTimes(1);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('registers and checks drop zones', () => {
|
|
84
|
+
const { result } = renderHook(() => useDnd(), { wrapper });
|
|
85
|
+
|
|
86
|
+
act(() => {
|
|
87
|
+
result.current.registerDropZone({
|
|
88
|
+
id: 'zone-1',
|
|
89
|
+
acceptTypes: ['card', 'widget'],
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
expect(result.current.canDrop('zone-1', 'card')).toBe(true);
|
|
94
|
+
expect(result.current.canDrop('zone-1', 'unknown')).toBe(false);
|
|
95
|
+
expect(result.current.canDrop('nonexistent', 'card')).toBe(false);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('unregisters drop zones', () => {
|
|
99
|
+
const { result } = renderHook(() => useDnd(), { wrapper });
|
|
100
|
+
|
|
101
|
+
act(() => {
|
|
102
|
+
result.current.registerDropZone({
|
|
103
|
+
id: 'zone-1',
|
|
104
|
+
acceptTypes: ['card'],
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
act(() => {
|
|
109
|
+
result.current.unregisterDropZone('zone-1');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
expect(result.current.canDrop('zone-1', 'card')).toBe(false);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('respects disabled config', () => {
|
|
116
|
+
const onDragStart = vi.fn();
|
|
117
|
+
const customWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
118
|
+
<DndProvider config={{ enabled: false }} onDragStart={onDragStart}>{children}</DndProvider>
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const { result } = renderHook(() => useDnd(), { wrapper: customWrapper });
|
|
122
|
+
|
|
123
|
+
act(() => {
|
|
124
|
+
result.current.startDrag({ id: 'item-1', type: 'card' });
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
expect(result.current.activeItem).toBeNull();
|
|
128
|
+
expect(onDragStart).not.toHaveBeenCalled();
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe('useHasDndProvider', () => {
|
|
133
|
+
it('returns false outside provider', () => {
|
|
134
|
+
const { result } = renderHook(() => useHasDndProvider());
|
|
135
|
+
expect(result.current).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('returns true inside provider', () => {
|
|
139
|
+
const { result } = renderHook(() => useHasDndProvider(), { wrapper });
|
|
140
|
+
expect(result.current).toBe(true);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe('useDnd without provider', () => {
|
|
145
|
+
it('throws error', () => {
|
|
146
|
+
expect(() => {
|
|
147
|
+
renderHook(() => useDnd());
|
|
148
|
+
}).toThrow('useDnd must be used within a <DndProvider>');
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for NotificationProvider and useNotifications
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
5
|
+
import { renderHook, act } from '@testing-library/react';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
import {
|
|
8
|
+
NotificationProvider,
|
|
9
|
+
useNotifications,
|
|
10
|
+
useHasNotificationProvider,
|
|
11
|
+
} from '../NotificationContext';
|
|
12
|
+
|
|
13
|
+
const wrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
14
|
+
<NotificationProvider>{children}</NotificationProvider>
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
describe('NotificationProvider', () => {
|
|
18
|
+
it('provides notifications context', () => {
|
|
19
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
20
|
+
expect(result.current.notifications).toEqual([]);
|
|
21
|
+
expect(result.current.unreadCount).toBe(0);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('adds a notification via notify', () => {
|
|
25
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
26
|
+
|
|
27
|
+
act(() => {
|
|
28
|
+
result.current.notify({ title: 'Hello', severity: 'info' });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
expect(result.current.notifications).toHaveLength(1);
|
|
32
|
+
expect(result.current.notifications[0].title).toBe('Hello');
|
|
33
|
+
expect(result.current.unreadCount).toBe(1);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('adds info notification', () => {
|
|
37
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
38
|
+
|
|
39
|
+
act(() => {
|
|
40
|
+
result.current.info('Info title', 'Info message');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
expect(result.current.notifications[0].severity).toBe('info');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('adds success notification', () => {
|
|
47
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
48
|
+
|
|
49
|
+
act(() => {
|
|
50
|
+
result.current.success('Success!');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
expect(result.current.notifications[0].severity).toBe('success');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('adds warning notification', () => {
|
|
57
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
58
|
+
|
|
59
|
+
act(() => {
|
|
60
|
+
result.current.warning('Warning!');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
expect(result.current.notifications[0].severity).toBe('warning');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('adds error notification', () => {
|
|
67
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
68
|
+
|
|
69
|
+
act(() => {
|
|
70
|
+
result.current.error('Error!');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
expect(result.current.notifications[0].severity).toBe('error');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('marks notification as read', () => {
|
|
77
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
78
|
+
|
|
79
|
+
let id: string = '';
|
|
80
|
+
act(() => {
|
|
81
|
+
id = result.current.info('Test');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
act(() => {
|
|
85
|
+
result.current.markAsRead(id);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(result.current.notifications[0].read).toBe(true);
|
|
89
|
+
expect(result.current.unreadCount).toBe(0);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('marks all as read', () => {
|
|
93
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
94
|
+
|
|
95
|
+
act(() => {
|
|
96
|
+
result.current.info('Test 1');
|
|
97
|
+
result.current.info('Test 2');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
act(() => {
|
|
101
|
+
result.current.markAllAsRead();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
expect(result.current.unreadCount).toBe(0);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('dismisses a notification', () => {
|
|
108
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
109
|
+
|
|
110
|
+
let id: string = '';
|
|
111
|
+
act(() => {
|
|
112
|
+
id = result.current.info('Test');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
act(() => {
|
|
116
|
+
result.current.dismiss(id);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(result.current.notifications).toHaveLength(0);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('clears all notifications', () => {
|
|
123
|
+
const { result } = renderHook(() => useNotifications(), { wrapper });
|
|
124
|
+
|
|
125
|
+
act(() => {
|
|
126
|
+
result.current.info('Test 1');
|
|
127
|
+
result.current.info('Test 2');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
act(() => {
|
|
131
|
+
result.current.clearAll();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
expect(result.current.notifications).toHaveLength(0);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('calls onToast handler', () => {
|
|
138
|
+
const onToast = vi.fn();
|
|
139
|
+
const customWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
140
|
+
<NotificationProvider onToast={onToast}>{children}</NotificationProvider>
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const { result } = renderHook(() => useNotifications(), {
|
|
144
|
+
wrapper: customWrapper,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
act(() => {
|
|
148
|
+
result.current.info('Toast test');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect(onToast).toHaveBeenCalledTimes(1);
|
|
152
|
+
expect(onToast).toHaveBeenCalledWith(
|
|
153
|
+
expect.objectContaining({ title: 'Toast test', severity: 'info' })
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe('useHasNotificationProvider', () => {
|
|
159
|
+
it('returns false outside provider', () => {
|
|
160
|
+
const { result } = renderHook(() => useHasNotificationProvider());
|
|
161
|
+
expect(result.current).toBe(false);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('returns true inside provider', () => {
|
|
165
|
+
const { result } = renderHook(() => useHasNotificationProvider(), { wrapper });
|
|
166
|
+
expect(result.current).toBe(true);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('useNotifications without provider', () => {
|
|
171
|
+
it('throws error', () => {
|
|
172
|
+
expect(() => {
|
|
173
|
+
renderHook(() => useNotifications());
|
|
174
|
+
}).toThrow('useNotifications must be used within a <NotificationProvider>');
|
|
175
|
+
});
|
|
176
|
+
});
|
package/src/context/index.ts
CHANGED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useActionRunner hook
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
6
|
+
import { renderHook, waitFor, act } from '@testing-library/react';
|
|
7
|
+
import { useActionRunner } from '../useActionRunner';
|
|
8
|
+
|
|
9
|
+
describe('useActionRunner', () => {
|
|
10
|
+
it('returns initial state with loading=false, error=null, result=null', () => {
|
|
11
|
+
const { result } = renderHook(() => useActionRunner());
|
|
12
|
+
|
|
13
|
+
expect(result.current.loading).toBe(false);
|
|
14
|
+
expect(result.current.error).toBeNull();
|
|
15
|
+
expect(result.current.result).toBeNull();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('exposes runner, execute, and updateContext', () => {
|
|
19
|
+
const { result } = renderHook(() => useActionRunner());
|
|
20
|
+
|
|
21
|
+
expect(result.current.runner).toBeDefined();
|
|
22
|
+
expect(typeof result.current.execute).toBe('function');
|
|
23
|
+
expect(typeof result.current.updateContext).toBe('function');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('handles options with context key format', () => {
|
|
27
|
+
const { result } = renderHook(() =>
|
|
28
|
+
useActionRunner({ context: { data: { name: 'test' } } }),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
expect(result.current.runner).toBeDefined();
|
|
32
|
+
expect(result.current.loading).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('handles plain context object format (backwards compat)', () => {
|
|
36
|
+
const { result } = renderHook(() =>
|
|
37
|
+
useActionRunner({ data: { name: 'test' } }),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(result.current.runner).toBeDefined();
|
|
41
|
+
expect(result.current.loading).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('sets loading=true during execution and loading=false after', async () => {
|
|
45
|
+
let resolveAction: () => void;
|
|
46
|
+
const pendingAction = new Promise<void>((resolve) => {
|
|
47
|
+
resolveAction = resolve;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const { result } = renderHook(() => useActionRunner());
|
|
51
|
+
|
|
52
|
+
let executePromise: Promise<unknown>;
|
|
53
|
+
act(() => {
|
|
54
|
+
executePromise = result.current.execute({
|
|
55
|
+
onClick: () => pendingAction,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
await waitFor(() => {
|
|
60
|
+
expect(result.current.loading).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await act(async () => {
|
|
64
|
+
resolveAction!();
|
|
65
|
+
await executePromise;
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
expect(result.current.loading).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('sets result on successful action execution', async () => {
|
|
72
|
+
const onNavigate = vi.fn();
|
|
73
|
+
const { result } = renderHook(() =>
|
|
74
|
+
useActionRunner({ context: {}, onNavigate }),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
await act(async () => {
|
|
78
|
+
await result.current.execute({
|
|
79
|
+
type: 'navigation',
|
|
80
|
+
navigate: { to: '/dashboard' },
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
expect(result.current.result).toEqual({ success: true });
|
|
85
|
+
expect(result.current.error).toBeNull();
|
|
86
|
+
expect(result.current.loading).toBe(false);
|
|
87
|
+
expect(onNavigate).toHaveBeenCalledWith(
|
|
88
|
+
'/dashboard',
|
|
89
|
+
expect.objectContaining({ external: false }),
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('sets error on failed action execution (catch path)', async () => {
|
|
94
|
+
const { result } = renderHook(() => useActionRunner());
|
|
95
|
+
|
|
96
|
+
await act(async () => {
|
|
97
|
+
await result.current.execute({
|
|
98
|
+
onClick: () => {
|
|
99
|
+
throw new Error('Something went wrong');
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
expect(result.current.error).toBe('Something went wrong');
|
|
105
|
+
expect(result.current.result).toEqual({
|
|
106
|
+
success: false,
|
|
107
|
+
error: 'Something went wrong',
|
|
108
|
+
});
|
|
109
|
+
expect(result.current.loading).toBe(false);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('updateContext calls runner.updateContext', () => {
|
|
113
|
+
const { result } = renderHook(() => useActionRunner());
|
|
114
|
+
|
|
115
|
+
const spy = vi.spyOn(result.current.runner, 'updateContext');
|
|
116
|
+
|
|
117
|
+
act(() => {
|
|
118
|
+
result.current.updateContext({ data: { id: 42 } });
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
expect(spy).toHaveBeenCalledWith({ data: { id: 42 } });
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('accepts handler options (onToast, onNavigate, etc.)', async () => {
|
|
125
|
+
const onToast = vi.fn();
|
|
126
|
+
const onNavigate = vi.fn();
|
|
127
|
+
|
|
128
|
+
const { result } = renderHook(() =>
|
|
129
|
+
useActionRunner({
|
|
130
|
+
context: {},
|
|
131
|
+
onToast,
|
|
132
|
+
onNavigate,
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
await act(async () => {
|
|
137
|
+
await result.current.execute({
|
|
138
|
+
type: 'navigation',
|
|
139
|
+
navigate: { to: '/settings' },
|
|
140
|
+
successMessage: 'Navigated!',
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
expect(onNavigate).toHaveBeenCalledWith(
|
|
145
|
+
'/settings',
|
|
146
|
+
expect.objectContaining({ external: false }),
|
|
147
|
+
);
|
|
148
|
+
expect(onToast).toHaveBeenCalledWith(
|
|
149
|
+
'Navigated!',
|
|
150
|
+
expect.objectContaining({ type: 'success' }),
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
});
|