Test useLazyComponents

This commit is contained in:
Mark Kaylor 2022-11-16 13:57:21 +01:00
parent 7db54acd8d
commit 55a420d35d

View File

@ -0,0 +1,50 @@
import { renderHook } from '@testing-library/react-hooks';
import useLazyComponents from '../index';
const mockCustomField = {
name: 'color',
pluginId: 'mycustomfields',
type: 'text',
icon: jest.fn(),
intlLabel: {
id: 'mycustomfields.color.label',
defaultMessage: 'Color',
},
intlDescription: {
id: 'mycustomfields.color.description',
defaultMessage: 'Select any color',
},
components: {
Input: jest.fn().mockResolvedValue({ default: jest.fn() }),
},
};
jest.mock('@strapi/helper-plugin', () => ({
useCustomFields: () => ({
get: jest.fn().mockReturnValue(mockCustomField),
}),
}));
describe('useLazyComponents', () => {
it('lazy loads the components', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useLazyComponents(['plugin::test.test'])
);
expect(result.current).toEqual({ isLazyLoading: true, lazyComponentStore: {} });
await waitForNextUpdate();
expect(JSON.stringify(result.current)).toEqual(
JSON.stringify({
isLazyLoading: false,
lazyComponentStore: { 'plugin::test.test': jest.fn() },
})
);
});
it('handles no components to load', async () => {
const { result } = renderHook(() => useLazyComponents([]));
expect(result.current).toEqual({ isLazyLoading: false, lazyComponentStore: {} });
});
});