import { LoadingPageIndicator } from '@theorchard/suite-components';
import { createIdentity, renderInAppContext } from '@theorchard/suite-testing';
import { screen } from '@testing-library/react';
import React, { Suspense } from 'react';
import { ABACUS_PROFILE } from 'src/constants';
import { LazyRouteErrorBoundary } from 'src/components/shared/lazy-route-error-boundary';
// Eager import of the lazy target so it shares this test file's React
// instance. jest.config.json sets `resetModules: true`, which clears the
// module cache between tests; a raw `import()` inside `React.lazy`
// evaluated AFTER that reset would load `react` fresh from disk and
// produce a second React copy, blowing up hooks with "Cannot read
// 'useState' of null". By eager-loading the chunk here and returning
// `Promise.resolve({ default: ... })`, we satisfy `React.lazy`'s
// contract while keeping a single React instance, and still exercise
// the production wrapper triple (LazyRouteErrorBoundary + Suspense +
// LoadingPageIndicator). This does NOT exercise webpack's chunk
// resolution — that is intentionally out of scope for this smoke test.
import TransferProjectsModule from 'src/components/transfer-projects/transfer-projects';

const LazyTransferProjects = React.lazy(() =>
    Promise.resolve({ default: TransferProjectsModule })
);

const identity = createIdentity({ profileType: ABACUS_PROFILE });

describe('TransferProjects (Suspense composition; chunk resolution mocked)', () => {
    it('shows LoadingPageIndicator while the chunk loads, then renders content', async () => {
        renderInAppContext(
            <LazyRouteErrorBoundary>
                <Suspense
                    fallback={<LoadingPageIndicator testId="lazy-fallback" />}
                >
                    <LazyTransferProjects />
                </Suspense>
            </LazyRouteErrorBoundary>,
            { identity }
        );

        // Production fallback renders during lazy chunk load.
        expect(screen.getByTestId('lazy-fallback')).toBeInTheDocument();

        // Lazy chunk resolves and the component mounts.
        expect(
            await screen.findByText('Transfer Projects')
        ).toBeInTheDocument();

        // Fallback gone after resolution.
        expect(screen.queryByTestId('lazy-fallback')).not.toBeInTheDocument();
    });
});
