import * as defaultQueries from "./queries/index"; import type { TestInstance } from "./types"; export type BoundFunction = T extends ( container: TestInstance, ...args: infer P ) => infer R ? (...args: P) => R : never; export type BoundFunctions = TQueries extends typeof defaultQueries ? { getByText: ( ...args: Parameters>> ) => ReturnType>; queryByText: ( ...args: Parameters>> ) => ReturnType>; findByText: ( ...args: Parameters>> ) => ReturnType>; } & { [P in keyof TQueries]: BoundFunction; } : { [P in keyof TQueries]: BoundFunction; }; export type Query = ( container: TestInstance, ...args: Array ) => | Error | TestInstance | Array | Promise> | Promise | null; export interface Queries { [T: string]: Query; } /** * @param instance * @param queries object of functions * @param initialValue for reducer * @returns returns object of functions bound to container */ function getQueriesForElement( instance: TestInstance, queries: T = defaultQueries as unknown as T, initialValue = {}, ): BoundFunctions { return Object.keys(queries).reduce((helpers, key) => { const fn = queries[key]; helpers[key] = fn!.bind(null, instance); return helpers; }, initialValue as BoundFunctions); } export { getQueriesForElement };