|
| 1 | +/** |
| 2 | + * MetadataProvider lazy-loading tests |
| 3 | + * |
| 4 | + * Verifies the Phase 1 refactor: only the `app` type is fetched at |
| 5 | + * provider mount; other metadata types are deferred until a consumer |
| 6 | + * actually reads them. Also covers in-flight de-duplication and |
| 7 | + * per-type invalidation/refresh behaviour. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 11 | +import { render, act, waitFor } from '@testing-library/react'; |
| 12 | +import { MetadataProvider, useMetadata, useMetadataItem } from '../context/MetadataProvider'; |
| 13 | + |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | +// Helpers |
| 16 | +// --------------------------------------------------------------------------- |
| 17 | + |
| 18 | +function createMockAdapter() { |
| 19 | + const meta = { |
| 20 | + getItems: vi.fn().mockImplementation((type: string) => { |
| 21 | + // Each type returns a distinguishable single-item payload. |
| 22 | + return Promise.resolve({ items: [{ name: `${type}-1` }] }); |
| 23 | + }), |
| 24 | + getItem: vi.fn().mockImplementation((type: string, name: string) => { |
| 25 | + return Promise.resolve({ item: { name, type } }); |
| 26 | + }), |
| 27 | + }; |
| 28 | + const adapter = { |
| 29 | + getClient: () => ({ meta }), |
| 30 | + } as any; |
| 31 | + return { adapter, meta }; |
| 32 | +} |
| 33 | + |
| 34 | +function flushAll() { |
| 35 | + // Two microtask ticks are needed: one for the `ensureType` promise |
| 36 | + // chain, another for the React state update that bumps the provider's |
| 37 | + // version counter. `waitFor` is used elsewhere for assertion polling; |
| 38 | + // `flushAll` just gets us past the initial mount transition. |
| 39 | + return act(async () => { |
| 40 | + await Promise.resolve(); |
| 41 | + await Promise.resolve(); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +// Component that captures the latest context value into a ref-like object |
| 46 | +// so assertions can run outside of render. |
| 47 | +function Capture({ sink, read }: { sink: { value: any }; read?: (ctx: any) => void }) { |
| 48 | + const ctx = useMetadata(); |
| 49 | + sink.value = ctx; |
| 50 | + if (read) read(ctx); |
| 51 | + return null; |
| 52 | +} |
| 53 | + |
| 54 | +beforeEach(() => { |
| 55 | + if (typeof sessionStorage !== 'undefined') sessionStorage.clear(); |
| 56 | +}); |
| 57 | + |
| 58 | +// --------------------------------------------------------------------------- |
| 59 | +// Tests |
| 60 | +// --------------------------------------------------------------------------- |
| 61 | + |
| 62 | +describe('MetadataProvider — lazy loading (Phase 1)', () => { |
| 63 | + it('fetches only the app list on initial mount', async () => { |
| 64 | + const { adapter, meta } = createMockAdapter(); |
| 65 | + const sink = { value: null as any }; |
| 66 | + |
| 67 | + render( |
| 68 | + <MetadataProvider adapter={adapter}> |
| 69 | + <Capture sink={sink} /> |
| 70 | + </MetadataProvider>, |
| 71 | + ); |
| 72 | + |
| 73 | + await flushAll(); |
| 74 | + await waitFor(() => expect(sink.value.loading).toBe(false)); |
| 75 | + |
| 76 | + // Only `app` should have been fetched at this point. |
| 77 | + const calledTypes = meta.getItems.mock.calls.map((c: any[]) => c[0]); |
| 78 | + expect(calledTypes).toEqual(['app']); |
| 79 | + expect(sink.value.apps).toEqual([{ name: 'app-1' }]); |
| 80 | + |
| 81 | + // Lazy buckets are still empty arrays — no fetch yet. |
| 82 | + expect(meta.getItems).toHaveBeenCalledTimes(1); |
| 83 | + }); |
| 84 | + |
| 85 | + it('lazily fetches other types only when accessed via the legacy getter', async () => { |
| 86 | + const { adapter, meta } = createMockAdapter(); |
| 87 | + const sink = { value: null as any }; |
| 88 | + |
| 89 | + render( |
| 90 | + <MetadataProvider adapter={adapter}> |
| 91 | + <Capture sink={sink} /> |
| 92 | + </MetadataProvider>, |
| 93 | + ); |
| 94 | + await flushAll(); |
| 95 | + await waitFor(() => expect(sink.value.loading).toBe(false)); |
| 96 | + expect(meta.getItems).toHaveBeenCalledTimes(1); |
| 97 | + |
| 98 | + // Read `objects` via the legacy property (auto-triggers ensureType). |
| 99 | + let firstRead: any[] = []; |
| 100 | + await act(async () => { |
| 101 | + firstRead = sink.value.objects; |
| 102 | + }); |
| 103 | + // Fetch is in-flight; the synchronous read returns the empty snapshot. |
| 104 | + expect(firstRead).toEqual([]); |
| 105 | + |
| 106 | + // Wait for the lazy fetch to settle. |
| 107 | + await waitFor(() => |
| 108 | + expect(meta.getItems).toHaveBeenCalledWith('object'), |
| 109 | + ); |
| 110 | + await waitFor(() => expect(sink.value.objects).toEqual([{ name: 'object-1' }])); |
| 111 | + |
| 112 | + // Still only two list fetches in total — `dashboard / report / page` untouched. |
| 113 | + expect(meta.getItems).toHaveBeenCalledTimes(2); |
| 114 | + const types = meta.getItems.mock.calls.map((c: any[]) => c[0]).sort(); |
| 115 | + expect(types).toEqual(['app', 'object']); |
| 116 | + }); |
| 117 | + |
| 118 | + it('deduplicates concurrent ensureType calls into a single network request', async () => { |
| 119 | + const { adapter, meta } = createMockAdapter(); |
| 120 | + const sink = { value: null as any }; |
| 121 | + |
| 122 | + render( |
| 123 | + <MetadataProvider adapter={adapter}> |
| 124 | + <Capture sink={sink} /> |
| 125 | + </MetadataProvider>, |
| 126 | + ); |
| 127 | + await flushAll(); |
| 128 | + await waitFor(() => expect(sink.value.loading).toBe(false)); |
| 129 | + |
| 130 | + await act(async () => { |
| 131 | + const a = sink.value.ensureType('dashboard'); |
| 132 | + const b = sink.value.ensureType('dashboard'); |
| 133 | + const c = sink.value.ensureType('dashboard'); |
| 134 | + await Promise.all([a, b, c]); |
| 135 | + }); |
| 136 | + |
| 137 | + const dashCalls = meta.getItems.mock.calls.filter((c: any[]) => c[0] === 'dashboard'); |
| 138 | + expect(dashCalls.length).toBe(1); |
| 139 | + }); |
| 140 | + |
| 141 | + it('getItem fetches a single record without triggering a list fetch', async () => { |
| 142 | + const { adapter, meta } = createMockAdapter(); |
| 143 | + const sink = { value: null as any }; |
| 144 | + |
| 145 | + render( |
| 146 | + <MetadataProvider adapter={adapter}> |
| 147 | + <Capture sink={sink} /> |
| 148 | + </MetadataProvider>, |
| 149 | + ); |
| 150 | + await flushAll(); |
| 151 | + await waitFor(() => expect(sink.value.loading).toBe(false)); |
| 152 | + |
| 153 | + let item: any; |
| 154 | + await act(async () => { |
| 155 | + item = await sink.value.getItem('dashboard', 'sales-overview'); |
| 156 | + }); |
| 157 | + expect(item).toEqual({ name: 'sales-overview', type: 'dashboard' }); |
| 158 | + |
| 159 | + // No list fetch for `dashboard`. |
| 160 | + const dashCalls = meta.getItems.mock.calls.filter((c: any[]) => c[0] === 'dashboard'); |
| 161 | + expect(dashCalls.length).toBe(0); |
| 162 | + expect(meta.getItem).toHaveBeenCalledWith('dashboard', 'sales-overview'); |
| 163 | + |
| 164 | + // Second call serves from the in-memory cache. |
| 165 | + meta.getItem.mockClear(); |
| 166 | + await act(async () => { |
| 167 | + item = await sink.value.getItem('dashboard', 'sales-overview'); |
| 168 | + }); |
| 169 | + expect(item).toEqual({ name: 'sales-overview', type: 'dashboard' }); |
| 170 | + expect(meta.getItem).not.toHaveBeenCalled(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('invalidate(type, name) drops a single record without dropping the bucket', async () => { |
| 174 | + const { adapter, meta } = createMockAdapter(); |
| 175 | + const sink = { value: null as any }; |
| 176 | + |
| 177 | + render( |
| 178 | + <MetadataProvider adapter={adapter}> |
| 179 | + <Capture sink={sink} /> |
| 180 | + </MetadataProvider>, |
| 181 | + ); |
| 182 | + await flushAll(); |
| 183 | + await waitFor(() => expect(sink.value.loading).toBe(false)); |
| 184 | + |
| 185 | + // Prime the dashboard bucket. |
| 186 | + await act(async () => { |
| 187 | + await sink.value.ensureType('dashboard'); |
| 188 | + }); |
| 189 | + expect(sink.value.dashboards).toEqual([{ name: 'dashboard-1' }]); |
| 190 | + |
| 191 | + // Invalidate the single item — the bucket is preserved but the entry vanishes. |
| 192 | + await act(async () => { |
| 193 | + sink.value.invalidate('dashboard', 'dashboard-1'); |
| 194 | + }); |
| 195 | + expect(sink.value.dashboards).toEqual([]); |
| 196 | + |
| 197 | + // Re-reading via getItem triggers a single-item fetch. |
| 198 | + meta.getItem.mockClear(); |
| 199 | + await act(async () => { |
| 200 | + await sink.value.getItem('dashboard', 'dashboard-1'); |
| 201 | + }); |
| 202 | + expect(meta.getItem).toHaveBeenCalledWith('dashboard', 'dashboard-1'); |
| 203 | + }); |
| 204 | + |
| 205 | + it('refresh(type) re-fetches a single bucket; refresh() refreshes only loaded buckets', async () => { |
| 206 | + const { adapter, meta } = createMockAdapter(); |
| 207 | + const sink = { value: null as any }; |
| 208 | + |
| 209 | + render( |
| 210 | + <MetadataProvider adapter={adapter}> |
| 211 | + <Capture sink={sink} /> |
| 212 | + </MetadataProvider>, |
| 213 | + ); |
| 214 | + await flushAll(); |
| 215 | + await waitFor(() => expect(sink.value.loading).toBe(false)); |
| 216 | + |
| 217 | + // Load `object` only; leave dashboard/report/page idle. |
| 218 | + await act(async () => { |
| 219 | + await sink.value.ensureType('object'); |
| 220 | + }); |
| 221 | + |
| 222 | + meta.getItems.mockClear(); |
| 223 | + await act(async () => { |
| 224 | + await sink.value.refresh(); |
| 225 | + }); |
| 226 | + |
| 227 | + // refresh() should re-fetch `app` and `object` (loaded), not the idle types. |
| 228 | + const refreshed = meta.getItems.mock.calls.map((c: any[]) => c[0]).sort(); |
| 229 | + expect(refreshed).toEqual(['app', 'object']); |
| 230 | + }); |
| 231 | +}); |
| 232 | + |
| 233 | +describe('useMetadataItem hook', () => { |
| 234 | + it('resolves to the fetched item and reports loading transitions', async () => { |
| 235 | + const { adapter, meta } = createMockAdapter(); |
| 236 | + let captured: any = null; |
| 237 | + |
| 238 | + function Probe() { |
| 239 | + const state = useMetadataItem('dashboard', 'sales'); |
| 240 | + captured = state; |
| 241 | + return null; |
| 242 | + } |
| 243 | + |
| 244 | + render( |
| 245 | + <MetadataProvider adapter={adapter}> |
| 246 | + <Probe /> |
| 247 | + </MetadataProvider>, |
| 248 | + ); |
| 249 | + |
| 250 | + expect(captured.loading).toBe(true); |
| 251 | + await waitFor(() => expect(captured.loading).toBe(false)); |
| 252 | + expect(captured.item).toEqual({ name: 'sales', type: 'dashboard' }); |
| 253 | + expect(captured.error).toBeNull(); |
| 254 | + expect(meta.getItem).toHaveBeenCalledWith('dashboard', 'sales'); |
| 255 | + }); |
| 256 | +}); |
0 commit comments