test: loadable component is added (#2650)

This commit is contained in:
Palash Gupta 2023-05-01 18:42:44 +05:30 committed by GitHub
parent 93220ba6c2
commit a8eec1b7ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,49 @@
import {
render,
screen,
waitForElementToBeRemoved,
} from '@testing-library/react';
import React from 'react';
import Loadable from './index';
// Sample component to be loaded lazily
function SampleComponent(): JSX.Element {
return <div>Sample Component</div>;
}
const loadSampleComponent = (): Promise<{
default: React.ComponentType;
}> =>
new Promise<{ default: React.ComponentType }>((resolve) => {
setTimeout(() => {
resolve({ default: SampleComponent });
}, 500);
});
describe('Loadable', () => {
it('should render the lazily loaded component', async () => {
const LoadableSampleComponent = Loadable(loadSampleComponent);
const { container } = render(
<React.Suspense fallback={<div>Loading...</div>}>
<LoadableSampleComponent />
</React.Suspense>,
);
expect(screen.getByText('Loading...')).toBeInTheDocument();
await waitForElementToBeRemoved(() => screen.queryByText('Loading...'));
expect(container.querySelector('div')).toHaveTextContent('Sample Component');
});
it('should call React.lazy with the provided import path', () => {
const reactLazySpy = jest.spyOn(React, 'lazy');
Loadable(loadSampleComponent);
expect(reactLazySpy).toHaveBeenCalledTimes(1);
expect(reactLazySpy).toHaveBeenCalledWith(expect.any(Function));
reactLazySpy.mockRestore();
});
});