|
1 | 1 | import { TSource } from '@hyperdx/common-utils/dist/types';
|
| 2 | +import { act, renderHook } from '@testing-library/react'; |
2 | 3 |
|
3 | 4 | import { MetricsDataType, NumberFormat } from '../types';
|
| 5 | +import * as utils from '../utils'; |
4 | 6 | import {
|
5 | 7 | formatAttributeClause,
|
6 | 8 | formatDate,
|
@@ -268,3 +270,204 @@ describe('formatNumber', () => {
|
268 | 270 | });
|
269 | 271 | });
|
270 | 272 | });
|
| 273 | + |
| 274 | +describe('useLocalStorage', () => { |
| 275 | + // Create a mock for localStorage |
| 276 | + let localStorageMock: jest.Mocked<Storage>; |
| 277 | + |
| 278 | + beforeEach(() => { |
| 279 | + // Clear all mocks between tests |
| 280 | + jest.clearAllMocks(); |
| 281 | + |
| 282 | + // Create localStorage mock |
| 283 | + localStorageMock = { |
| 284 | + getItem: jest.fn().mockImplementation((_: string) => null), |
| 285 | + setItem: jest.fn(), |
| 286 | + removeItem: jest.fn(), |
| 287 | + clear: jest.fn(), |
| 288 | + key: jest.fn(), |
| 289 | + length: 0, |
| 290 | + }; |
| 291 | + |
| 292 | + // Replace window.localStorage with our mock |
| 293 | + Object.defineProperty(window, 'localStorage', { |
| 294 | + value: localStorageMock, |
| 295 | + writable: true, |
| 296 | + }); |
| 297 | + }); |
| 298 | + |
| 299 | + afterAll(() => { |
| 300 | + // Restore original implementations |
| 301 | + jest.restoreAllMocks(); |
| 302 | + }); |
| 303 | + |
| 304 | + test('should initialize with initial value when localStorage is empty', () => { |
| 305 | + // Mock localStorage.getItem to return null (empty) |
| 306 | + localStorageMock.getItem.mockReturnValueOnce(null); |
| 307 | + |
| 308 | + const initialValue = { test: 'value' }; |
| 309 | + const { result } = renderHook(() => |
| 310 | + utils.useLocalStorage('testKey', initialValue), |
| 311 | + ); |
| 312 | + |
| 313 | + // Check if initialized with initial value |
| 314 | + expect(result.current[0]).toEqual(initialValue); |
| 315 | + |
| 316 | + // Verify localStorage was checked |
| 317 | + expect(localStorageMock.getItem).toHaveBeenCalledWith('testKey'); |
| 318 | + }); |
| 319 | + |
| 320 | + test('should retrieve existing value from localStorage', () => { |
| 321 | + // Mock localStorage to return existing value |
| 322 | + const existingValue = { test: 'existing' }; |
| 323 | + localStorageMock.getItem.mockReturnValueOnce(JSON.stringify(existingValue)); |
| 324 | + |
| 325 | + const { result } = renderHook(() => |
| 326 | + utils.useLocalStorage('testKey', { test: 'default' }), |
| 327 | + ); |
| 328 | + |
| 329 | + // Should use the value from localStorage, not the initial value |
| 330 | + expect(result.current[0]).toEqual(existingValue); |
| 331 | + expect(localStorageMock.getItem).toHaveBeenCalledWith('testKey'); |
| 332 | + }); |
| 333 | + |
| 334 | + test('should update localStorage when setValue is called', () => { |
| 335 | + localStorageMock.getItem.mockReturnValueOnce(null); |
| 336 | + |
| 337 | + const { result } = renderHook(() => |
| 338 | + utils.useLocalStorage('testKey', 'initial'), |
| 339 | + ); |
| 340 | + |
| 341 | + // Update value |
| 342 | + const newValue = 'updated'; |
| 343 | + act(() => { |
| 344 | + result.current[1](newValue); |
| 345 | + }); |
| 346 | + |
| 347 | + // Check if state updated |
| 348 | + expect(result.current[0]).toBe(newValue); |
| 349 | + |
| 350 | + // Check if localStorage was updated |
| 351 | + expect(localStorageMock.setItem).toHaveBeenCalledWith( |
| 352 | + 'testKey', |
| 353 | + JSON.stringify(newValue), |
| 354 | + ); |
| 355 | + }); |
| 356 | + |
| 357 | + test('should handle functional updates', () => { |
| 358 | + localStorageMock.getItem.mockReturnValueOnce(JSON.stringify(0)); |
| 359 | + |
| 360 | + const { result } = renderHook(() => |
| 361 | + utils.useLocalStorage<number>('testKey', 0), |
| 362 | + ); |
| 363 | + |
| 364 | + // Update using function |
| 365 | + act(() => { |
| 366 | + result.current[1](prev => prev + 1); |
| 367 | + }); |
| 368 | + |
| 369 | + // Check if state updated correctly |
| 370 | + expect(result.current[0]).toBe(1); |
| 371 | + |
| 372 | + // Check if localStorage was updated |
| 373 | + expect(localStorageMock.setItem).toHaveBeenCalledWith( |
| 374 | + 'testKey', |
| 375 | + JSON.stringify(1), |
| 376 | + ); |
| 377 | + }); |
| 378 | + |
| 379 | + test('should handle storage event from another window', () => { |
| 380 | + // Initial setup |
| 381 | + localStorageMock.getItem.mockReturnValueOnce(JSON.stringify('initial')); |
| 382 | + |
| 383 | + const { result } = renderHook(() => |
| 384 | + utils.useLocalStorage('testKey', 'initial'), |
| 385 | + ); |
| 386 | + |
| 387 | + // Update mock to return new value when checked after event |
| 388 | + localStorageMock.getItem.mockReturnValue(JSON.stringify('external update')); |
| 389 | + |
| 390 | + // Dispatch storage event |
| 391 | + act(() => { |
| 392 | + window.dispatchEvent( |
| 393 | + new StorageEvent('storage', { |
| 394 | + key: 'testKey', |
| 395 | + newValue: JSON.stringify('external update'), |
| 396 | + }), |
| 397 | + ); |
| 398 | + }); |
| 399 | + |
| 400 | + // State should be updated |
| 401 | + expect(result.current[0]).toBe('external update'); |
| 402 | + }); |
| 403 | + |
| 404 | + test('should handle customStorage event from same window but different hook instance', () => { |
| 405 | + // First hook instance |
| 406 | + localStorageMock.getItem.mockReturnValueOnce(JSON.stringify('initial1')); |
| 407 | + const { result: result1 } = renderHook(() => |
| 408 | + utils.useLocalStorage('sharedKey', 'initial1'), |
| 409 | + ); |
| 410 | + |
| 411 | + // Second hook instance |
| 412 | + localStorageMock.getItem.mockReturnValueOnce(JSON.stringify('initial1')); |
| 413 | + const { result: result2 } = renderHook(() => |
| 414 | + utils.useLocalStorage('sharedKey', 'initial2'), |
| 415 | + ); |
| 416 | + |
| 417 | + // Clear mock calls count |
| 418 | + localStorageMock.getItem.mockClear(); |
| 419 | + |
| 420 | + // When the second hook checks localStorage after custom event |
| 421 | + localStorageMock.getItem.mockReturnValue( |
| 422 | + JSON.stringify('updated by hook 1'), |
| 423 | + ); |
| 424 | + |
| 425 | + // Update value in the first instance |
| 426 | + act(() => { |
| 427 | + result1.current[1]('updated by hook 1'); |
| 428 | + }); |
| 429 | + |
| 430 | + // Manually trigger custom event (since it's happening within the same test) |
| 431 | + act(() => { |
| 432 | + const event = new CustomEvent<utils.CustomStorageChangeDetail>( |
| 433 | + 'customStorage', |
| 434 | + { |
| 435 | + detail: { |
| 436 | + key: 'sharedKey', |
| 437 | + instanceId: 'some-id', // Different from the instance updating |
| 438 | + }, |
| 439 | + }, |
| 440 | + ); |
| 441 | + window.dispatchEvent(event); |
| 442 | + }); |
| 443 | + |
| 444 | + // The second instance should have updated values |
| 445 | + expect(result2.current[0]).toBe('updated by hook 1'); |
| 446 | + }); |
| 447 | + |
| 448 | + test('should not update if storage event is for a different key', () => { |
| 449 | + // Initial setup |
| 450 | + localStorageMock.getItem.mockReturnValueOnce(JSON.stringify('initial')); |
| 451 | + const { result } = renderHook(() => |
| 452 | + utils.useLocalStorage('testKey', 'initial'), |
| 453 | + ); |
| 454 | + |
| 455 | + // Clear the mock calls counter |
| 456 | + localStorageMock.getItem.mockClear(); |
| 457 | + |
| 458 | + // Simulate storage event for a different key |
| 459 | + act(() => { |
| 460 | + window.dispatchEvent( |
| 461 | + new StorageEvent('storage', { |
| 462 | + key: 'differentKey', |
| 463 | + newValue: JSON.stringify('different value'), |
| 464 | + }), |
| 465 | + ); |
| 466 | + }); |
| 467 | + |
| 468 | + // State should remain unchanged |
| 469 | + expect(result.current[0]).toBe('initial'); |
| 470 | + // localStorage should not be accessed since key doesn't match |
| 471 | + expect(localStorageMock.getItem).not.toHaveBeenCalled(); |
| 472 | + }); |
| 473 | +}); |
0 commit comments