|
| 1 | +import React from 'react'; |
| 2 | +import { act, render } from '@testing-library/react'; |
| 3 | +import type { dh as DhType } from '@deephaven/jsapi-types'; |
| 4 | +import { TestUtils } from '@deephaven/utils'; |
| 5 | +import DeferredApiBootstrap from './DeferredApiBootstrap'; |
| 6 | +import { DeferredApiContext } from './useDeferredApi'; |
| 7 | + |
| 8 | +it('should call the error callback if no API provider wrapped', () => { |
| 9 | + const onError = jest.fn(); |
| 10 | + render(<DeferredApiBootstrap onError={onError} />); |
| 11 | + expect(onError).toHaveBeenCalled(); |
| 12 | +}); |
| 13 | + |
| 14 | +it('renders children if the API is loaded', () => { |
| 15 | + const api = TestUtils.createMockProxy<DhType>(); |
| 16 | + const { queryByText } = render( |
| 17 | + <DeferredApiContext.Provider value={api}> |
| 18 | + <DeferredApiBootstrap> |
| 19 | + <div>Child</div> |
| 20 | + </DeferredApiBootstrap> |
| 21 | + </DeferredApiContext.Provider> |
| 22 | + ); |
| 23 | + expect(queryByText('Child')).not.toBeNull(); |
| 24 | +}); |
| 25 | + |
| 26 | +it('waits to render children until the API is loaded', async () => { |
| 27 | + let resolveApi: (api: DhType) => void; |
| 28 | + const apiPromise = new Promise<DhType>(resolve => { |
| 29 | + resolveApi = resolve; |
| 30 | + }); |
| 31 | + const deferredApi = jest.fn(() => apiPromise); |
| 32 | + const options = { foo: 'bar' }; |
| 33 | + const { queryByText } = render( |
| 34 | + <DeferredApiContext.Provider value={deferredApi}> |
| 35 | + <DeferredApiBootstrap options={options}> |
| 36 | + <div>Child</div> |
| 37 | + </DeferredApiBootstrap> |
| 38 | + </DeferredApiContext.Provider> |
| 39 | + ); |
| 40 | + expect(queryByText('Child')).toBeNull(); |
| 41 | + expect(deferredApi).toHaveBeenCalledTimes(1); |
| 42 | + expect(deferredApi).toHaveBeenCalledWith(options); |
| 43 | + |
| 44 | + const api = TestUtils.createMockProxy<DhType>(); |
| 45 | + await act(async () => { |
| 46 | + resolveApi(api); |
| 47 | + await apiPromise; |
| 48 | + }); |
| 49 | + expect(queryByText('Child')).not.toBeNull(); |
| 50 | +}); |
0 commit comments