import { render, screen, waitFor } from '@testing-library/react'; import { describe, it, expect, vi } from 'vitest'; import { createLazyComponent } from './LazyComponent'; import { Suspense } from 'react'; // Mock component const MockComponent = () =>
Lazy Loaded Component
; describe('LazyComponent', () => { it('creates lazy component factory', () => { const LazyTest = createLazyComponent(() => Promise.resolve({ default: MockComponent }), ); expect(LazyTest).toBeDefined(); expect(typeof LazyTest).toBe('function'); }); it('renders lazy component when loaded', async () => { const LazyTest = createLazyComponent(() => Promise.resolve({ default: MockComponent }), ); render( Loading...}> , ); await waitFor(() => { expect(screen.getByText('Lazy Loaded Component')).toBeInTheDocument(); }); }); it('shows fallback while loading', () => { const LazyTest = createLazyComponent( () => new Promise(() => {}), // Never resolves ); render( Loading...}> , ); expect(screen.getByText('Loading...')).toBeInTheDocument(); }); it('uses custom fallback when provided', () => { const LazyTest = createLazyComponent( () => new Promise(() => {}),
Custom Loading...
, ); render( Default Loading...}> Custom Loading...} /> , ); // The Suspense fallback will show, but the component can also have its own expect(screen.getByText('Default Loading...')).toBeInTheDocument(); }); });