@moontra/moonui-pro 2.20.1 → 2.20.3

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.
Files changed (162) hide show
  1. package/dist/index.d.ts +691 -261
  2. package/dist/index.mjs +7418 -4934
  3. package/package.json +11 -5
  4. package/plugin/index.d.ts +86 -0
  5. package/plugin/index.js +308 -0
  6. package/scripts/postbuild.js +27 -0
  7. package/scripts/postinstall.js +176 -23
  8. package/src/__tests__/use-intersection-observer.test.tsx +0 -216
  9. package/src/__tests__/use-local-storage.test.tsx +0 -174
  10. package/src/__tests__/use-pro-access.test.tsx +0 -183
  11. package/src/components/advanced-chart/advanced-chart.test.tsx +0 -281
  12. package/src/components/advanced-chart/index.tsx +0 -1242
  13. package/src/components/advanced-forms/index.tsx +0 -426
  14. package/src/components/animated-button/index.tsx +0 -385
  15. package/src/components/calendar/event-dialog.tsx +0 -372
  16. package/src/components/calendar/index.tsx +0 -1073
  17. package/src/components/calendar-pro/index.tsx +0 -1697
  18. package/src/components/color-picker/index.tsx +0 -432
  19. package/src/components/credit-card-input/index.tsx +0 -406
  20. package/src/components/dashboard/dashboard-grid.tsx +0 -462
  21. package/src/components/dashboard/demo.tsx +0 -425
  22. package/src/components/dashboard/index.tsx +0 -1046
  23. package/src/components/dashboard/time-range-picker.tsx +0 -336
  24. package/src/components/dashboard/types.ts +0 -222
  25. package/src/components/dashboard/widgets/activity-feed.tsx +0 -344
  26. package/src/components/dashboard/widgets/chart-widget.tsx +0 -418
  27. package/src/components/dashboard/widgets/metric-card.tsx +0 -343
  28. package/src/components/data-table/data-table-bulk-actions.tsx +0 -204
  29. package/src/components/data-table/data-table-column-toggle.tsx +0 -169
  30. package/src/components/data-table/data-table-export.ts +0 -156
  31. package/src/components/data-table/data-table-filter-drawer.tsx +0 -448
  32. package/src/components/data-table/data-table.test.tsx +0 -187
  33. package/src/components/data-table/index.tsx +0 -845
  34. package/src/components/draggable-list/index.tsx +0 -100
  35. package/src/components/enhanced/badge.tsx +0 -191
  36. package/src/components/enhanced/button.tsx +0 -362
  37. package/src/components/enhanced/card.tsx +0 -266
  38. package/src/components/enhanced/dialog.tsx +0 -246
  39. package/src/components/enhanced/index.ts +0 -4
  40. package/src/components/error-boundary/index.tsx +0 -109
  41. package/src/components/file-upload/file-upload.test.tsx +0 -243
  42. package/src/components/file-upload/index.tsx +0 -1660
  43. package/src/components/floating-action-button/index.tsx +0 -206
  44. package/src/components/form-wizard/form-wizard-context.tsx +0 -307
  45. package/src/components/form-wizard/form-wizard-navigation.tsx +0 -118
  46. package/src/components/form-wizard/form-wizard-progress.tsx +0 -298
  47. package/src/components/form-wizard/form-wizard-step.tsx +0 -111
  48. package/src/components/form-wizard/index.tsx +0 -102
  49. package/src/components/form-wizard/types.ts +0 -76
  50. package/src/components/gesture-drawer/index.tsx +0 -551
  51. package/src/components/github-stars/github-api.ts +0 -426
  52. package/src/components/github-stars/hooks.ts +0 -516
  53. package/src/components/github-stars/index.tsx +0 -375
  54. package/src/components/github-stars/types.ts +0 -148
  55. package/src/components/github-stars/variants.tsx +0 -513
  56. package/src/components/health-check/index.tsx +0 -439
  57. package/src/components/hover-card-3d/index.tsx +0 -530
  58. package/src/components/index.ts +0 -128
  59. package/src/components/internal/index.ts +0 -78
  60. package/src/components/kanban/add-card-modal.tsx +0 -502
  61. package/src/components/kanban/card-detail-modal.tsx +0 -761
  62. package/src/components/kanban/index.ts +0 -13
  63. package/src/components/kanban/kanban.tsx +0 -1684
  64. package/src/components/kanban/types.ts +0 -168
  65. package/src/components/lazy-component/index.tsx +0 -823
  66. package/src/components/license-error/index.tsx +0 -29
  67. package/src/components/magnetic-button/index.tsx +0 -167
  68. package/src/components/memory-efficient-data/index.tsx +0 -1016
  69. package/src/components/moonui-quiz-form/index.tsx +0 -817
  70. package/src/components/optimized-image/index.tsx +0 -425
  71. package/src/components/performance-debugger/index.tsx +0 -589
  72. package/src/components/performance-monitor/index.tsx +0 -794
  73. package/src/components/phone-number-input/index.tsx +0 -338
  74. package/src/components/pinch-zoom/index.tsx +0 -566
  75. package/src/components/quiz-form/index.tsx +0 -479
  76. package/src/components/rich-text-editor/index-old-backup.tsx +0 -437
  77. package/src/components/rich-text-editor/index.tsx +0 -2324
  78. package/src/components/rich-text-editor/slash-commands-extension.ts +0 -220
  79. package/src/components/rich-text-editor/slash-commands.css +0 -35
  80. package/src/components/rich-text-editor/table-styles.css +0 -65
  81. package/src/components/sidebar/index.tsx +0 -865
  82. package/src/components/spotlight-card/index.tsx +0 -191
  83. package/src/components/swipeable-card/index.tsx +0 -100
  84. package/src/components/timeline/index.tsx +0 -1148
  85. package/src/components/ui/accordion.tsx +0 -73
  86. package/src/components/ui/alert-dialog.tsx +0 -141
  87. package/src/components/ui/alert.tsx +0 -141
  88. package/src/components/ui/aspect-ratio.tsx +0 -245
  89. package/src/components/ui/avatar.tsx +0 -153
  90. package/src/components/ui/badge.tsx +0 -228
  91. package/src/components/ui/breadcrumb.tsx +0 -214
  92. package/src/components/ui/button.tsx +0 -222
  93. package/src/components/ui/calendar.tsx +0 -387
  94. package/src/components/ui/card.tsx +0 -214
  95. package/src/components/ui/checkbox.tsx +0 -259
  96. package/src/components/ui/collapsible.tsx +0 -135
  97. package/src/components/ui/color-picker.tsx +0 -97
  98. package/src/components/ui/command.tsx +0 -225
  99. package/src/components/ui/dialog.tsx +0 -334
  100. package/src/components/ui/dropdown-menu.tsx +0 -218
  101. package/src/components/ui/gesture-drawer.tsx +0 -11
  102. package/src/components/ui/hover-card.tsx +0 -29
  103. package/src/components/ui/index.ts +0 -190
  104. package/src/components/ui/input.tsx +0 -222
  105. package/src/components/ui/label.tsx +0 -29
  106. package/src/components/ui/lightbox.tsx +0 -606
  107. package/src/components/ui/magnetic-button.tsx +0 -129
  108. package/src/components/ui/media-gallery.tsx +0 -612
  109. package/src/components/ui/pagination.tsx +0 -123
  110. package/src/components/ui/popover.tsx +0 -185
  111. package/src/components/ui/progress.tsx +0 -30
  112. package/src/components/ui/radio-group.tsx +0 -257
  113. package/src/components/ui/scroll-area.tsx +0 -47
  114. package/src/components/ui/select.tsx +0 -374
  115. package/src/components/ui/separator.tsx +0 -145
  116. package/src/components/ui/sheet.tsx +0 -139
  117. package/src/components/ui/skeleton.tsx +0 -20
  118. package/src/components/ui/slider.tsx +0 -354
  119. package/src/components/ui/spotlight-card.tsx +0 -119
  120. package/src/components/ui/switch.tsx +0 -86
  121. package/src/components/ui/table.tsx +0 -329
  122. package/src/components/ui/tabs.tsx +0 -198
  123. package/src/components/ui/textarea.tsx +0 -28
  124. package/src/components/ui/toast.tsx +0 -317
  125. package/src/components/ui/toggle.tsx +0 -119
  126. package/src/components/ui/tooltip.tsx +0 -151
  127. package/src/components/virtual-list/index.tsx +0 -668
  128. package/src/hooks/use-chart.ts +0 -205
  129. package/src/hooks/use-data-table.ts +0 -182
  130. package/src/hooks/use-docs-pro-access.ts +0 -13
  131. package/src/hooks/use-license-check.ts +0 -65
  132. package/src/hooks/use-subscription.ts +0 -19
  133. package/src/hooks/use-toast.ts +0 -15
  134. package/src/index.ts +0 -14
  135. package/src/lib/ai-providers.ts +0 -377
  136. package/src/lib/component-metadata.ts +0 -18
  137. package/src/lib/micro-interactions.ts +0 -255
  138. package/src/lib/paddle.ts +0 -17
  139. package/src/lib/utils.ts +0 -6
  140. package/src/patterns/login-form/index.tsx +0 -276
  141. package/src/patterns/login-form/types.ts +0 -67
  142. package/src/setupTests.ts +0 -41
  143. package/src/styles/advanced-chart.css +0 -239
  144. package/src/styles/calendar.css +0 -35
  145. package/src/styles/design-system.css +0 -363
  146. package/src/styles/index.css +0 -85
  147. package/src/styles/tailwind.css +0 -7
  148. package/src/styles/tokens.css +0 -455
  149. package/src/types/moonui.d.ts +0 -22
  150. package/src/types/next-auth.d.ts +0 -21
  151. package/src/use-intersection-observer.tsx +0 -154
  152. package/src/use-local-storage.tsx +0 -71
  153. package/src/use-paddle.ts +0 -138
  154. package/src/use-performance-optimizer.ts +0 -389
  155. package/src/use-pro-access.ts +0 -141
  156. package/src/use-scroll-animation.ts +0 -219
  157. package/src/use-subscription.ts +0 -37
  158. package/src/use-toast.ts +0 -32
  159. package/src/utils/chart-helpers.ts +0 -357
  160. package/src/utils/cn.ts +0 -6
  161. package/src/utils/data-processing.ts +0 -151
  162. package/src/utils/license-validator.tsx +0 -183
