@testing-library/react-hooks#RenderHookResult TypeScript Examples
The following examples show how to use
@testing-library/react-hooks#RenderHookResult.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: renderHookWithContextSynchronously.tsx From gatsby-theme-shopify-manager with MIT License | 6 votes |
// this type signature matches renderHook's type signature
export function renderHookWithContextSynchronously<P, R>(
callback: (props: P) => R,
contextOptions?: Partial<ContextOptions>,
renderHookOptions?: RenderHookOptions<P>,
): RenderHookResult<P, R> {
return renderHook(callback, {
...renderHookOptions,
wrapper: wrapWithContext(contextOptions),
});
}
Example #2
Source File: renderHookWithContext.tsx From gatsby-theme-shopify-manager with MIT License | 5 votes |
// this type signature matches renderHook's type signature
export async function renderHookWithContext<P, R>(
callback: (props: P) => R,
contextOptions?: Partial<ContextOptions>,
renderHookOptions?: RenderHookOptions<P>,
): Promise<RenderHookResult<P, R>> {
const hookRender = renderHook(callback, {
...renderHookOptions,
wrapper: wrapWithContext(contextOptions),
});
await hookRender.waitForNextUpdate();
return hookRender;
}
Example #3
Source File: useUnregisterEntityDialogState.test.tsx From backstage with Apache License 2.0 | 4 votes |
describe('useUnregisterEntityDialogState', () => {
const catalogApiMock = {
getLocationByRef: jest.fn(),
getEntities: jest.fn(),
removeLocationById: jest.fn(),
removeEntityByUid: jest.fn(),
};
const catalogApi = catalogApiMock as Partial<CatalogApi> as CatalogApi;
const Wrapper = (props: { children?: React.ReactNode }) => (
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
{props.children}
</TestApiProvider>
);
let entity: Entity;
let resolveLocation: (location: Location | undefined) => void;
let resolveColocatedEntities: (entities: Entity[]) => void;
beforeEach(() => {
jest.resetAllMocks();
const deferredLocation = defer<Location | undefined>();
const deferredColocatedEntities = defer<Entity[]>();
resolveLocation = deferredLocation.resolve;
resolveColocatedEntities = deferredColocatedEntities.resolve;
catalogApiMock.getLocationByRef.mockReturnValue(deferredLocation.promise);
catalogApiMock.getEntities.mockReturnValue(
deferredColocatedEntities.promise.then(items => ({ items })),
);
entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'n',
namespace: 'ns',
annotations: {
[ANNOTATION_ORIGIN_LOCATION]: 'url:https://example.com',
},
},
spec: {},
};
});
it('goes through the happy unregister path', async () => {
let rendered: RenderHookResult<unknown, UseUnregisterEntityDialogState>;
act(() => {
rendered = renderHook(() => useUnregisterEntityDialogState(entity), {
wrapper: Wrapper,
});
});
expect(rendered!.result.current).toEqual({ type: 'loading' });
resolveLocation({ type: 'url', target: 'https://example.com', id: 'x' });
resolveColocatedEntities([entity]);
await act(async () => {
await rendered!.waitForNextUpdate();
});
expect(rendered!.result.current).toEqual({
type: 'unregister',
location: 'url:https://example.com',
colocatedEntities: [{ kind: 'Component', namespace: 'ns', name: 'n' }],
unregisterLocation: expect.any(Function),
deleteEntity: expect.any(Function),
});
});
it('chooses the bootstrap path when necessary', async () => {
entity.metadata.annotations![ANNOTATION_ORIGIN_LOCATION] =
'bootstrap:bootstrap';
let rendered: RenderHookResult<unknown, UseUnregisterEntityDialogState>;
act(() => {
rendered = renderHook(() => useUnregisterEntityDialogState(entity), {
wrapper: Wrapper,
});
});
resolveLocation({ type: 'bootstrap', target: 'bootstrap', id: 'x' });
resolveColocatedEntities([]);
await act(async () => {
await rendered!.waitForNextUpdate();
});
expect(rendered!.result.current).toEqual({
type: 'bootstrap',
location: 'bootstrap:bootstrap',
deleteEntity: expect.any(Function),
});
});
it('chooses only-delete when there was no location annotation', async () => {
delete entity.metadata.annotations![ANNOTATION_ORIGIN_LOCATION];
let rendered: RenderHookResult<unknown, UseUnregisterEntityDialogState>;
act(() => {
rendered = renderHook(() => useUnregisterEntityDialogState(entity), {
wrapper: Wrapper,
});
});
resolveLocation(undefined);
resolveColocatedEntities([]);
await act(async () => {
await rendered!.waitForNextUpdate();
});
expect(rendered!.result.current).toEqual({
type: 'only-delete',
deleteEntity: expect.any(Function),
});
});
it('chooses only-delete when the location could not be found', async () => {
let rendered: RenderHookResult<unknown, UseUnregisterEntityDialogState>;
act(() => {
rendered = renderHook(() => useUnregisterEntityDialogState(entity), {
wrapper: Wrapper,
});
});
resolveLocation(undefined);
resolveColocatedEntities([]);
await act(async () => {
await rendered!.waitForNextUpdate();
});
expect(rendered!.result.current).toEqual({
type: 'only-delete',
deleteEntity: expect.any(Function),
});
});
});