|
| 1 | +/** |
| 2 | + * Tests for useDynamicApp hook |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 6 | +import { renderHook, waitFor, act } from '@testing-library/react'; |
| 7 | +import { useDynamicApp } from '../useDynamicApp'; |
| 8 | + |
| 9 | +describe('useDynamicApp', () => { |
| 10 | + const staticConfig = { |
| 11 | + name: 'crm', |
| 12 | + label: 'CRM App', |
| 13 | + objects: ['contact', 'account'], |
| 14 | + }; |
| 15 | + |
| 16 | + it('returns static config when no adapter is provided', () => { |
| 17 | + const { result } = renderHook(() => |
| 18 | + useDynamicApp({ appId: 'crm', staticConfig }), |
| 19 | + ); |
| 20 | + |
| 21 | + expect(result.current.config).toEqual(staticConfig); |
| 22 | + expect(result.current.isLoading).toBe(false); |
| 23 | + expect(result.current.isServerConfig).toBe(false); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns null config when neither adapter nor static config provided', () => { |
| 27 | + const { result } = renderHook(() => |
| 28 | + useDynamicApp({ appId: 'crm' }), |
| 29 | + ); |
| 30 | + |
| 31 | + expect(result.current.config).toBeNull(); |
| 32 | + expect(result.current.isLoading).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it('loads config from server adapter', async () => { |
| 36 | + const serverConfig = { name: 'crm', label: 'CRM (Server)', objects: ['contact'] }; |
| 37 | + const adapter = { |
| 38 | + getApp: vi.fn().mockResolvedValue(serverConfig), |
| 39 | + }; |
| 40 | + |
| 41 | + const { result } = renderHook(() => |
| 42 | + useDynamicApp({ appId: 'crm', staticConfig, adapter }), |
| 43 | + ); |
| 44 | + |
| 45 | + // Initially loading |
| 46 | + expect(result.current.isLoading).toBe(true); |
| 47 | + |
| 48 | + await waitFor(() => { |
| 49 | + expect(result.current.isLoading).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + expect(result.current.config).toEqual(serverConfig); |
| 53 | + expect(result.current.isServerConfig).toBe(true); |
| 54 | + expect(adapter.getApp).toHaveBeenCalledWith('crm'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('falls back to static config when server returns null', async () => { |
| 58 | + const adapter = { |
| 59 | + getApp: vi.fn().mockResolvedValue(null), |
| 60 | + }; |
| 61 | + |
| 62 | + const { result } = renderHook(() => |
| 63 | + useDynamicApp({ appId: 'crm', staticConfig, adapter }), |
| 64 | + ); |
| 65 | + |
| 66 | + await waitFor(() => { |
| 67 | + expect(result.current.isLoading).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + expect(result.current.config).toEqual(staticConfig); |
| 71 | + expect(result.current.isServerConfig).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('falls back to static config on server error', async () => { |
| 75 | + const adapter = { |
| 76 | + getApp: vi.fn().mockRejectedValue(new Error('Network error')), |
| 77 | + }; |
| 78 | + |
| 79 | + const { result } = renderHook(() => |
| 80 | + useDynamicApp({ appId: 'crm', staticConfig, adapter }), |
| 81 | + ); |
| 82 | + |
| 83 | + await waitFor(() => { |
| 84 | + expect(result.current.isLoading).toBe(false); |
| 85 | + }); |
| 86 | + |
| 87 | + expect(result.current.config).toEqual(staticConfig); |
| 88 | + expect(result.current.isServerConfig).toBe(false); |
| 89 | + expect(result.current.error?.message).toBe('Network error'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('skips loading when enabled is false', () => { |
| 93 | + const adapter = { |
| 94 | + getApp: vi.fn(), |
| 95 | + }; |
| 96 | + |
| 97 | + const { result } = renderHook(() => |
| 98 | + useDynamicApp({ appId: 'crm', staticConfig, adapter, enabled: false }), |
| 99 | + ); |
| 100 | + |
| 101 | + expect(result.current.config).toEqual(staticConfig); |
| 102 | + expect(result.current.isLoading).toBe(false); |
| 103 | + expect(adapter.getApp).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('refresh invalidates cache and re-fetches', async () => { |
| 107 | + let callCount = 0; |
| 108 | + const adapter = { |
| 109 | + getApp: vi.fn().mockImplementation(async () => { |
| 110 | + callCount++; |
| 111 | + return { name: 'crm', version: callCount }; |
| 112 | + }), |
| 113 | + invalidateCache: vi.fn(), |
| 114 | + }; |
| 115 | + |
| 116 | + const { result } = renderHook(() => |
| 117 | + useDynamicApp({ appId: 'crm', adapter }), |
| 118 | + ); |
| 119 | + |
| 120 | + await waitFor(() => { |
| 121 | + expect(result.current.isLoading).toBe(false); |
| 122 | + }); |
| 123 | + |
| 124 | + expect((result.current.config as any).version).toBe(1); |
| 125 | + |
| 126 | + await act(async () => { |
| 127 | + await result.current.refresh(); |
| 128 | + }); |
| 129 | + |
| 130 | + expect(adapter.invalidateCache).toHaveBeenCalledWith('app:crm'); |
| 131 | + expect((result.current.config as any).version).toBe(2); |
| 132 | + }); |
| 133 | + |
| 134 | + it('reloads when appId changes', async () => { |
| 135 | + const adapter = { |
| 136 | + getApp: vi.fn().mockImplementation(async (appId: string) => { |
| 137 | + return { name: appId }; |
| 138 | + }), |
| 139 | + }; |
| 140 | + |
| 141 | + const { result, rerender } = renderHook( |
| 142 | + ({ appId }: { appId: string }) => |
| 143 | + useDynamicApp({ appId, adapter }), |
| 144 | + { initialProps: { appId: 'crm' } }, |
| 145 | + ); |
| 146 | + |
| 147 | + await waitFor(() => { |
| 148 | + expect(result.current.isLoading).toBe(false); |
| 149 | + }); |
| 150 | + |
| 151 | + expect((result.current.config as any).name).toBe('crm'); |
| 152 | + |
| 153 | + rerender({ appId: 'erp' }); |
| 154 | + |
| 155 | + await waitFor(() => { |
| 156 | + expect((result.current.config as any).name).toBe('erp'); |
| 157 | + }); |
| 158 | + |
| 159 | + expect(adapter.getApp).toHaveBeenCalledWith('erp'); |
| 160 | + }); |
| 161 | +}); |
0 commit comments