@object-ui/react 0.3.1 → 2.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 +4 -0
- package/CHANGELOG.md +13 -0
- package/dist/LazyPluginLoader.d.ts +82 -0
- package/dist/LazyPluginLoader.js +124 -0
- package/dist/SchemaRenderer.js +44 -12
- 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/SchemaRendererContext.d.ts +2 -1
- package/dist/context/SchemaRendererContext.js +4 -2
- package/dist/context/ThemeContext.d.ts +94 -0
- package/dist/context/ThemeContext.js +215 -0
- package/dist/context/index.d.ts +2 -0
- package/dist/context/index.js +2 -0
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/useActionRunner.d.ts +21 -7
- package/dist/hooks/useActionRunner.js +27 -7
- 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/useNavigationOverlay.d.ts +78 -0
- package/dist/hooks/useNavigationOverlay.js +112 -0
- package/dist/hooks/usePageVariables.d.ts +67 -0
- package/dist/hooks/usePageVariables.js +109 -0
- package/dist/hooks/useViewData.d.ts +73 -0
- package/dist/hooks/useViewData.js +135 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/utils/i18n.d.ts +10 -0
- package/dist/utils/i18n.js +12 -0
- package/package.json +16 -6
- package/src/LazyPluginLoader.tsx +174 -0
- package/src/SchemaRenderer.tsx +64 -12
- package/src/__tests__/LazyPluginLoader.test.tsx +176 -0
- package/src/__tests__/ThemeProvider.test.tsx +451 -0
- package/src/__tests__/useNavigationOverlay.test.ts +416 -0
- package/src/__tests__/usePageVariables.test.tsx +334 -0
- package/src/__tests__/useViewData.test.tsx +422 -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/SchemaRendererContext.tsx +5 -2
- package/src/context/ThemeContext.tsx +325 -0
- package/src/context/__tests__/ActionContext.test.tsx +174 -0
- package/src/context/index.ts +2 -0
- package/src/hooks/__tests__/useActionRunner.test.ts +153 -0
- package/src/hooks/__tests__/useDiscovery.test.tsx +149 -0
- package/src/hooks/__tests__/useDynamicApp.test.ts +161 -0
- package/src/hooks/__tests__/useExpression.test.ts +86 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/useActionRunner.ts +49 -11
- package/src/hooks/useDiscovery.ts +141 -0
- package/src/hooks/useDynamicApp.ts +142 -0
- package/src/hooks/useNavigationOverlay.ts +180 -0
- package/src/hooks/usePageVariables.tsx +157 -0
- package/src/hooks/useViewData.ts +201 -0
- package/src/index.ts +22 -0
- package/src/utils/i18n.ts +10 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +2 -0
- 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,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
10
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { createLazyPlugin, preloadPlugin } from '../LazyPluginLoader';
|
|
13
|
+
|
|
14
|
+
describe('createLazyPlugin', () => {
|
|
15
|
+
it('should create a lazy-loaded component', async () => {
|
|
16
|
+
const TestComponent = () => <div>Test Component</div>;
|
|
17
|
+
|
|
18
|
+
const LazyComponent = createLazyPlugin(
|
|
19
|
+
() => Promise.resolve({ default: TestComponent })
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
render(<LazyComponent />);
|
|
23
|
+
|
|
24
|
+
await waitFor(() => {
|
|
25
|
+
expect(screen.getByText('Test Component')).toBeInTheDocument();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should show custom fallback while loading', async () => {
|
|
30
|
+
const TestComponent = () => <div>Test Component</div>;
|
|
31
|
+
|
|
32
|
+
const LazyComponent = createLazyPlugin(
|
|
33
|
+
() => new Promise<{ default: React.ComponentType }>((resolve) => {
|
|
34
|
+
setTimeout(() => resolve({ default: TestComponent }), 100);
|
|
35
|
+
}),
|
|
36
|
+
{ fallback: <div>Loading...</div> }
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
render(<LazyComponent />);
|
|
40
|
+
|
|
41
|
+
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
|
42
|
+
|
|
43
|
+
await waitFor(() => {
|
|
44
|
+
expect(screen.getByText('Test Component')).toBeInTheDocument();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should pass props to the lazy component', async () => {
|
|
49
|
+
interface TestProps {
|
|
50
|
+
message: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const TestComponent = ({ message }: TestProps) => <div>{message}</div>;
|
|
54
|
+
|
|
55
|
+
const LazyComponent = createLazyPlugin<TestProps>(
|
|
56
|
+
() => Promise.resolve({ default: TestComponent })
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
render(<LazyComponent message="Hello World" />);
|
|
60
|
+
|
|
61
|
+
await waitFor(() => {
|
|
62
|
+
expect(screen.getByText('Hello World')).toBeInTheDocument();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should handle loading errors gracefully', async () => {
|
|
67
|
+
// Mock console.error to avoid noise in test output
|
|
68
|
+
const originalError = console.error;
|
|
69
|
+
console.error = vi.fn();
|
|
70
|
+
|
|
71
|
+
const LazyComponent = createLazyPlugin<any>(
|
|
72
|
+
() => Promise.reject(new Error('Load error'))
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
expect(() => render(<LazyComponent />)).not.toThrow();
|
|
76
|
+
|
|
77
|
+
// Restore console.error
|
|
78
|
+
console.error = originalError;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should preload a plugin without rendering', async () => {
|
|
82
|
+
const TestComponent = () => <div>Preloaded</div>;
|
|
83
|
+
const importFn = vi.fn(() => Promise.resolve({ default: TestComponent }));
|
|
84
|
+
|
|
85
|
+
const result = await preloadPlugin(importFn);
|
|
86
|
+
|
|
87
|
+
expect(importFn).toHaveBeenCalledTimes(1);
|
|
88
|
+
expect(result).toEqual({ default: TestComponent });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should render errorFallback when component fails to load', async () => {
|
|
92
|
+
const originalError = console.error;
|
|
93
|
+
console.error = vi.fn();
|
|
94
|
+
|
|
95
|
+
const ErrorFallback = ({ error, retry }: { error: Error; retry: () => void }) => (
|
|
96
|
+
<div>
|
|
97
|
+
<span>Error: {error.message}</span>
|
|
98
|
+
<button onClick={retry}>Retry</button>
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const LazyComponent = createLazyPlugin(
|
|
103
|
+
() => Promise.reject(new Error('chunk failed')),
|
|
104
|
+
{ retries: 0, errorFallback: ErrorFallback }
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
render(<LazyComponent />);
|
|
108
|
+
|
|
109
|
+
await waitFor(() => {
|
|
110
|
+
expect(screen.getByText('Error: chunk failed')).toBeInTheDocument();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
expect(screen.getByRole('button', { name: 'Retry' })).toBeInTheDocument();
|
|
114
|
+
|
|
115
|
+
console.error = originalError;
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should not retry when retries=0', async () => {
|
|
119
|
+
const originalError = console.error;
|
|
120
|
+
console.error = vi.fn();
|
|
121
|
+
|
|
122
|
+
const importFn = vi.fn(() => Promise.reject(new Error('fail')));
|
|
123
|
+
|
|
124
|
+
const ErrorFallback = ({ error }: { error: Error; retry: () => void }) => (
|
|
125
|
+
<div>Error: {error.message}</div>
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const LazyComponent = createLazyPlugin(importFn, {
|
|
129
|
+
retries: 0,
|
|
130
|
+
errorFallback: ErrorFallback,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
render(<LazyComponent />);
|
|
134
|
+
|
|
135
|
+
await waitFor(() => {
|
|
136
|
+
expect(screen.getByText('Error: fail')).toBeInTheDocument();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Called only once — no retries
|
|
140
|
+
expect(importFn).toHaveBeenCalledTimes(1);
|
|
141
|
+
|
|
142
|
+
console.error = originalError;
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('should retry on failure then succeed', async () => {
|
|
146
|
+
const originalError = console.error;
|
|
147
|
+
console.error = vi.fn();
|
|
148
|
+
|
|
149
|
+
const TestComponent = () => <div>Success after retry</div>;
|
|
150
|
+
|
|
151
|
+
let callCount = 0;
|
|
152
|
+
const importFn = vi.fn(() => {
|
|
153
|
+
callCount++;
|
|
154
|
+
if (callCount <= 2) {
|
|
155
|
+
return Promise.reject(new Error('transient error'));
|
|
156
|
+
}
|
|
157
|
+
return Promise.resolve({ default: TestComponent as React.ComponentType });
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const LazyComponent = createLazyPlugin(importFn, {
|
|
161
|
+
retries: 3,
|
|
162
|
+
retryDelay: 10,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
render(<LazyComponent />);
|
|
166
|
+
|
|
167
|
+
await waitFor(() => {
|
|
168
|
+
expect(screen.getByText('Success after retry')).toBeInTheDocument();
|
|
169
|
+
}, { timeout: 5000 });
|
|
170
|
+
|
|
171
|
+
// First call + 2 retries (fails) + 1 success = called 3 times
|
|
172
|
+
expect(importFn).toHaveBeenCalledTimes(3);
|
|
173
|
+
|
|
174
|
+
console.error = originalError;
|
|
175
|
+
});
|
|
176
|
+
});
|
|
@@ -0,0 +1,451 @@
|
|
|
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 React from 'react';
|
|
10
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
11
|
+
import { renderHook, act } from '@testing-library/react';
|
|
12
|
+
import type { Theme } from '@object-ui/types';
|
|
13
|
+
import {
|
|
14
|
+
ThemeProvider,
|
|
15
|
+
useTheme,
|
|
16
|
+
useOptionalTheme,
|
|
17
|
+
} from '../context/ThemeContext';
|
|
18
|
+
|
|
19
|
+
// ============================================================================
|
|
20
|
+
// Fixtures
|
|
21
|
+
// ============================================================================
|
|
22
|
+
|
|
23
|
+
const lightTheme: Theme = {
|
|
24
|
+
name: 'light',
|
|
25
|
+
label: 'Light',
|
|
26
|
+
mode: 'light',
|
|
27
|
+
colors: {
|
|
28
|
+
primary: '#3B82F6',
|
|
29
|
+
secondary: '#64748B',
|
|
30
|
+
background: '#FFFFFF',
|
|
31
|
+
text: '#0F172A',
|
|
32
|
+
error: '#EF4444',
|
|
33
|
+
},
|
|
34
|
+
typography: {
|
|
35
|
+
fontFamily: { base: 'Inter, sans-serif' },
|
|
36
|
+
fontSize: { base: '1rem' },
|
|
37
|
+
},
|
|
38
|
+
borderRadius: { base: '0.25rem', lg: '0.5rem' },
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const darkTheme: Theme = {
|
|
42
|
+
name: 'dark',
|
|
43
|
+
label: 'Dark',
|
|
44
|
+
mode: 'dark',
|
|
45
|
+
colors: {
|
|
46
|
+
primary: '#60A5FA',
|
|
47
|
+
secondary: '#94A3B8',
|
|
48
|
+
background: '#0F172A',
|
|
49
|
+
text: '#F1F5F9',
|
|
50
|
+
error: '#FCA5A5',
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const childTheme: Theme = {
|
|
55
|
+
name: 'corporate',
|
|
56
|
+
label: 'Corporate',
|
|
57
|
+
colors: { primary: '#1E40AF' },
|
|
58
|
+
extends: 'light',
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function createWrapper(props: Omit<React.ComponentProps<typeof ThemeProvider>, 'children'>) {
|
|
62
|
+
return function Wrapper({ children }: { children: React.ReactNode }) {
|
|
63
|
+
return <ThemeProvider {...props}>{children}</ThemeProvider>;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ============================================================================
|
|
68
|
+
// Setup
|
|
69
|
+
// ============================================================================
|
|
70
|
+
|
|
71
|
+
beforeEach(() => {
|
|
72
|
+
// Clean DOM state
|
|
73
|
+
document.documentElement.style.cssText = '';
|
|
74
|
+
document.documentElement.className = '';
|
|
75
|
+
localStorage.clear();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
afterEach(() => {
|
|
79
|
+
document.documentElement.style.cssText = '';
|
|
80
|
+
document.documentElement.className = '';
|
|
81
|
+
localStorage.clear();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// ============================================================================
|
|
85
|
+
// useTheme — basic context
|
|
86
|
+
// ============================================================================
|
|
87
|
+
|
|
88
|
+
describe('useTheme', () => {
|
|
89
|
+
it('should throw when used outside ThemeProvider', () => {
|
|
90
|
+
expect(() => {
|
|
91
|
+
renderHook(() => useTheme());
|
|
92
|
+
}).toThrow('useTheme must be used within a <ThemeProvider>');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should return theme context when inside ThemeProvider', () => {
|
|
96
|
+
const { result } = renderHook(() => useTheme(), {
|
|
97
|
+
wrapper: createWrapper({ themes: [lightTheme] }),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
expect(result.current.theme).toBeTruthy();
|
|
101
|
+
expect(result.current.theme?.name).toBe('light');
|
|
102
|
+
expect(result.current.activeTheme).toBe('light');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should default to first theme when no defaultTheme', () => {
|
|
106
|
+
const { result } = renderHook(() => useTheme(), {
|
|
107
|
+
wrapper: createWrapper({ themes: [darkTheme, lightTheme] }),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
expect(result.current.activeTheme).toBe('dark');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('should use defaultTheme when provided', () => {
|
|
114
|
+
const { result } = renderHook(() => useTheme(), {
|
|
115
|
+
wrapper: createWrapper({ themes: [lightTheme, darkTheme], defaultTheme: 'dark' }),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
expect(result.current.activeTheme).toBe('dark');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should return empty state when no themes', () => {
|
|
122
|
+
const { result } = renderHook(() => useTheme(), {
|
|
123
|
+
wrapper: createWrapper({ themes: [] }),
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
expect(result.current.theme).toBeNull();
|
|
127
|
+
expect(result.current.activeTheme).toBeNull();
|
|
128
|
+
expect(result.current.cssVars).toEqual({});
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ============================================================================
|
|
133
|
+
// useOptionalTheme
|
|
134
|
+
// ============================================================================
|
|
135
|
+
|
|
136
|
+
describe('useOptionalTheme', () => {
|
|
137
|
+
it('should return null outside ThemeProvider', () => {
|
|
138
|
+
const { result } = renderHook(() => useOptionalTheme());
|
|
139
|
+
expect(result.current).toBeNull();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should return context inside ThemeProvider', () => {
|
|
143
|
+
const { result } = renderHook(() => useOptionalTheme(), {
|
|
144
|
+
wrapper: createWrapper({ themes: [lightTheme] }),
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
expect(result.current).toBeTruthy();
|
|
148
|
+
expect(result.current?.theme?.name).toBe('light');
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// ============================================================================
|
|
153
|
+
// Theme switching
|
|
154
|
+
// ============================================================================
|
|
155
|
+
|
|
156
|
+
describe('Theme switching', () => {
|
|
157
|
+
it('should switch theme via setTheme', () => {
|
|
158
|
+
const { result } = renderHook(() => useTheme(), {
|
|
159
|
+
wrapper: createWrapper({ themes: [lightTheme, darkTheme], defaultTheme: 'light' }),
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
expect(result.current.activeTheme).toBe('light');
|
|
163
|
+
|
|
164
|
+
act(() => {
|
|
165
|
+
result.current.setTheme('dark');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
expect(result.current.activeTheme).toBe('dark');
|
|
169
|
+
expect(result.current.theme?.name).toBe('dark');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should not switch to nonexistent theme', () => {
|
|
173
|
+
const { result } = renderHook(() => useTheme(), {
|
|
174
|
+
wrapper: createWrapper({ themes: [lightTheme], defaultTheme: 'light' }),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
act(() => {
|
|
178
|
+
result.current.setTheme('nonexistent');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
expect(result.current.activeTheme).toBe('light');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should update CSS variables when theme changes', () => {
|
|
185
|
+
const { result } = renderHook(() => useTheme(), {
|
|
186
|
+
wrapper: createWrapper({ themes: [lightTheme, darkTheme], defaultTheme: 'light' }),
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const lightPrimary = result.current.cssVars['--primary'];
|
|
190
|
+
|
|
191
|
+
act(() => {
|
|
192
|
+
result.current.setTheme('dark');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const darkPrimary = result.current.cssVars['--primary'];
|
|
196
|
+
expect(darkPrimary).not.toBe(lightPrimary);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// ============================================================================
|
|
201
|
+
// Mode management
|
|
202
|
+
// ============================================================================
|
|
203
|
+
|
|
204
|
+
describe('Mode management', () => {
|
|
205
|
+
it('should default mode to auto', () => {
|
|
206
|
+
const { result } = renderHook(() => useTheme(), {
|
|
207
|
+
wrapper: createWrapper({ themes: [lightTheme] }),
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
expect(result.current.mode).toBe('auto');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('should use defaultMode when provided', () => {
|
|
214
|
+
const { result } = renderHook(() => useTheme(), {
|
|
215
|
+
wrapper: createWrapper({ themes: [lightTheme], defaultMode: 'dark' }),
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
expect(result.current.mode).toBe('dark');
|
|
219
|
+
expect(result.current.resolvedMode).toBe('dark');
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('should switch mode via setMode', () => {
|
|
223
|
+
const { result } = renderHook(() => useTheme(), {
|
|
224
|
+
wrapper: createWrapper({ themes: [lightTheme], defaultMode: 'light' }),
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
expect(result.current.resolvedMode).toBe('light');
|
|
228
|
+
|
|
229
|
+
act(() => {
|
|
230
|
+
result.current.setMode('dark');
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
expect(result.current.mode).toBe('dark');
|
|
234
|
+
expect(result.current.resolvedMode).toBe('dark');
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('should resolve auto mode based on system preference', () => {
|
|
238
|
+
// Mock system dark mode
|
|
239
|
+
const original = window.matchMedia;
|
|
240
|
+
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
|
|
241
|
+
matches: query === '(prefers-color-scheme: dark)',
|
|
242
|
+
media: query,
|
|
243
|
+
addEventListener: vi.fn(),
|
|
244
|
+
removeEventListener: vi.fn(),
|
|
245
|
+
}));
|
|
246
|
+
|
|
247
|
+
const { result } = renderHook(() => useTheme(), {
|
|
248
|
+
wrapper: createWrapper({ themes: [lightTheme], defaultMode: 'auto' }),
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
expect(result.current.resolvedMode).toBe('dark');
|
|
252
|
+
|
|
253
|
+
window.matchMedia = original;
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// ============================================================================
|
|
258
|
+
// CSS variable injection
|
|
259
|
+
// ============================================================================
|
|
260
|
+
|
|
261
|
+
describe('CSS variable injection', () => {
|
|
262
|
+
it('should inject CSS variables into documentElement', () => {
|
|
263
|
+
renderHook(() => useTheme(), {
|
|
264
|
+
wrapper: createWrapper({ themes: [lightTheme] }),
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const root = document.documentElement;
|
|
268
|
+
expect(root.style.getPropertyValue('--primary')).toBeTruthy();
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('should inject mode class on documentElement', () => {
|
|
272
|
+
renderHook(() => useTheme(), {
|
|
273
|
+
wrapper: createWrapper({ themes: [lightTheme], defaultMode: 'dark' }),
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
expect(document.documentElement.classList.contains('dark')).toBe(true);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('should inject into custom target element', () => {
|
|
280
|
+
const target = document.createElement('div');
|
|
281
|
+
document.body.appendChild(target);
|
|
282
|
+
|
|
283
|
+
renderHook(() => useTheme(), {
|
|
284
|
+
wrapper: createWrapper({ themes: [lightTheme], target }),
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
expect(target.style.getPropertyValue('--primary')).toBeTruthy();
|
|
288
|
+
|
|
289
|
+
document.body.removeChild(target);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('should clean up CSS variables on unmount', () => {
|
|
293
|
+
const { unmount } = renderHook(() => useTheme(), {
|
|
294
|
+
wrapper: createWrapper({ themes: [lightTheme] }),
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
const root = document.documentElement;
|
|
298
|
+
expect(root.style.getPropertyValue('--primary')).toBeTruthy();
|
|
299
|
+
|
|
300
|
+
unmount();
|
|
301
|
+
|
|
302
|
+
expect(root.style.getPropertyValue('--primary')).toBe('');
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('should include typography and radius variables', () => {
|
|
306
|
+
renderHook(() => useTheme(), {
|
|
307
|
+
wrapper: createWrapper({ themes: [lightTheme] }),
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
const root = document.documentElement;
|
|
311
|
+
expect(root.style.getPropertyValue('--font-sans')).toBe('Inter, sans-serif');
|
|
312
|
+
expect(root.style.getPropertyValue('--radius')).toBe('0.25rem');
|
|
313
|
+
expect(root.style.getPropertyValue('--radius-lg')).toBe('0.5rem');
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
// ============================================================================
|
|
318
|
+
// Theme inheritance
|
|
319
|
+
// ============================================================================
|
|
320
|
+
|
|
321
|
+
describe('Theme inheritance', () => {
|
|
322
|
+
it('should resolve theme.extends', () => {
|
|
323
|
+
const { result } = renderHook(() => useTheme(), {
|
|
324
|
+
wrapper: createWrapper({
|
|
325
|
+
themes: [lightTheme, childTheme],
|
|
326
|
+
defaultTheme: 'corporate',
|
|
327
|
+
}),
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// Child overrides primary
|
|
331
|
+
expect(result.current.theme?.colors.primary).toBe('#1E40AF');
|
|
332
|
+
// Inherits from parent
|
|
333
|
+
expect(result.current.theme?.colors.secondary).toBe('#64748B');
|
|
334
|
+
expect(result.current.theme?.colors.background).toBe('#FFFFFF');
|
|
335
|
+
expect(result.current.theme?.typography?.fontFamily?.base).toBe('Inter, sans-serif');
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
// ============================================================================
|
|
340
|
+
// Persistence
|
|
341
|
+
// ============================================================================
|
|
342
|
+
|
|
343
|
+
describe('Persistence', () => {
|
|
344
|
+
it('should persist theme name to localStorage', () => {
|
|
345
|
+
renderHook(() => useTheme(), {
|
|
346
|
+
wrapper: createWrapper({
|
|
347
|
+
themes: [lightTheme, darkTheme],
|
|
348
|
+
defaultTheme: 'light',
|
|
349
|
+
persist: true,
|
|
350
|
+
}),
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
expect(localStorage.getItem('objectui-theme-name')).toBe('light');
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('should persist mode to localStorage', () => {
|
|
357
|
+
renderHook(() => useTheme(), {
|
|
358
|
+
wrapper: createWrapper({
|
|
359
|
+
themes: [lightTheme],
|
|
360
|
+
defaultMode: 'dark',
|
|
361
|
+
persist: true,
|
|
362
|
+
}),
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
expect(localStorage.getItem('objectui-theme-mode')).toBe('dark');
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it('should restore theme from localStorage', () => {
|
|
369
|
+
localStorage.setItem('objectui-theme-name', 'dark');
|
|
370
|
+
localStorage.setItem('objectui-theme-mode', 'dark');
|
|
371
|
+
|
|
372
|
+
const { result } = renderHook(() => useTheme(), {
|
|
373
|
+
wrapper: createWrapper({
|
|
374
|
+
themes: [lightTheme, darkTheme],
|
|
375
|
+
defaultTheme: 'light',
|
|
376
|
+
defaultMode: 'light',
|
|
377
|
+
persist: true,
|
|
378
|
+
}),
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
expect(result.current.activeTheme).toBe('dark');
|
|
382
|
+
expect(result.current.mode).toBe('dark');
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it('should use custom storageKey', () => {
|
|
386
|
+
renderHook(() => useTheme(), {
|
|
387
|
+
wrapper: createWrapper({
|
|
388
|
+
themes: [lightTheme],
|
|
389
|
+
persist: true,
|
|
390
|
+
storageKey: 'myapp-theme',
|
|
391
|
+
}),
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
expect(localStorage.getItem('myapp-theme-name')).toBe('light');
|
|
395
|
+
expect(localStorage.getItem('myapp-theme-mode')).toBeTruthy();
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it('should not persist when persist=false', () => {
|
|
399
|
+
renderHook(() => useTheme(), {
|
|
400
|
+
wrapper: createWrapper({
|
|
401
|
+
themes: [lightTheme],
|
|
402
|
+
persist: false,
|
|
403
|
+
}),
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
expect(localStorage.getItem('objectui-theme-name')).toBeNull();
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it('should update persistence when theme changes', () => {
|
|
410
|
+
const { result } = renderHook(() => useTheme(), {
|
|
411
|
+
wrapper: createWrapper({
|
|
412
|
+
themes: [lightTheme, darkTheme],
|
|
413
|
+
defaultTheme: 'light',
|
|
414
|
+
persist: true,
|
|
415
|
+
}),
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
act(() => {
|
|
419
|
+
result.current.setTheme('dark');
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
expect(localStorage.getItem('objectui-theme-name')).toBe('dark');
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// ============================================================================
|
|
427
|
+
// Generated CSS vars object
|
|
428
|
+
// ============================================================================
|
|
429
|
+
|
|
430
|
+
describe('cssVars output', () => {
|
|
431
|
+
it('should expose cssVars in the context', () => {
|
|
432
|
+
const { result } = renderHook(() => useTheme(), {
|
|
433
|
+
wrapper: createWrapper({ themes: [lightTheme] }),
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
expect(result.current.cssVars).toBeDefined();
|
|
437
|
+
expect(result.current.cssVars['--primary']).toBeTruthy();
|
|
438
|
+
expect(result.current.cssVars['--foreground']).toBeTruthy();
|
|
439
|
+
expect(result.current.cssVars['--font-sans']).toBe('Inter, sans-serif');
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it('should provide themes list', () => {
|
|
443
|
+
const { result } = renderHook(() => useTheme(), {
|
|
444
|
+
wrapper: createWrapper({ themes: [lightTheme, darkTheme] }),
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
expect(result.current.themes).toHaveLength(2);
|
|
448
|
+
expect(result.current.themes[0].name).toBe('light');
|
|
449
|
+
expect(result.current.themes[1].name).toBe('dark');
|
|
450
|
+
});
|
|
451
|
+
});
|