/* eslint-disable react/prop-types */ import React from 'react'; import { render, screen, cleanup, waitFor } from '@testing-library/react'; import { expect } from 'chai'; import { ContentWithFallback } from './content-with-fallback'; describe('ContentWithFallback', function () { afterEach(cleanup); function TestContentWithFallback({ isContentReady, fallbackTimeout, contentAfterFallbackTimeout, }: { isContentReady: boolean; fallbackTimeout?: number; contentAfterFallbackTimeout?: number; }) { return ( ready && (
I am ready!
) } fallback={(notReady) => notReady &&
I am not ready yet!
} isContentReady={isContentReady} fallbackTimeout={fallbackTimeout} contentAfterFallbackTimeout={contentAfterFallbackTimeout} >
); } it('should render content immediately if content is ready on the first render', function () { render( ); expect(screen.getByText('I am ready!')).to.exist; }); it('should render nothing when content is not ready on the first render', function () { const container = document.createElement('div'); render( , { container } ); expect(container).to.be.empty; }); it('should render fallback when the timeout passes', async function () { render( ); await waitFor(() => screen.getByText('I am not ready yet!')); }); it('should render content with animation if rendered after fallback', async function () { const { rerender } = render( ); await waitFor(() => screen.getByText('I am not ready yet!')); rerender( ); await waitFor(() => screen.getByText('I am ready!')); expect(screen.getByText('I am ready!')).to.have.attribute( 'data-animated', 'true' ); }); });