@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,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useAnimation and useReducedMotion hooks
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect } from 'vitest';
|
|
5
|
+
import { renderHook } from '@testing-library/react';
|
|
6
|
+
import { useAnimation } from '../useAnimation';
|
|
7
|
+
import { useReducedMotion } from '../useReducedMotion';
|
|
8
|
+
|
|
9
|
+
describe('useAnimation', () => {
|
|
10
|
+
it('returns empty className and style for no config', () => {
|
|
11
|
+
const { result } = renderHook(() => useAnimation());
|
|
12
|
+
expect(result.current.className).toBe('');
|
|
13
|
+
expect(result.current.style).toEqual({});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('returns empty when preset is none', () => {
|
|
17
|
+
const { result } = renderHook(() => useAnimation({ preset: 'none' }));
|
|
18
|
+
expect(result.current.className).toBe('');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('returns empty when disabled', () => {
|
|
22
|
+
const { result } = renderHook(() =>
|
|
23
|
+
useAnimation({ preset: 'fade', enabled: false })
|
|
24
|
+
);
|
|
25
|
+
expect(result.current.className).toBe('');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('returns correct className for fade preset', () => {
|
|
29
|
+
const { result } = renderHook(() => useAnimation({ preset: 'fade' }));
|
|
30
|
+
expect(result.current.className).toContain('fade-in');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('returns correct className for slide-up preset', () => {
|
|
34
|
+
const { result } = renderHook(() => useAnimation({ preset: 'slide-up' }));
|
|
35
|
+
expect(result.current.className).toContain('slide-in-from-bottom');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('returns correct className for scale preset', () => {
|
|
39
|
+
const { result } = renderHook(() => useAnimation({ preset: 'scale' }));
|
|
40
|
+
expect(result.current.className).toContain('zoom-in');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('returns correct className for scale-fade preset', () => {
|
|
44
|
+
const { result } = renderHook(() => useAnimation({ preset: 'scale-fade' }));
|
|
45
|
+
expect(result.current.className).toContain('fade-in');
|
|
46
|
+
expect(result.current.className).toContain('zoom-in');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('applies custom duration', () => {
|
|
50
|
+
const { result } = renderHook(() =>
|
|
51
|
+
useAnimation({ preset: 'fade', duration: 300 })
|
|
52
|
+
);
|
|
53
|
+
expect(result.current.style.animationDuration).toBe('300ms');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('applies custom delay', () => {
|
|
57
|
+
const { result } = renderHook(() =>
|
|
58
|
+
useAnimation({ preset: 'fade', delay: 100 })
|
|
59
|
+
);
|
|
60
|
+
expect(result.current.style.animationDelay).toBe('100ms');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('applies easing function', () => {
|
|
64
|
+
const { result } = renderHook(() =>
|
|
65
|
+
useAnimation({ preset: 'fade', easing: 'spring' })
|
|
66
|
+
);
|
|
67
|
+
expect(result.current.style.animationTimingFunction).toContain('cubic-bezier');
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('useReducedMotion', () => {
|
|
72
|
+
it('returns a boolean value', () => {
|
|
73
|
+
const { result } = renderHook(() => useReducedMotion());
|
|
74
|
+
expect(typeof result.current).toBe('boolean');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('defaults to false when matchMedia returns false', () => {
|
|
78
|
+
const { result } = renderHook(() => useReducedMotion());
|
|
79
|
+
// jsdom matchMedia always returns false by default
|
|
80
|
+
expect(result.current).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -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,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useDiscovery hook
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
7
|
+
import { renderHook, waitFor } from '@testing-library/react';
|
|
8
|
+
import { useDiscovery } from '../useDiscovery';
|
|
9
|
+
import { SchemaRendererContext } from '../../context/SchemaRendererContext';
|
|
10
|
+
|
|
11
|
+
function createWrapper(dataSource: any) {
|
|
12
|
+
return ({ children }: { children: React.ReactNode }) =>
|
|
13
|
+
React.createElement(
|
|
14
|
+
SchemaRendererContext.Provider,
|
|
15
|
+
{ value: { dataSource } },
|
|
16
|
+
children,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('useDiscovery', () => {
|
|
21
|
+
it('returns isLoading false and null discovery when no context is provided', async () => {
|
|
22
|
+
const { result } = renderHook(() => useDiscovery());
|
|
23
|
+
|
|
24
|
+
await waitFor(() => {
|
|
25
|
+
expect(result.current.isLoading).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
expect(result.current.discovery).toBeNull();
|
|
29
|
+
expect(result.current.error).toBeNull();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('returns isLoading false and null discovery when dataSource has no getDiscovery method', async () => {
|
|
33
|
+
const dataSource = { someOtherMethod: vi.fn() };
|
|
34
|
+
|
|
35
|
+
const { result } = renderHook(() => useDiscovery(), {
|
|
36
|
+
wrapper: createWrapper(dataSource),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
await waitFor(() => {
|
|
40
|
+
expect(result.current.isLoading).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
expect(result.current.discovery).toBeNull();
|
|
44
|
+
expect(result.current.error).toBeNull();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('successfully fetches discovery data from dataSource.getDiscovery()', async () => {
|
|
48
|
+
const discoveryData = {
|
|
49
|
+
name: 'test-server',
|
|
50
|
+
version: '1.0.0',
|
|
51
|
+
services: {
|
|
52
|
+
auth: { enabled: true, status: 'available' as const },
|
|
53
|
+
data: { enabled: true, status: 'available' as const },
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const dataSource = {
|
|
58
|
+
getDiscovery: vi.fn().mockResolvedValue(discoveryData),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const { result } = renderHook(() => useDiscovery(), {
|
|
62
|
+
wrapper: createWrapper(dataSource),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
expect(result.current.isLoading).toBe(true);
|
|
66
|
+
|
|
67
|
+
await waitFor(() => {
|
|
68
|
+
expect(result.current.isLoading).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
expect(result.current.discovery).toEqual(discoveryData);
|
|
72
|
+
expect(result.current.error).toBeNull();
|
|
73
|
+
expect(dataSource.getDiscovery).toHaveBeenCalled();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('handles errors from getDiscovery gracefully', async () => {
|
|
77
|
+
const dataSource = {
|
|
78
|
+
getDiscovery: vi.fn().mockRejectedValue(new Error('Network error')),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const { result } = renderHook(() => useDiscovery(), {
|
|
82
|
+
wrapper: createWrapper(dataSource),
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
await waitFor(() => {
|
|
86
|
+
expect(result.current.isLoading).toBe(false);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
expect(result.current.discovery).toBeNull();
|
|
90
|
+
expect(result.current.error?.message).toBe('Network error');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('isAuthEnabled defaults to true when no discovery', async () => {
|
|
94
|
+
const { result } = renderHook(() => useDiscovery());
|
|
95
|
+
|
|
96
|
+
await waitFor(() => {
|
|
97
|
+
expect(result.current.isLoading).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
expect(result.current.isAuthEnabled).toBe(true);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('isAuthEnabled reflects the auth service enabled state', async () => {
|
|
104
|
+
const discoveryData = {
|
|
105
|
+
services: {
|
|
106
|
+
auth: { enabled: false },
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const dataSource = {
|
|
111
|
+
getDiscovery: vi.fn().mockResolvedValue(discoveryData),
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const { result } = renderHook(() => useDiscovery(), {
|
|
115
|
+
wrapper: createWrapper(dataSource),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
await waitFor(() => {
|
|
119
|
+
expect(result.current.isLoading).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
expect(result.current.isAuthEnabled).toBe(false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('cleans up on unmount (cancelled flag)', async () => {
|
|
126
|
+
let resolveDiscovery: (value: any) => void;
|
|
127
|
+
const discoveryPromise = new Promise((resolve) => {
|
|
128
|
+
resolveDiscovery = resolve;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const dataSource = {
|
|
132
|
+
getDiscovery: vi.fn().mockReturnValue(discoveryPromise),
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const { result, unmount } = renderHook(() => useDiscovery(), {
|
|
136
|
+
wrapper: createWrapper(dataSource),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(result.current.isLoading).toBe(true);
|
|
140
|
+
|
|
141
|
+
unmount();
|
|
142
|
+
|
|
143
|
+
// Resolve after unmount — state should not update
|
|
144
|
+
resolveDiscovery!({ name: 'late-response' });
|
|
145
|
+
|
|
146
|
+
// Discovery should remain null since the component was unmounted
|
|
147
|
+
expect(result.current.discovery).toBeNull();
|
|
148
|
+
});
|
|
149
|
+
});
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for useDynamicApp hook
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
6
|
+
import { renderHook, waitFor, act } from '@testing-library/react';
|
|
7
|
+
import { useDynamicApp } from '../useDynamicApp';
|
|
8
|
+
|
|
9
|
+
describe('useDynamicApp', () => {
|
|
10
|
+
const staticConfig = {
|
|
11
|
+
name: 'crm',
|
|
12
|
+
label: 'CRM App',
|
|
13
|
+
objects: ['contact', 'account'],
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
it('returns static config when no adapter is provided', () => {
|
|
17
|
+
const { result } = renderHook(() =>
|
|
18
|
+
useDynamicApp({ appId: 'crm', staticConfig }),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
expect(result.current.config).toEqual(staticConfig);
|
|
22
|
+
expect(result.current.isLoading).toBe(false);
|
|
23
|
+
expect(result.current.isServerConfig).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('returns null config when neither adapter nor static config provided', () => {
|
|
27
|
+
const { result } = renderHook(() =>
|
|
28
|
+
useDynamicApp({ appId: 'crm' }),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
expect(result.current.config).toBeNull();
|
|
32
|
+
expect(result.current.isLoading).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('loads config from server adapter', async () => {
|
|
36
|
+
const serverConfig = { name: 'crm', label: 'CRM (Server)', objects: ['contact'] };
|
|
37
|
+
const adapter = {
|
|
38
|
+
getApp: vi.fn().mockResolvedValue(serverConfig),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const { result } = renderHook(() =>
|
|
42
|
+
useDynamicApp({ appId: 'crm', staticConfig, adapter }),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// Initially loading
|
|
46
|
+
expect(result.current.isLoading).toBe(true);
|
|
47
|
+
|
|
48
|
+
await waitFor(() => {
|
|
49
|
+
expect(result.current.isLoading).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
expect(result.current.config).toEqual(serverConfig);
|
|
53
|
+
expect(result.current.isServerConfig).toBe(true);
|
|
54
|
+
expect(adapter.getApp).toHaveBeenCalledWith('crm');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('falls back to static config when server returns null', async () => {
|
|
58
|
+
const adapter = {
|
|
59
|
+
getApp: vi.fn().mockResolvedValue(null),
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const { result } = renderHook(() =>
|
|
63
|
+
useDynamicApp({ appId: 'crm', staticConfig, adapter }),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
await waitFor(() => {
|
|
67
|
+
expect(result.current.isLoading).toBe(false);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(result.current.config).toEqual(staticConfig);
|
|
71
|
+
expect(result.current.isServerConfig).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('falls back to static config on server error', async () => {
|
|
75
|
+
const adapter = {
|
|
76
|
+
getApp: vi.fn().mockRejectedValue(new Error('Network error')),
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const { result } = renderHook(() =>
|
|
80
|
+
useDynamicApp({ appId: 'crm', staticConfig, adapter }),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
await waitFor(() => {
|
|
84
|
+
expect(result.current.isLoading).toBe(false);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
expect(result.current.config).toEqual(staticConfig);
|
|
88
|
+
expect(result.current.isServerConfig).toBe(false);
|
|
89
|
+
expect(result.current.error?.message).toBe('Network error');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('skips loading when enabled is false', () => {
|
|
93
|
+
const adapter = {
|
|
94
|
+
getApp: vi.fn(),
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const { result } = renderHook(() =>
|
|
98
|
+
useDynamicApp({ appId: 'crm', staticConfig, adapter, enabled: false }),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
expect(result.current.config).toEqual(staticConfig);
|
|
102
|
+
expect(result.current.isLoading).toBe(false);
|
|
103
|
+
expect(adapter.getApp).not.toHaveBeenCalled();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('refresh invalidates cache and re-fetches', async () => {
|
|
107
|
+
let callCount = 0;
|
|
108
|
+
const adapter = {
|
|
109
|
+
getApp: vi.fn().mockImplementation(async () => {
|
|
110
|
+
callCount++;
|
|
111
|
+
return { name: 'crm', version: callCount };
|
|
112
|
+
}),
|
|
113
|
+
invalidateCache: vi.fn(),
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const { result } = renderHook(() =>
|
|
117
|
+
useDynamicApp({ appId: 'crm', adapter }),
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
await waitFor(() => {
|
|
121
|
+
expect(result.current.isLoading).toBe(false);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
expect((result.current.config as any).version).toBe(1);
|
|
125
|
+
|
|
126
|
+
await act(async () => {
|
|
127
|
+
await result.current.refresh();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
expect(adapter.invalidateCache).toHaveBeenCalledWith('app:crm');
|
|
131
|
+
expect((result.current.config as any).version).toBe(2);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('reloads when appId changes', async () => {
|
|
135
|
+
const adapter = {
|
|
136
|
+
getApp: vi.fn().mockImplementation(async (appId: string) => {
|
|
137
|
+
return { name: appId };
|
|
138
|
+
}),
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const { result, rerender } = renderHook(
|
|
142
|
+
({ appId }: { appId: string }) =>
|
|
143
|
+
useDynamicApp({ appId, adapter }),
|
|
144
|
+
{ initialProps: { appId: 'crm' } },
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
await waitFor(() => {
|
|
148
|
+
expect(result.current.isLoading).toBe(false);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
expect((result.current.config as any).name).toBe('crm');
|
|
152
|
+
|
|
153
|
+
rerender({ appId: 'erp' });
|
|
154
|
+
|
|
155
|
+
await waitFor(() => {
|
|
156
|
+
expect((result.current.config as any).name).toBe('erp');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
expect(adapter.getApp).toHaveBeenCalledWith('erp');
|
|
160
|
+
});
|
|
161
|
+
});
|