Skip to content

Commit

Permalink
parametrize Queries
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored and MattAgn committed Jan 5, 2021
1 parent 6f5b08d commit cc05823
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/helpers/byTestId.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const {
queryBy: queryByTestId,
findBy: findByTestId,
findAllBy: findAllByTestId,
}: Queries = makeQueries(queryAllByTestId, getMissingError, getMultipleError);
}: Queries<string | RegExp> = makeQueries(
queryAllByTestId,
getMissingError,
getMultipleError
);

export {
getByTestId,
Expand Down
50 changes: 27 additions & 23 deletions src/helpers/makeQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import waitFor from '../waitFor';
import type { WaitForOptions } from '../waitFor';
import { ErrorWithStack } from './errors';

// TODO: fix typing
// is it always string | RegExp for every query?
// what about options for each query?
type QueryArg = string | RegExp;

type QueryFunction<ArgType, ReturnType> = (
instance: ReactTestInstance
) => (args: ArgType) => ReturnType;
Expand All @@ -16,28 +11,37 @@ type FindQueryFunction<ArgType, ReturnType> = (
instance: ReactTestInstance
) => (args: ArgType, waitForOptions?: WaitForOptions) => Promise<ReturnType>;

type QueryAllByQuery = QueryFunction<QueryArg, Array<ReactTestInstance>>;
type QueryByQuery = QueryFunction<QueryArg, null | ReactTestInstance>;

type GetAllByQuery = QueryFunction<QueryArg, Array<ReactTestInstance>>;
type GetByQuery = QueryFunction<QueryArg, ReactTestInstance>;

type FindAllByQuery = FindQueryFunction<QueryArg, Array<ReactTestInstance>>;
type FindByQuery = FindQueryFunction<QueryArg, ReactTestInstance>;

export type Queries = {
getBy: GetByQuery,
getAllBy: GetAllByQuery,
queryBy: QueryByQuery,
findBy: FindByQuery,
findAllBy: FindAllByQuery,
type QueryAllByQuery<QueryArg> = QueryFunction<
QueryArg,
Array<ReactTestInstance>
>;
type QueryByQuery<QueryArg> = QueryFunction<QueryArg, null | ReactTestInstance>;

type GetAllByQuery<QueryArg> = QueryFunction<
QueryArg,
Array<ReactTestInstance>
>;
type GetByQuery<QueryArg> = QueryFunction<QueryArg, ReactTestInstance>;

type FindAllByQuery<QueryArg> = FindQueryFunction<
QueryArg,
Array<ReactTestInstance>
>;
type FindByQuery<QueryArg> = FindQueryFunction<QueryArg, ReactTestInstance>;

export type Queries<QueryArg> = {
getBy: GetByQuery<QueryArg>,
getAllBy: GetAllByQuery<QueryArg>,
queryBy: QueryByQuery<QueryArg>,
findBy: FindByQuery<QueryArg>,
findAllBy: FindAllByQuery<QueryArg>,
};

export function makeQueries(
queryAllByQuery: QueryAllByQuery,
export function makeQueries<QueryArg>(
queryAllByQuery: QueryAllByQuery<QueryArg>,
getMissingError: (args: QueryArg) => string,
getMultipleError: (args: QueryArg) => string
): Queries {
): Queries<QueryArg> {
function getAllByQuery(instance: ReactTestInstance) {
return function getAllFn(args: QueryArg) {
const results = queryAllByQuery(instance)(args);
Expand Down

0 comments on commit cc05823

Please sign in to comment.