import { renderHook, waitFor } from '@testing-library/react'; import { ResourceBrowserPlugin } from '../types'; import { mockResource } from '../__mocks__/MockModels'; import { useSelectedState } from './useSelectedState'; describe('useSelectedState', () => { it('Should load the selectedState from the plugin', async () => { const selectedStateResponse = {}; const resource = mockResource({ id: '1' }); const mockRenderSelectedResource = jest.fn().mockResolvedValue(selectedStateResponse); const secondMockRenderSelectedResource = jest.fn(); const plugin: Partial = { type: 'dam', renderSelectedResource: mockRenderSelectedResource, }; const { result } = renderHook(() => useSelectedState({ plugin: plugin as ResourceBrowserPlugin, resource })); expect(result.current.isLoading).toBe(true); expect(result.current.error).toBe(null); expect(result.current.data).toEqual(null); await waitFor(() => expect(result.current.isLoading).toBe(false)); expect(mockRenderSelectedResource).toHaveBeenCalledTimes(1); expect(secondMockRenderSelectedResource).not.toHaveBeenCalled(); expect(result.current.isLoading).toBe(false); expect(result.current.data).toBe(selectedStateResponse); }); it('Should not load the resource if no reference provided', async () => { const mockRenderSelectedResource = jest.fn(); const plugin: Partial = { type: 'dam', renderSelectedResource: mockRenderSelectedResource, }; const { result } = renderHook(() => useSelectedState({ plugin: plugin as ResourceBrowserPlugin, resource: null })); expect(result.current.isLoading).toBe(false); expect(result.current.error).toBe(null); expect(result.current.data).toEqual(null); expect(mockRenderSelectedResource).not.toHaveBeenCalled(); }); });