mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-13 02:59:05 +08:00
test: loadable component is added (#2650)
This commit is contained in:
parent
93220ba6c2
commit
a8eec1b7ab
49
frontend/src/components/Loadable/Loadable.test.tsx
Normal file
49
frontend/src/components/Loadable/Loadable.test.tsx
Normal 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();
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user