import { describe, expect, it, vi } from 'vitest'; import { lazyWithPreload, preloadAll } from './lazyWithPreload'; describe('lazyWithPreload', () => { it('deduplicates preload calls until cleared', async () => { const Component = () => null; const factory = vi.fn(async () => ({ default: Component, })); const LazyComponent = lazyWithPreload(factory); const [first, second] = await Promise.all([ LazyComponent.preload(), LazyComponent.preload(), ]); expect(first.default).toBe(Component); expect(second.default).toBe(Component); expect(factory).toHaveBeenCalledTimes(1); expect(LazyComponent.read()?.default).toBe(Component); LazyComponent.clearPreload(); await LazyComponent.preload(); expect(factory).toHaveBeenCalledTimes(2); }); it('supports batch preloading and settled failure collection', async () => { const ok = { preload: vi.fn(async () => 'ok'), }; const bad = { preload: vi.fn(async () => { throw new Error('nope'); }), }; const result = await preloadAll([ok, bad], { settle: true }); expect(ok.preload).toHaveBeenCalledTimes(1); expect(bad.preload).toHaveBeenCalledTimes(1); expect(result).toHaveLength(2); expect((result[0] as PromiseSettledResult).status).toBe('fulfilled'); expect((result[1] as PromiseSettledResult).status).toBe('rejected'); }); });