Skip to content

More elegant renderHook interface. #160

@PolarbearDK

Description

@PolarbearDK

Describe the feature you'd like:

I think the renderHook interface is a bit clumbersome, especially when working with custom hooks with arguments.

Suggested implementation:

I have created a Typescript wrapper to simplify the unit testing code

// Test utilty for custom hooks with effects.
// Usage:
//       const hook = renderCustomHook(useMyHook, TypedArd1, TypeArg2, TypedArg3...);
//
// assert results from: hook.result
// use hook.rerender(typed arguments) to update hook.
export function renderCustomHook<T extends any[], R>(
  theHook: (...args: T) => R,
  ...args: T
): {
  readonly result: HookResult<R>;
  readonly waitForNextUpdate: () => Promise<void>;
  readonly unmount: () => boolean;
  readonly rerender: (...args: T) => void;
};
export function renderCustomHook(theHook: (...args: any[]) => any, ...args: any[]) {
  const hook = renderHook((param: { props: any[] }) => theHook(...param.props), {
    initialProps: { props: args },
  });
  return { ...hook, rerender: (...args: any[]) => hook.rerender({ props: args }) };
}

Describe alternatives you've considered:

Would it be possible to include something like this in the library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions