|
| 1 | +/** |
| 2 | + * ObjectUI — useDataRefresh Tests |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * Tests for the reusable data-refresh hook (P1/P2). |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect, vi } from 'vitest'; |
| 9 | +import { renderHook, act } from '@testing-library/react'; |
| 10 | +import { useDataRefresh } from '../useDataRefresh'; |
| 11 | + |
| 12 | +describe('useDataRefresh', () => { |
| 13 | + it('should return refreshKey=0 and a refresh function', () => { |
| 14 | + const { result } = renderHook(() => useDataRefresh(undefined, undefined)); |
| 15 | + |
| 16 | + expect(result.current.refreshKey).toBe(0); |
| 17 | + expect(typeof result.current.refresh).toBe('function'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should increment refreshKey when refresh() is called', () => { |
| 21 | + const { result } = renderHook(() => useDataRefresh(undefined, 'contacts')); |
| 22 | + |
| 23 | + act(() => { |
| 24 | + result.current.refresh(); |
| 25 | + }); |
| 26 | + |
| 27 | + expect(result.current.refreshKey).toBe(1); |
| 28 | + |
| 29 | + act(() => { |
| 30 | + result.current.refresh(); |
| 31 | + }); |
| 32 | + |
| 33 | + expect(result.current.refreshKey).toBe(2); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should auto-subscribe to DataSource.onMutation() when available', () => { |
| 37 | + let listener: ((event: any) => void) | null = null; |
| 38 | + const unsub = vi.fn(); |
| 39 | + const ds: any = { |
| 40 | + onMutation: vi.fn((cb: any) => { |
| 41 | + listener = cb; |
| 42 | + return unsub; |
| 43 | + }), |
| 44 | + }; |
| 45 | + |
| 46 | + const { result } = renderHook(() => useDataRefresh(ds, 'contacts')); |
| 47 | + |
| 48 | + expect(ds.onMutation).toHaveBeenCalledOnce(); |
| 49 | + |
| 50 | + // Simulate a mutation on the same resource |
| 51 | + act(() => { |
| 52 | + listener?.({ type: 'create', resource: 'contacts' }); |
| 53 | + }); |
| 54 | + |
| 55 | + expect(result.current.refreshKey).toBe(1); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should NOT increment refreshKey for mutations on a different resource', () => { |
| 59 | + let listener: ((event: any) => void) | null = null; |
| 60 | + const ds: any = { |
| 61 | + onMutation: vi.fn((cb: any) => { |
| 62 | + listener = cb; |
| 63 | + return vi.fn(); |
| 64 | + }), |
| 65 | + }; |
| 66 | + |
| 67 | + const { result } = renderHook(() => useDataRefresh(ds, 'contacts')); |
| 68 | + |
| 69 | + act(() => { |
| 70 | + listener?.({ type: 'create', resource: 'accounts' }); |
| 71 | + }); |
| 72 | + |
| 73 | + expect(result.current.refreshKey).toBe(0); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should unsubscribe on unmount', () => { |
| 77 | + const unsub = vi.fn(); |
| 78 | + const ds: any = { |
| 79 | + onMutation: vi.fn(() => unsub), |
| 80 | + }; |
| 81 | + |
| 82 | + const { unmount } = renderHook(() => useDataRefresh(ds, 'contacts')); |
| 83 | + |
| 84 | + expect(unsub).not.toHaveBeenCalled(); |
| 85 | + |
| 86 | + unmount(); |
| 87 | + |
| 88 | + expect(unsub).toHaveBeenCalledOnce(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should work without onMutation (backward compatible)', () => { |
| 92 | + const ds: any = { |
| 93 | + find: vi.fn(), |
| 94 | + }; |
| 95 | + |
| 96 | + const { result } = renderHook(() => useDataRefresh(ds, 'contacts')); |
| 97 | + |
| 98 | + expect(result.current.refreshKey).toBe(0); |
| 99 | + // Should not throw |
| 100 | + act(() => { |
| 101 | + result.current.refresh(); |
| 102 | + }); |
| 103 | + expect(result.current.refreshKey).toBe(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should skip subscription when objectName is undefined', () => { |
| 107 | + const ds: any = { |
| 108 | + onMutation: vi.fn(() => vi.fn()), |
| 109 | + }; |
| 110 | + |
| 111 | + renderHook(() => useDataRefresh(ds, undefined)); |
| 112 | + |
| 113 | + expect(ds.onMutation).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments