-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix useDeferredApi export (#1742)
- It wasn't actually being exported - Also add a DeferredApiBootstrap component to gate on loading the API
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
packages/jsapi-bootstrap/src/DeferredApiBootstrap.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
import { act, render } from '@testing-library/react'; | ||
import type { dh as DhType } from '@deephaven/jsapi-types'; | ||
import { TestUtils } from '@deephaven/utils'; | ||
import DeferredApiBootstrap from './DeferredApiBootstrap'; | ||
import { DeferredApiContext } from './useDeferredApi'; | ||
|
||
it('should call the error callback if no API provider wrapped', () => { | ||
const onError = jest.fn(); | ||
render(<DeferredApiBootstrap onError={onError} />); | ||
expect(onError).toHaveBeenCalled(); | ||
}); | ||
|
||
it('renders children if the API is loaded', () => { | ||
const api = TestUtils.createMockProxy<DhType>(); | ||
const { queryByText } = render( | ||
<DeferredApiContext.Provider value={api}> | ||
<DeferredApiBootstrap> | ||
<div>Child</div> | ||
</DeferredApiBootstrap> | ||
</DeferredApiContext.Provider> | ||
); | ||
expect(queryByText('Child')).not.toBeNull(); | ||
}); | ||
|
||
it('waits to render children until the API is loaded', async () => { | ||
let resolveApi: (api: DhType) => void; | ||
const apiPromise = new Promise<DhType>(resolve => { | ||
resolveApi = resolve; | ||
}); | ||
const deferredApi = jest.fn(() => apiPromise); | ||
const options = { foo: 'bar' }; | ||
const { queryByText } = render( | ||
<DeferredApiContext.Provider value={deferredApi}> | ||
<DeferredApiBootstrap options={options}> | ||
<div>Child</div> | ||
</DeferredApiBootstrap> | ||
</DeferredApiContext.Provider> | ||
); | ||
expect(queryByText('Child')).toBeNull(); | ||
expect(deferredApi).toHaveBeenCalledTimes(1); | ||
expect(deferredApi).toHaveBeenCalledWith(options); | ||
|
||
const api = TestUtils.createMockProxy<DhType>(); | ||
await act(async () => { | ||
resolveApi(api); | ||
await apiPromise; | ||
}); | ||
expect(queryByText('Child')).not.toBeNull(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import useDeferredApi from './useDeferredApi'; | ||
import { ApiContext } from './ApiBootstrap'; | ||
|
||
type DeferredApiBootstrapProps = React.PropsWithChildren<{ | ||
onError?: (error: unknown) => void; | ||
/** | ||
* Options to use when fetching the deferred API. | ||
*/ | ||
options?: Record<string, unknown>; | ||
}>; | ||
|
||
/** | ||
* Does not render children until the deferred API is resolved. | ||
*/ | ||
export const DeferredApiBootstrap = React.memo( | ||
({ | ||
children, | ||
onError, | ||
options, | ||
}: DeferredApiBootstrapProps): JSX.Element | null => { | ||
const [api, apiError] = useDeferredApi(options); | ||
if (apiError != null) { | ||
onError?.(apiError); | ||
return null; | ||
} | ||
if (api == null) { | ||
// Still waiting for the API to load | ||
return null; | ||
} | ||
return <ApiContext.Provider value={api}>{children}</ApiContext.Provider>; | ||
} | ||
); | ||
|
||
DeferredApiBootstrap.displayName = 'DeferredApiBootstrap'; | ||
|
||
export default DeferredApiBootstrap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './ApiBootstrap'; | ||
export * from './ClientBootstrap'; | ||
export * from './DeferredApiBootstrap'; | ||
export * from './useApi'; | ||
export * from './useClient'; | ||
export * from './useDeferredApi'; |