|
| 1 | +import React from 'react'; |
| 2 | +import { act, renderHook } from '@testing-library/react-hooks'; |
| 3 | +import { TestUtils } from '@deephaven/utils'; |
| 4 | +import { useWidget } from './useWidget'; |
| 5 | +import { ObjectFetchManagerContext } from './useObjectFetch'; |
| 6 | + |
| 7 | +describe('useWidget', () => { |
| 8 | + it('should return a widget when available', async () => { |
| 9 | + const descriptor = { type: 'type', name: 'name' }; |
| 10 | + const widget = { close: jest.fn() }; |
| 11 | + const fetch = jest.fn(async () => widget); |
| 12 | + const objectFetch = { fetch, error: null }; |
| 13 | + const subscribe = jest.fn((subscribeDescriptor, onUpdate) => { |
| 14 | + expect(subscribeDescriptor).toEqual(descriptor); |
| 15 | + onUpdate(objectFetch); |
| 16 | + return jest.fn(); |
| 17 | + }); |
| 18 | + const objectManager = { subscribe }; |
| 19 | + const wrapper = ({ children }) => ( |
| 20 | + <ObjectFetchManagerContext.Provider value={objectManager}> |
| 21 | + {children} |
| 22 | + </ObjectFetchManagerContext.Provider> |
| 23 | + ); |
| 24 | + const { result } = renderHook(() => useWidget(descriptor), { wrapper }); |
| 25 | + await act(TestUtils.flushPromises); |
| 26 | + expect(result.current).toEqual({ widget, error: null }); |
| 27 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return an error when an error occurs', () => { |
| 31 | + const descriptor = { type: 'type', name: 'name' }; |
| 32 | + const error = new Error('Error fetching widget'); |
| 33 | + const objectFetch = { fetch: null, error }; |
| 34 | + const subscribe = jest.fn((subscribeDescriptor, onUpdate) => { |
| 35 | + expect(subscribeDescriptor).toEqual(descriptor); |
| 36 | + onUpdate(objectFetch); |
| 37 | + return jest.fn(); |
| 38 | + }); |
| 39 | + const objectManager = { subscribe }; |
| 40 | + const wrapper = ({ children }) => ( |
| 41 | + <ObjectFetchManagerContext.Provider value={objectManager}> |
| 42 | + {children} |
| 43 | + </ObjectFetchManagerContext.Provider> |
| 44 | + ); |
| 45 | + |
| 46 | + const { result } = renderHook(() => useWidget(descriptor), { wrapper }); |
| 47 | + |
| 48 | + expect(result.current).toEqual({ widget: null, error }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should return null when still loading', () => { |
| 52 | + const descriptor = { type: 'type', name: 'name' }; |
| 53 | + const objectFetch = { fetch: null, error: null }; |
| 54 | + const subscribe = jest.fn((_, onUpdate) => { |
| 55 | + onUpdate(objectFetch); |
| 56 | + return jest.fn(); |
| 57 | + }); |
| 58 | + const objectManager = { subscribe }; |
| 59 | + const wrapper = ({ children }) => ( |
| 60 | + <ObjectFetchManagerContext.Provider value={objectManager}> |
| 61 | + {children} |
| 62 | + </ObjectFetchManagerContext.Provider> |
| 63 | + ); |
| 64 | + const { result } = renderHook(() => useWidget(descriptor), { wrapper }); |
| 65 | + |
| 66 | + expect(result.current).toEqual({ widget: null, error: null }); |
| 67 | + }); |
| 68 | +}); |
0 commit comments