@@ -155,22 +155,99 @@ const validateAuth = async (token) => {
155
155
  };
156
156
 
157
157
  const checkEnvironmentAuth = () => {
158
- // Check for auth token in environment variables
158
+ // Check for different types of auth tokens in environment variables
159
+ const teamToken = process.env.MOONUI_TEAM_TOKEN;
160
+ const ciToken = process.env.MOONUI_CI_TOKEN;
159
161
  const envToken = process.env.MOONUI_AUTH_TOKEN || process.env.MOONUI_ACCESS_TOKEN;
162
+ const licenseKey = process.env.MOONUI_LICENSE_KEY;
163
+
164
+ if (teamToken) {
165
+ log('👥 Found team token in environment variables', colors.blue);
166
+ return { token: teamToken, type: 'team' };
167
+ }
168
+
169
+ if (ciToken) {
170
+ log('🤖 Found CI token in environment variables', colors.blue);
171
+ return { token: ciToken, type: 'ci' };
172
+ }
173
+
174
+ if (licenseKey) {
175
+ log('🔐 Found license key in environment variables', colors.blue);
176
+ return { token: licenseKey, type: 'license' };
177
+ }
160
178
 
161
179
  if (envToken) {
162
180
  log('🔑 Found auth token in environment variables', colors.blue);
163
- return envToken;
181
+ return { token: envToken, type: 'personal' };
164
182
  }
165
183
 
166
184
  return null;
167
185
  };
