All files / tests hooked.tsx

100% Statements 10/10
100% Branches 6/6
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28            10x     8x                 10x 10x   10x 9x   8x      
export type Hook<T extends any[], R> = (...args: T) => R;
export type HookProps<T extends [any] | any[]> = T extends []
  ? { hook?: undefined }
  : { hook: T extends [any] ? T | T[0] : T };
 
function toArgs<T extends [any] | any[]>(props: T extends [any] ? T | T[0] : T): T {
  if (Array.isArray(props)) return props;
 
  // TODO: Typescript doesn't understand that T[0] and [any] are the same values
  return [props] as T;
}
 
/**
 * @deprecated in favor of the `@testing-library/react` packages {@link renderHook} function
 *
 * This will be removed in the next major version.
 */
export function hooked<T extends [any] | any[], R>(useHook: Hook<T, R>) {
  return ({ hook }: HookProps<T>) => {
    const result = useHook(...toArgs(hook));
 
    if (typeof result === 'undefined') return '<undefined>';
    if (typeof result === 'object') return <div>{JSON.stringify(result)}</div>;
 
    return result.toString();
  };
}