@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,54 @@
|
|
|
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
|
+
export interface ViewTransitionConfig {
|
|
9
|
+
/** Enable view transitions (default: true) */
|
|
10
|
+
enabled?: boolean;
|
|
11
|
+
/** Fallback animation class when View Transitions API is unsupported */
|
|
12
|
+
fallbackClass?: string;
|
|
13
|
+
/** Transition duration in ms (default: 300) */
|
|
14
|
+
duration?: number;
|
|
15
|
+
/** Called before transition starts */
|
|
16
|
+
onTransitionStart?: () => void;
|
|
17
|
+
/** Called after transition completes */
|
|
18
|
+
onTransitionEnd?: () => void;
|
|
19
|
+
}
|
|
20
|
+
export interface ViewTransitionResult {
|
|
21
|
+
/** Whether the View Transitions API is supported */
|
|
22
|
+
isSupported: boolean;
|
|
23
|
+
/** Whether a transition is currently in progress */
|
|
24
|
+
isTransitioning: boolean;
|
|
25
|
+
/** Execute a callback with a view transition */
|
|
26
|
+
startTransition: (callback: () => void | Promise<void>) => Promise<void>;
|
|
27
|
+
/** CSS class to apply during transitions */
|
|
28
|
+
transitionClass: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Hook for integrating with the browser View Transitions API.
|
|
32
|
+
*
|
|
33
|
+
* Uses the native `document.startViewTransition()` when available and
|
|
34
|
+
* falls back to a temporary CSS class for animation when unsupported.
|
|
35
|
+
* Respects `prefers-reduced-motion`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* function ViewSwitcher({ onViewChange }: { onViewChange: (v: string) => void }) {
|
|
40
|
+
* const { startTransition, transitionClass, isTransitioning } = useViewTransition();
|
|
41
|
+
*
|
|
42
|
+
* const handleChange = (view: string) => {
|
|
43
|
+
* startTransition(() => onViewChange(view));
|
|
44
|
+
* };
|
|
45
|
+
*
|
|
46
|
+
* return (
|
|
47
|
+
* <div className={transitionClass}>
|
|
48
|
+
* ...
|
|
49
|
+
* </div>
|
|
50
|
+
* );
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function useViewTransition(config?: ViewTransitionConfig): ViewTransitionResult;
|
|
@@ -0,0 +1,83 @@
|
|
|
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
|
+
import { useState, useCallback, useMemo } from 'react';
|
|
9
|
+
import { useReducedMotion } from './useReducedMotion';
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// Helpers
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
const DEFAULT_DURATION = 300;
|
|
14
|
+
const DEFAULT_FALLBACK_CLASS = 'oui-view-transition-fallback';
|
|
15
|
+
function supportsViewTransitions() {
|
|
16
|
+
return (typeof document !== 'undefined' &&
|
|
17
|
+
'startViewTransition' in document);
|
|
18
|
+
}
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// useViewTransition hook
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
/**
|
|
23
|
+
* Hook for integrating with the browser View Transitions API.
|
|
24
|
+
*
|
|
25
|
+
* Uses the native `document.startViewTransition()` when available and
|
|
26
|
+
* falls back to a temporary CSS class for animation when unsupported.
|
|
27
|
+
* Respects `prefers-reduced-motion`.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```tsx
|
|
31
|
+
* function ViewSwitcher({ onViewChange }: { onViewChange: (v: string) => void }) {
|
|
32
|
+
* const { startTransition, transitionClass, isTransitioning } = useViewTransition();
|
|
33
|
+
*
|
|
34
|
+
* const handleChange = (view: string) => {
|
|
35
|
+
* startTransition(() => onViewChange(view));
|
|
36
|
+
* };
|
|
37
|
+
*
|
|
38
|
+
* return (
|
|
39
|
+
* <div className={transitionClass}>
|
|
40
|
+
* ...
|
|
41
|
+
* </div>
|
|
42
|
+
* );
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function useViewTransition(config = {}) {
|
|
47
|
+
const { enabled = true, fallbackClass = DEFAULT_FALLBACK_CLASS, duration = DEFAULT_DURATION, onTransitionStart, onTransitionEnd, } = config;
|
|
48
|
+
const reducedMotion = useReducedMotion();
|
|
49
|
+
const [isTransitioning, setIsTransitioning] = useState(false);
|
|
50
|
+
const isSupported = useMemo(() => supportsViewTransitions(), []);
|
|
51
|
+
const startTransition = useCallback(async (callback) => {
|
|
52
|
+
// Skip transition when disabled or user prefers reduced motion
|
|
53
|
+
if (!enabled || reducedMotion) {
|
|
54
|
+
await callback();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
onTransitionStart?.();
|
|
58
|
+
setIsTransitioning(true);
|
|
59
|
+
try {
|
|
60
|
+
if (isSupported) {
|
|
61
|
+
// Use native View Transitions API
|
|
62
|
+
const transition = document.startViewTransition(callback);
|
|
63
|
+
await transition.finished;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
// Fallback: apply class, run callback, remove class after duration
|
|
67
|
+
await callback();
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, duration));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
setIsTransitioning(false);
|
|
73
|
+
onTransitionEnd?.();
|
|
74
|
+
}
|
|
75
|
+
}, [enabled, reducedMotion, isSupported, duration, onTransitionStart, onTransitionEnd]);
|
|
76
|
+
const transitionClass = isTransitioning && !isSupported ? fallbackClass : '';
|
|
77
|
+
return {
|
|
78
|
+
isSupported,
|
|
79
|
+
isTransitioning,
|
|
80
|
+
startTransition,
|
|
81
|
+
transitionClass,
|
|
82
|
+
};
|
|
83
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@object-ui/react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "React bindings and SchemaRenderer component for Object UI",
|
|
@@ -23,19 +23,19 @@
|
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@objectstack/spec": "^
|
|
26
|
+
"@objectstack/spec": "^3.0.2",
|
|
27
27
|
"react-hook-form": "^7.71.1",
|
|
28
|
-
"@object-ui/core": "
|
|
29
|
-
"@object-ui/
|
|
30
|
-
"@object-ui/
|
|
28
|
+
"@object-ui/core": "3.0.0",
|
|
29
|
+
"@object-ui/types": "3.0.0",
|
|
30
|
+
"@object-ui/i18n": "3.0.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"react": "^18.0.0 || ^19.0.0",
|
|
34
34
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@types/react": "
|
|
38
|
-
"@types/react-dom": "
|
|
37
|
+
"@types/react": "19.2.13",
|
|
38
|
+
"@types/react-dom": "19.2.3",
|
|
39
39
|
"typescript": "^5.9.3"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
package/src/SchemaRenderer.tsx
CHANGED
|
@@ -6,9 +6,100 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import React, { forwardRef, useContext, useMemo } from 'react';
|
|
10
|
-
import {
|
|
9
|
+
import React, { forwardRef, useContext, useMemo, Component } from 'react';
|
|
10
|
+
import {
|
|
11
|
+
SchemaNode,
|
|
12
|
+
ComponentRegistry,
|
|
13
|
+
ExpressionEvaluator,
|
|
14
|
+
isObjectUIError,
|
|
15
|
+
type ObjectUIError,
|
|
16
|
+
ERROR_CODES,
|
|
17
|
+
debugLog,
|
|
18
|
+
debugTime,
|
|
19
|
+
debugTimeEnd,
|
|
20
|
+
} from '@object-ui/core';
|
|
11
21
|
import { SchemaRendererContext } from './context/SchemaRendererContext';
|
|
22
|
+
import { resolveI18nLabel } from './utils/i18n';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Extract AriaPropsSchema properties from a schema node and convert
|
|
26
|
+
* them to standard HTML ARIA attributes.
|
|
27
|
+
*
|
|
28
|
+
* @objectstack/spec AriaPropsSchema defines:
|
|
29
|
+
* ariaLabel: string | I18nLabel (→ aria-label)
|
|
30
|
+
* ariaDescribedBy: string (→ aria-describedby)
|
|
31
|
+
* role: string (→ role)
|
|
32
|
+
*/
|
|
33
|
+
function resolveAriaProps(schema: Record<string, any>): Record<string, string | undefined> {
|
|
34
|
+
const aria: Record<string, string | undefined> = {};
|
|
35
|
+
if (schema.ariaLabel) {
|
|
36
|
+
aria['aria-label'] = resolveI18nLabel(schema.ariaLabel);
|
|
37
|
+
}
|
|
38
|
+
if (schema.ariaDescribedBy) {
|
|
39
|
+
aria['aria-describedby'] = schema.ariaDescribedBy;
|
|
40
|
+
}
|
|
41
|
+
if (schema.role) {
|
|
42
|
+
aria['role'] = schema.role;
|
|
43
|
+
}
|
|
44
|
+
return aria;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Per-component Error Boundary for SchemaRenderer.
|
|
49
|
+
* Catches render errors in individual components, preventing one broken
|
|
50
|
+
* component from crashing the entire page.
|
|
51
|
+
*/
|
|
52
|
+
interface SchemaErrorBoundaryState {
|
|
53
|
+
hasError: boolean;
|
|
54
|
+
error: Error | null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class SchemaErrorBoundary extends Component<
|
|
58
|
+
{ componentType?: string; children: React.ReactNode },
|
|
59
|
+
SchemaErrorBoundaryState
|
|
60
|
+
> {
|
|
61
|
+
state: SchemaErrorBoundaryState = { hasError: false, error: null };
|
|
62
|
+
|
|
63
|
+
static getDerivedStateFromError(error: Error): SchemaErrorBoundaryState {
|
|
64
|
+
return { hasError: true, error };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
handleRetry = () => {
|
|
68
|
+
this.setState({ hasError: false, error: null });
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
render() {
|
|
72
|
+
if (this.state.hasError && this.state.error) {
|
|
73
|
+
const error = this.state.error;
|
|
74
|
+
const isDev = process.env.NODE_ENV !== 'production';
|
|
75
|
+
const objuiError = isObjectUIError(error) ? error as ObjectUIError : null;
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<div className="p-4 border border-orange-400 rounded bg-orange-50 text-orange-700 my-2" role="alert">
|
|
79
|
+
<p className="font-medium">
|
|
80
|
+
Component{this.props.componentType ? ` "${this.props.componentType}"` : ''} failed to render
|
|
81
|
+
</p>
|
|
82
|
+
<p className="text-sm mt-1">{error.message}</p>
|
|
83
|
+
{isDev && objuiError?.code && (
|
|
84
|
+
<p className="text-xs mt-1 text-orange-500">
|
|
85
|
+
Error code: {objuiError.code}
|
|
86
|
+
{objuiError.details?.suggestion ? (
|
|
87
|
+
<span className="block mt-0.5">💡 {String(objuiError.details.suggestion)}</span>
|
|
88
|
+
) : null}
|
|
89
|
+
</p>
|
|
90
|
+
)}
|
|
91
|
+
<button
|
|
92
|
+
onClick={this.handleRetry}
|
|
93
|
+
className="mt-2 text-sm underline hover:no-underline"
|
|
94
|
+
>
|
|
95
|
+
Retry
|
|
96
|
+
</button>
|
|
97
|
+
</div>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return this.props.children;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
12
103
|
|
|
13
104
|
export const SchemaRenderer = forwardRef<any, { schema: SchemaNode } & Record<string, any>>(({ schema, ...props }, _ref) => {
|
|
14
105
|
const context = useContext(SchemaRendererContext);
|
|
@@ -48,13 +139,20 @@ export const SchemaRenderer = forwardRef<any, { schema: SchemaNode } & Record<st
|
|
|
48
139
|
if (!evaluatedSchema) return null;
|
|
49
140
|
// If schema is just a string, render it as text
|
|
50
141
|
if (typeof evaluatedSchema === 'string') return <>{evaluatedSchema}</>;
|
|
142
|
+
|
|
143
|
+
debugLog('schema', 'Rendering schema node', { type: evaluatedSchema.type, id: evaluatedSchema.id });
|
|
51
144
|
|
|
52
145
|
const Component = ComponentRegistry.get(evaluatedSchema.type);
|
|
53
146
|
|
|
54
147
|
if (!Component) {
|
|
148
|
+
debugLog('schema', 'Component not found in registry', { type: evaluatedSchema.type });
|
|
149
|
+
const errorInfo = ERROR_CODES['OBJUI-001'];
|
|
55
150
|
return (
|
|
56
|
-
<div className="p-4 border border-red-500 rounded text-red-500 bg-red-50 my-2">
|
|
57
|
-
Unknown component type: <strong>{evaluatedSchema.type}</strong>
|
|
151
|
+
<div className="p-4 border border-red-500 rounded text-red-500 bg-red-50 my-2" role="alert">
|
|
152
|
+
<p className="font-medium">Unknown component type: <strong>{evaluatedSchema.type}</strong></p>
|
|
153
|
+
{process.env.NODE_ENV !== 'production' && (
|
|
154
|
+
<p className="text-xs mt-1">💡 {errorInfo.suggestion} (OBJUI-001)</p>
|
|
155
|
+
)}
|
|
58
156
|
<pre className="text-xs mt-2 overflow-auto">{JSON.stringify(evaluatedSchema, null, 2)}</pre>
|
|
59
157
|
</div>
|
|
60
158
|
);
|
|
@@ -78,14 +176,25 @@ export const SchemaRenderer = forwardRef<any, { schema: SchemaNode } & Record<st
|
|
|
78
176
|
...componentProps
|
|
79
177
|
} = evaluatedSchema;
|
|
80
178
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
179
|
+
// Extract AriaPropsSchema properties for accessibility
|
|
180
|
+
const ariaProps = resolveAriaProps(evaluatedSchema);
|
|
181
|
+
|
|
182
|
+
debugTime(`render:${evaluatedSchema.type}:${evaluatedSchema.id ?? 'anon'}`);
|
|
183
|
+
const rendered = (
|
|
184
|
+
<SchemaErrorBoundary componentType={evaluatedSchema.type}>
|
|
185
|
+
{React.createElement(Component, {
|
|
186
|
+
schema: evaluatedSchema,
|
|
187
|
+
...componentProps, // Spread non-metadata schema properties as props
|
|
188
|
+
...(evaluatedSchema.props || {}), // Override with explicit props if provided
|
|
189
|
+
...ariaProps, // Inject ARIA attributes from AriaPropsSchema
|
|
190
|
+
className: evaluatedSchema.className,
|
|
191
|
+
'data-obj-id': evaluatedSchema.id,
|
|
192
|
+
'data-obj-type': evaluatedSchema.type,
|
|
193
|
+
...props
|
|
194
|
+
})}
|
|
195
|
+
</SchemaErrorBoundary>
|
|
196
|
+
);
|
|
197
|
+
debugTimeEnd(`render:${evaluatedSchema.type}:${evaluatedSchema.id ?? 'anon'}`);
|
|
198
|
+
return rendered;
|
|
90
199
|
});
|
|
91
200
|
SchemaRenderer.displayName = 'SchemaRenderer';
|
|
@@ -0,0 +1,118 @@
|
|
|
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, beforeEach, afterEach } from 'vitest';
|
|
10
|
+
import { render, screen } from '@testing-library/react';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { ComponentRegistry } from '@object-ui/core';
|
|
13
|
+
import { SchemaRenderer } from '../SchemaRenderer';
|
|
14
|
+
|
|
15
|
+
// A simple test component that forwards ARIA attributes
|
|
16
|
+
const TestWidget: React.FC<any> = (props) => (
|
|
17
|
+
<div
|
|
18
|
+
data-testid="test-widget"
|
|
19
|
+
aria-label={props['aria-label']}
|
|
20
|
+
aria-describedby={props['aria-describedby']}
|
|
21
|
+
role={props['role']}
|
|
22
|
+
>
|
|
23
|
+
{props.content || 'Test'}
|
|
24
|
+
</div>
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
describe('SchemaRenderer AriaProps injection', () => {
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
ComponentRegistry.register('test-widget', TestWidget);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
ComponentRegistry.unregister?.('test-widget');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should inject aria-label from ariaLabel string', () => {
|
|
37
|
+
render(
|
|
38
|
+
<SchemaRenderer
|
|
39
|
+
schema={{
|
|
40
|
+
type: 'test-widget',
|
|
41
|
+
ariaLabel: 'Close dialog',
|
|
42
|
+
}}
|
|
43
|
+
/>
|
|
44
|
+
);
|
|
45
|
+
const el = screen.getByTestId('test-widget');
|
|
46
|
+
expect(el).toHaveAttribute('aria-label', 'Close dialog');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should resolve ariaLabel from I18nLabel object', () => {
|
|
50
|
+
render(
|
|
51
|
+
<SchemaRenderer
|
|
52
|
+
schema={{
|
|
53
|
+
type: 'test-widget',
|
|
54
|
+
ariaLabel: { key: 'dialog.close', defaultValue: 'Close dialog' },
|
|
55
|
+
}}
|
|
56
|
+
/>
|
|
57
|
+
);
|
|
58
|
+
const el = screen.getByTestId('test-widget');
|
|
59
|
+
expect(el).toHaveAttribute('aria-label', 'Close dialog');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should inject aria-describedby from ariaDescribedBy', () => {
|
|
63
|
+
render(
|
|
64
|
+
<SchemaRenderer
|
|
65
|
+
schema={{
|
|
66
|
+
type: 'test-widget',
|
|
67
|
+
ariaDescribedBy: 'help-text-1',
|
|
68
|
+
}}
|
|
69
|
+
/>
|
|
70
|
+
);
|
|
71
|
+
const el = screen.getByTestId('test-widget');
|
|
72
|
+
expect(el).toHaveAttribute('aria-describedby', 'help-text-1');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should inject role from schema', () => {
|
|
76
|
+
render(
|
|
77
|
+
<SchemaRenderer
|
|
78
|
+
schema={{
|
|
79
|
+
type: 'test-widget',
|
|
80
|
+
role: 'navigation',
|
|
81
|
+
}}
|
|
82
|
+
/>
|
|
83
|
+
);
|
|
84
|
+
const el = screen.getByTestId('test-widget');
|
|
85
|
+
expect(el).toHaveAttribute('role', 'navigation');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should inject all ARIA props together', () => {
|
|
89
|
+
render(
|
|
90
|
+
<SchemaRenderer
|
|
91
|
+
schema={{
|
|
92
|
+
type: 'test-widget',
|
|
93
|
+
ariaLabel: 'Main nav',
|
|
94
|
+
ariaDescribedBy: 'nav-desc',
|
|
95
|
+
role: 'navigation',
|
|
96
|
+
}}
|
|
97
|
+
/>
|
|
98
|
+
);
|
|
99
|
+
const el = screen.getByTestId('test-widget');
|
|
100
|
+
expect(el).toHaveAttribute('aria-label', 'Main nav');
|
|
101
|
+
expect(el).toHaveAttribute('aria-describedby', 'nav-desc');
|
|
102
|
+
expect(el).toHaveAttribute('role', 'navigation');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should not inject ARIA attrs when not in schema', () => {
|
|
106
|
+
render(
|
|
107
|
+
<SchemaRenderer
|
|
108
|
+
schema={{
|
|
109
|
+
type: 'test-widget',
|
|
110
|
+
content: 'Hello',
|
|
111
|
+
}}
|
|
112
|
+
/>
|
|
113
|
+
);
|
|
114
|
+
const el = screen.getByTestId('test-widget');
|
|
115
|
+
expect(el).not.toHaveAttribute('aria-label');
|
|
116
|
+
expect(el).not.toHaveAttribute('aria-describedby');
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
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, beforeEach, afterEach, vi } from 'vitest';
|
|
10
|
+
import { render, screen, fireEvent } from '@testing-library/react';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { ComponentRegistry } from '@object-ui/core';
|
|
13
|
+
import { SchemaRenderer, SchemaErrorBoundary } from '../SchemaRenderer';
|
|
14
|
+
|
|
15
|
+
// Suppress console.error from React error boundary during tests
|
|
16
|
+
const originalConsoleError = console.error;
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
console.error = vi.fn();
|
|
19
|
+
});
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
console.error = originalConsoleError;
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const CrashingWidget: React.FC = () => {
|
|
25
|
+
throw new Error('Widget exploded!');
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const StableWidget: React.FC<any> = (props) => (
|
|
29
|
+
<div data-testid="stable-widget">{props.content || 'Stable'}</div>
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
describe('SchemaErrorBoundary', () => {
|
|
33
|
+
it('should render children normally when no error', () => {
|
|
34
|
+
render(
|
|
35
|
+
<SchemaErrorBoundary>
|
|
36
|
+
<div data-testid="child">OK</div>
|
|
37
|
+
</SchemaErrorBoundary>
|
|
38
|
+
);
|
|
39
|
+
expect(screen.getByTestId('child')).toBeInTheDocument();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should catch render errors and display fallback', () => {
|
|
43
|
+
render(
|
|
44
|
+
<SchemaErrorBoundary componentType="crashing-widget">
|
|
45
|
+
<CrashingWidget />
|
|
46
|
+
</SchemaErrorBoundary>
|
|
47
|
+
);
|
|
48
|
+
expect(screen.getByRole('alert')).toBeInTheDocument();
|
|
49
|
+
expect(screen.getByText(/failed to render/i)).toBeInTheDocument();
|
|
50
|
+
expect(screen.getByText(/"crashing-widget"/)).toBeInTheDocument();
|
|
51
|
+
expect(screen.getByText('Widget exploded!')).toBeInTheDocument();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should display retry button that resets the error', () => {
|
|
55
|
+
render(
|
|
56
|
+
<SchemaErrorBoundary>
|
|
57
|
+
<CrashingWidget />
|
|
58
|
+
</SchemaErrorBoundary>
|
|
59
|
+
);
|
|
60
|
+
expect(screen.getByRole('alert')).toBeInTheDocument();
|
|
61
|
+
const retryButton = screen.getByText('Retry');
|
|
62
|
+
expect(retryButton).toBeInTheDocument();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should work without componentType prop', () => {
|
|
66
|
+
render(
|
|
67
|
+
<SchemaErrorBoundary>
|
|
68
|
+
<CrashingWidget />
|
|
69
|
+
</SchemaErrorBoundary>
|
|
70
|
+
);
|
|
71
|
+
expect(screen.getByText(/failed to render/i)).toBeInTheDocument();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe('SchemaRenderer error boundary integration', () => {
|
|
76
|
+
beforeEach(() => {
|
|
77
|
+
ComponentRegistry.register('crashing-widget', CrashingWidget);
|
|
78
|
+
ComponentRegistry.register('stable-widget', StableWidget);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
afterEach(() => {
|
|
82
|
+
ComponentRegistry.unregister?.('crashing-widget');
|
|
83
|
+
ComponentRegistry.unregister?.('stable-widget');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should catch errors from a crashing component', () => {
|
|
87
|
+
render(
|
|
88
|
+
<SchemaRenderer schema={{ type: 'crashing-widget' }} />
|
|
89
|
+
);
|
|
90
|
+
expect(screen.getByRole('alert')).toBeInTheDocument();
|
|
91
|
+
expect(screen.getByText('Widget exploded!')).toBeInTheDocument();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should not affect stable components', () => {
|
|
95
|
+
render(
|
|
96
|
+
<SchemaRenderer schema={{ type: 'stable-widget', content: 'Hello' }} />
|
|
97
|
+
);
|
|
98
|
+
expect(screen.getByTestId('stable-widget')).toBeInTheDocument();
|
|
99
|
+
expect(screen.getByText('Hello')).toBeInTheDocument();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should isolate crashes between multiple renderers', () => {
|
|
103
|
+
render(
|
|
104
|
+
<div>
|
|
105
|
+
<SchemaRenderer schema={{ type: 'crashing-widget' }} />
|
|
106
|
+
<SchemaRenderer schema={{ type: 'stable-widget', content: 'Still works' }} />
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
// Crashing one should not affect the other
|
|
110
|
+
expect(screen.getByRole('alert')).toBeInTheDocument();
|
|
111
|
+
expect(screen.getByTestId('stable-widget')).toBeInTheDocument();
|
|
112
|
+
expect(screen.getByText('Still works')).toBeInTheDocument();
|
|
113
|
+
});
|
|
114
|
+
});
|