168
186
 
187
+ // Check for perpetual license stamp
188
+ const checkLicenseStamp = () => {
189
+ try {
190
+ const stampPath = path.join(process.cwd(), '.moonui-license-stamp');
191
+ if (fs.existsSync(stampPath)) {
192
+ const stamp = JSON.parse(fs.readFileSync(stampPath, 'utf8'));
193
+
194
+ if (stamp.projectId && stamp.licensedAt && stamp.version) {
195
+ log('📋 Using perpetual license stamp', colors.green);
196
+ log(` Licensed on: ${new Date(stamp.licensedAt).toLocaleDateString()}`, colors.blue);
197
+ log(` Version: ${stamp.version}`, colors.blue);
198
+ log(` Mode: Perpetual (no active license required)`, colors.green);
199
+ log('', '');
200
+ log('ℹ️ Your project has perpetual usage rights', colors.cyan);
201
+ log(' Updates require active license', colors.gray);
202
+ return true;
203
+ }
204
+ }
205
+ } catch (error) {
206
+ // Invalid stamp
207
+ }
208
+ return false;
209
+ };
210
+
211
+ // Create license stamp for perpetual usage
212
+ const createLicenseStamp = (authInfo) => {
213
+ try {
214
+ const stamp = {
215
+ projectId: crypto.randomBytes(16).toString('hex'),
216
+ licensedAt: new Date().toISOString(),
217
+ version: process.env.npm_package_version || '2.x',
218
+ licensedTo: authInfo.user?.email || 'unknown',
219
+ plan: authInfo.user?.plan || 'unknown',
220
+ mode: 'perpetual',
221
+ installedComponents: []
222
+ };
223
+
224
+ const stampPath = path.join(process.cwd(), '.moonui-license-stamp');
225
+ fs.writeFileSync(stampPath, JSON.stringify(stamp, null, 2), 'utf8');
226
+
227
+ log('', '');
228
+ log('📋 License stamp created!', colors.green + colors.bright);
229
+ log(' Your project now has perpetual usage rights', colors.green);
230
+ log(' You can build without active license', colors.blue);
231
+ log(' Commit .moonui-license-stamp to your repository', colors.yellow);
232
+
233
+ return true;
234
+ } catch (error) {
235
+ return false;
236
+ }
237
+ };
238
+
169
239
  const main = async () => {
170
240
  log('', ''); // Empty line
171
241
  log('🌙 MoonUI Pro Installation', colors.cyan + colors.bright);
172
242
  log('═'.repeat(50), colors.gray);
173
243
 
244
+ // Check for perpetual license stamp first
245
+ if (checkLicenseStamp()) {
246
+ log('✅ Installation complete with perpetual license', colors.green);
247
+ log('═'.repeat(50), colors.gray);
248
+ return;
249
+ }
250
+
174
251
  // Check for development bypass
175
252
  if (process.env.MOONUI_DEV_MODE === 'true' || process.env.MOONUI_SKIP_AUTH === 'true') {
176
253
  log('🔧 Development Mode Enabled', colors.yellow + colors.bright);
@@ -197,35 +274,108 @@ const main = async () => {
197
274
  return;
198
275
  }
199
276
 
200
- // Check if this is a CI environment
201
- const isCI = process.env.CI || process.env.CONTINUOUS_INTEGRATION || process.env.GITHUB_ACTIONS;
277
+ // Check for environment-based authentication first
278
+ const envAuth = checkEnvironmentAuth();
202
279
 
203
- if (isCI) {
204
- log('🤖 CI Environment detected', colors.blue);
280
+ if (envAuth) {
281
+ // Handle different token types
282
+ if (envAuth.type === 'team') {
283
+ log('🏢 Team License Mode', colors.blue + colors.bright);
284
+ log(' Multiple developers can use this token', colors.blue);
285
+ log(' No device restrictions applied', colors.gray);
286
+
287
+ // Validate team token
288
+ log('🔍 Validating team license...', colors.blue);
289
+ const validation = await validateAuth(envAuth.token);
290
+
291
+ if (validation.valid) {
292
+ log('✅ Team authentication successful!', colors.green);
293
+ log(` Team: ${validation.team?.name || 'N/A'}`, colors.blue);
294
+ log(` Seats: ${validation.team?.seats || 'N/A'}`, colors.blue);
295
+
296
+ // Create perpetual license stamp for team
297
+ createLicenseStamp({ user: { email: 'team', plan: 'team' }, ...validation });
298
+ return;
299
+ } else {
300
+ log('❌ Team authentication failed!', colors.red);
301
+ log(` Error: ${validation.message}`, colors.red);
302
+ process.exit(1);
303
+ }
304
+ }
205
305
 
206
- const envToken = checkEnvironmentAuth();
207
- if (!envToken) {
208
- log('⚠️ No auth token found in CI environment', colors.yellow);
209
- log(' Set MOONUI_AUTH_TOKEN environment variable for CI builds', colors.gray);
210
- log(' Get your token by running: moonui whoami --token', colors.gray);
211
- return;
306
+ if (envAuth.type === 'ci' || process.env.CI) {
307
+ log('🤖 CI/CD Mode', colors.blue + colors.bright);
308
+ log(' Running in continuous integration environment', colors.blue);
309
+
310
+ // Validate CI token
311
+ log('🔍 Validating CI token...', colors.blue);
312
+ const validation = await validateAuth(envAuth.token);
313
+
314
+ if (validation.valid) {
315
+ log('✅ CI authentication successful!', colors.green);
316
+ log(` Environment: ${process.env.CI_NAME || 'CI'}`, colors.blue);
317
+ return;
318
+ } else {
319
+ log('❌ CI authentication failed!', colors.red);
320
+ log(` Error: ${validation.message}`, colors.red);
321
+ process.exit(1);
322
+ }
212
323
  }
213
324
 
214
- // Validate CI auth
215
- log('🔍 Validating CI authentication...', colors.blue);
216
- const validation = await validateAuth(envToken);
325
+ if (envAuth.type === 'license') {
326
+ log('🔐 License Key Mode', colors.blue + colors.bright);
327
+ log(' Project-based licensing', colors.blue);
328
+
329
+ // Validate license key
330
+ log('🔍 Validating license key...', colors.blue);
331
+ const validation = await validateAuth(envAuth.token);
332
+
333
+ if (validation.valid) {
334
+ log('✅ License validation successful!', colors.green);
335
+ log(` Project: ${validation.project?.name || 'N/A'}`, colors.blue);
336
+ log(` Valid until: ${validation.expiresAt || 'Lifetime'}`, colors.blue);
337
+ return;
338
+ } else {
339
+ log('❌ License validation failed!', colors.red);
340
+ log(` Error: ${validation.message}`, colors.red);
341
+ process.exit(1);
342
+ }
343
+ }
217
344
 
218
- if (validation.valid) {
219
- log('✅ CI Authentication successful!', colors.green);
220
- log(` Plan: ${validation.user?.plan || 'N/A'}`, colors.blue);
221
- return;
222
- } else {
223
- log('❌ CI Authentication failed!', colors.red);
224
- log(` Error: ${validation.message}`, colors.red);
225
- process.exit(1);
345
+ // Personal token with existing flow
346
+ if (envAuth.type === 'personal') {
347
+ log('👤 Personal License Mode', colors.blue + colors.bright);
348
+ log(' Single developer license', colors.blue);
349
+
350
+ const validation = await validateAuth(envAuth.token);
351
+
352
+ if (validation.valid) {
353
+ log('✅ Personal authentication successful!', colors.green);
354
+ log(` User: ${validation.user?.email || 'N/A'}`, colors.blue);
355
+ return;
356
+ } else {
357
+ log('❌ Personal authentication failed!', colors.red);
358
+ log(` Error: ${validation.message}`, colors.red);
359
+ process.exit(1);
360
+ }
226
361
  }
227
362
  }
228
363
 
364
+ // Check if this is a CI environment without token
365
+ const isCI = process.env.CI || process.env.CONTINUOUS_INTEGRATION || process.env.GITHUB_ACTIONS;
366
+
367
+ if (isCI && !envAuth) {
368
+ log('⚠️ CI Environment detected but no auth token found', colors.yellow);
369
+ log('', '');
370
+ log('📋 Please set one of the following environment variables:', colors.blue);
371
+ log(' MOONUI_CI_TOKEN - For CI/CD pipelines', colors.blue);
372
+ log(' MOONUI_TEAM_TOKEN - For team licenses', colors.blue);
373
+ log(' MOONUI_LICENSE_KEY - For project licenses', colors.blue);
374
+ log('', '');
375
+ log('📖 Documentation: https://moonui.dev/docs/ci-setup', colors.gray);
376
+ return;
377
+ }
378
+
229
379
  // Regular installation - check for existing auth
230
380
  const authConfig = getAuthConfig();
231
381
 
@@ -273,6 +423,9 @@ const main = async () => {
273
423
  log(` Plan: ${validation.user?.plan || authConfig.user?.plan}`, colors.blue);
274
424
  log(` Account: ${authConfig.user?.email || 'N/A'}`, colors.blue);
275
425
 
426
+ // Create perpetual license stamp
427
+ createLicenseStamp(authConfig);
428
+
276
429
  if (validation.expiresAt) {
277
430
  const expiryDate = new Date(validation.expiresAt);
278
431
  const daysLeft = Math.ceil((expiryDate - new Date()) / (1000 * 60 * 60 * 24));
@@ -1,216 +0,0 @@
1
- import { renderHook, act } from '@testing-library/react';
2
- import { useIntersectionObserver } from '../use-intersection-observer';
3
-
4
- // Mock IntersectionObserver
5
- const mockIntersectionObserver = jest.fn();
6
- mockIntersectionObserver.mockImplementation((callback, options) => {
7
- return {
8
- observe: jest.fn(),
9
- unobserve: jest.fn(),
10
- disconnect: jest.fn(),
11
- };
12
- });
13
-
14
- beforeAll(() => {
15
- global.IntersectionObserver = mockIntersectionObserver;
16
- });
17
-
18
- describe('useIntersectionObserver', () => {
19
- beforeEach(() => {
20
- jest.clearAllMocks();
21
- });
22
-
23
- it('should return ref and isIntersecting state', () => {
24
- const { result } = renderHook(() => useIntersectionObserver());
25
-
26
- expect(result.current.ref).toBeDefined();
27
- expect(result.current.isIntersecting).toBe(false);
28
- expect(typeof result.current.ref).toBe('function');
29
- });
30
-
31
- it('should create IntersectionObserver with default options', () => {
32
- renderHook(() => useIntersectionObserver());
33
-
34
- expect(mockIntersectionObserver).toHaveBeenCalledWith(
35
- expect.any(Function),
36
- {
37
- threshold: 0,
38
- root: null,
39
- rootMargin: '0%',
40
- }
41
- );
42
- });
43
-
44
- it('should create IntersectionObserver with custom options', () => {
45
- const options = {
46
- threshold: 0.5,
47
- root: document.body,
48
- rootMargin: '10px',
49
- };
50
-
51
- renderHook(() => useIntersectionObserver(options));
52
-
53
- expect(mockIntersectionObserver).toHaveBeenCalledWith(
54
- expect.any(Function),
55
- options
56
- );
57
- });
58
-
59
- it('should observe element when ref is set', () => {
60
- const mockObserve = jest.fn();
61
- mockIntersectionObserver.mockImplementation(() => ({
62
- observe: mockObserve,
63
- unobserve: jest.fn(),
64
- disconnect: jest.fn(),
65
- }));
66
-
67
- const { result } = renderHook(() => useIntersectionObserver());
68
-
69
- const mockElement = document.createElement('div');
70
-
71
- act(() => {
72
- result.current.ref(mockElement);
73
- });
74
-
75
- expect(mockObserve).toHaveBeenCalledWith(mockElement);
76
- });
77
-
78
- it('should call onChange when intersection state changes', () => {
79
- const mockOnChange = jest.fn();
80
- let observerCallback: any;
81
-
82
- mockIntersectionObserver.mockImplementation((callback) => {
83
- observerCallback = callback;
84
- return {
85
- observe: jest.fn(),
86
- unobserve: jest.fn(),
87
- disconnect: jest.fn(),
88
- };
89
- });
90
-
91
- const { result } = renderHook(() =>
92
- useIntersectionObserver({ onChange: mockOnChange })
93
- );
94
-
95
- const mockElement = document.createElement('div');
96
- const mockEntry = {
97
- isIntersecting: true,
98
- target: mockElement,
99
- };
100
-
101
- act(() => {
102
- result.current.ref(mockElement);
103
- });
104
-
105
- act(() => {
106
- observerCallback([mockEntry]);
107
- });
108
-
109
- expect(mockOnChange).toHaveBeenCalledWith(true, mockEntry);
110
- });
111
-
112
- it('should freeze state when freezeOnceVisible is true', () => {
113
- let observerCallback: any;
114
-
115
- mockIntersectionObserver.mockImplementation((callback) => {
116
- observerCallback = callback;
117
- return {
118
- observe: jest.fn(),
119
- unobserve: jest.fn(),
120
- disconnect: jest.fn(),
121
- };
122
- });
123
-
124
- const { result } = renderHook(() =>
125
- useIntersectionObserver({ freezeOnceVisible: true })
126
- );
127
-
128
- const mockElement = document.createElement('div');
129
- const mockEntry = {
130
- isIntersecting: true,
131
- target: mockElement,
132
- };
133
-
134
- act(() => {
135
- result.current.ref(mockElement);
136
- });
137
-
138
- // First intersection - should update
139
- act(() => {
140
- observerCallback([mockEntry]);
141
- });
142
-
143
- expect(result.current.isIntersecting).toBe(true);
144
-
145
- // Second intersection with false - should not update (frozen)
146
- act(() => {
147
- observerCallback([{ ...mockEntry, isIntersecting: false }]);
148
- });
149
-
150
- expect(result.current.isIntersecting).toBe(true);
151
- });
152
-
153
- it('should disconnect observer on unmount', () => {
154
- const mockDisconnect = jest.fn();
155
- mockIntersectionObserver.mockImplementation(() => ({
156
- observe: jest.fn(),
157
- unobserve: jest.fn(),
158
- disconnect: mockDisconnect,
159
- }));
160
-
161
- const { unmount } = renderHook(() => useIntersectionObserver());
162
-
163
- unmount();
164
-
165
- expect(mockDisconnect).toHaveBeenCalled();
166
- });
167
-
168
- it('should handle initialIsIntersecting option', () => {
169
- const { result } = renderHook(() =>
170
- useIntersectionObserver({ initialIsIntersecting: true })
171
- );
172
-
173
- expect(result.current.isIntersecting).toBe(true);
174
- });
175
-
176
- it('should not create observer when IntersectionObserver is not supported', () => {
177
- // Temporarily remove IntersectionObserver
178
- const originalIntersectionObserver = global.IntersectionObserver;
179
- (global as any).IntersectionObserver = undefined;
180
-
181
- const { result } = renderHook(() => useIntersectionObserver());
182
-
183
- expect(result.current.ref).toBeDefined();
184
- expect(result.current.isIntersecting).toBe(false);
185
-
186
- // Restore IntersectionObserver
187
- global.IntersectionObserver = originalIntersectionObserver;
188
- });
189
-
190
- it('should handle multiple elements with useMultipleIntersectionObserver', () => {
191
- // This would be a separate hook, but we can test the concept
192
- const mockObserve = jest.fn();
193
- const mockUnobserve = jest.fn();
194
-
195
- mockIntersectionObserver.mockImplementation(() => ({
196
- observe: mockObserve,
197
- unobserve: mockUnobserve,
198
- disconnect: jest.fn(),
199
- }));
200
-
201
- const { result } = renderHook(() => useIntersectionObserver());
202
-
203
- const element1 = document.createElement('div');
204
- const element2 = document.createElement('div');
205
-
206
- act(() => {
207
- result.current.ref(element1);
208
- });
209
-
210
- act(() => {
211
- result.current.ref(element2);
212
- });
213
-
214
- expect(mockObserve).toHaveBeenCalledWith(element2);
215
- });
216
- });
@@ -1,174 +0,0 @@
1
- import { renderHook, act } from '@testing-library/react'
2
- import { useLocalStorage } from '../use-local-storage'
3
-
4
- // Mock localStorage
5
- const mockLocalStorage = (() => {
6
- let store: Record<string, string> = {}
7
-
8
- return {
9
- getItem: jest.fn((key: string) => store[key] || null),
10
- setItem: jest.fn((key: string, value: string) => {
11
- store[key] = value
12
- }),
13
- removeItem: jest.fn((key: string) => {
14
- delete store[key]
15
- }),
16
- clear: jest.fn(() => {
17
- store = {}
18
- }),
19
- get length() {
20
- return Object.keys(store).length
21
- },
22
- key: jest.fn((index: number) => Object.keys(store)[index] || null),
23
- }
24
- })()
25
-
26
- Object.defineProperty(window, 'localStorage', {
27
- value: mockLocalStorage,
28
- })
29
-
30
- describe('useLocalStorage', () => {
31
- beforeEach(() => {
32
- mockLocalStorage.clear()
33
- jest.clearAllMocks()
34
- })
35
-
36
- it('returns initial value when localStorage is empty', () => {
37
- const { result } = renderHook(() => useLocalStorage('test-key', 'initial'))
38
-
39
- expect(result.current[0]).toBe('initial')
40
- expect(mockLocalStorage.getItem).toHaveBeenCalledWith('test-key')
41
- })
42
-
43
- it('returns stored value from localStorage', () => {
44
- mockLocalStorage.setItem('test-key', JSON.stringify('stored-value'))
45
-
46
- const { result } = renderHook(() => useLocalStorage('test-key', 'initial'))
47
-
48
- expect(result.current[0]).toBe('stored-value')
49
- })
50
-
51
- it('stores value in localStorage when setValue is called', () => {
52
- const { result } = renderHook(() => useLocalStorage('test-key', 'initial'))
53
-
54
- act(() => {
55
- result.current[1]('new-value')
56
- })
57
-
58
- expect(result.current[0]).toBe('new-value')
59
- expect(mockLocalStorage.setItem).toHaveBeenCalledWith('test-key', JSON.stringify('new-value'))
60
- })
61
-
62
- it('handles function updates', () => {
63
- const { result } = renderHook(() => useLocalStorage('test-key', 10))
64
-
65
- act(() => {
66
- result.current[1]((prev) => (prev ?? 0) + 5)
67
- })
68
-
69
- expect(result.current[0]).toBe(15)
70
- expect(mockLocalStorage.setItem).toHaveBeenCalledWith('test-key', JSON.stringify(15))
71
- })
72
-
73
- it('handles complex objects', () => {
74
- const initialObject = { name: 'test', count: 0 }
75
- const { result } = renderHook(() => useLocalStorage('object-key', initialObject))
76
-
77
- const newObject = { name: 'updated', count: 5 }
78
-
79
- act(() => {
80
- result.current[1](newObject)
81
- })
82
-
83
- expect(result.current[0]).toEqual(newObject)
84
- expect(mockLocalStorage.setItem).toHaveBeenCalledWith('object-key', JSON.stringify(newObject))
85
- })
86
-
87
- it('handles localStorage errors gracefully', () => {
88
- mockLocalStorage.setItem.mockImplementation(() => {
89
- throw new Error('Storage quota exceeded')
90
- })
91
-
92
- const consoleSpy = jest.spyOn(console, 'error').mockImplementation()
93
-
94
- const { result } = renderHook(() => useLocalStorage('test-key', 'initial'))
95
-
96
- act(() => {
97
- result.current[1]('new-value')
98
- })
99
-
100
- // Should still update the state even if localStorage fails
101
- expect(result.current[0]).toBe('new-value')
102
- expect(consoleSpy).toHaveBeenCalled()
103
-
104
- consoleSpy.mockRestore()
105
- })
106
-
107
- it('handles malformed JSON in localStorage', () => {
108
- mockLocalStorage.getItem.mockReturnValue('invalid-json{')
109
-
110
- const consoleSpy = jest.spyOn(console, 'error').mockImplementation()
111
-
112
- const { result } = renderHook(() => useLocalStorage('test-key', 'initial'))
113
-
114
- // Should fall back to initial value when JSON parsing fails
115
- expect(result.current[0]).toBe('initial')
116
- expect(consoleSpy).toHaveBeenCalled()
117
-
118
- consoleSpy.mockRestore()
119
- })
120
-
121
- it('removes item from localStorage when value is undefined', () => {
122
- mockLocalStorage.setItem('test-key', JSON.stringify('stored-value'))
123
-
124
- const { result } = renderHook(() => useLocalStorage('test-key', 'initial'))
125
-
126
- act(() => {
127
- result.current[1](undefined)
128
- })
129
-
130
- expect(result.current[0]).toBeUndefined()
131
- expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('test-key')
132
- })
133
-
134
- it('handles boolean values correctly', () => {
135
- const { result } = renderHook(() => useLocalStorage('bool-key', false))
136
-
137
- act(() => {
138
- result.current[1](true)
139
- })
140
-
141
- expect(result.current[0]).toBe(true)
142
- expect(mockLocalStorage.setItem).toHaveBeenCalledWith('bool-key', JSON.stringify(true))
143
- })
144
-
145
- it('handles null values correctly', () => {
146
- const { result } = renderHook(() => useLocalStorage<string | null>('null-key', null))
147
-
148
- act(() => {
149
- result.current[1]('not-null')
150
- })
151
-
152
- expect(result.current[0]).toBe('not-null')
153
-
154
- act(() => {
155
- result.current[1](null)
156
- })
157
-
158
- expect(result.current[0]).toBe(null)
159
- expect(mockLocalStorage.setItem).toHaveBeenCalledWith('null-key', JSON.stringify(null))
160
- })
161
-
162
- it('maintains state consistency across multiple hook instances with same key', () => {
163
- const { result: result1 } = renderHook(() => useLocalStorage('shared-key', 'initial'))
164
- const { result: result2 } = renderHook(() => useLocalStorage('shared-key', 'initial'))
165
-
166
- act(() => {
167
- result1.current[1]('updated')
168
- })
169
-
170
- // Both instances should have the same value
171
- expect(result1.current[0]).toBe('updated')
172
- expect(result2.current[0]).toBe('updated')
173
- })
174
- })