Skip to content

refactor: re-organise regular (non A11y) queries by predicate #965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: self code review
  • Loading branch information
mdjastrzebski committed Apr 29, 2022
commit 4c3ed45b255eefc28d032fe98495fe7acb75be81
4 changes: 2 additions & 2 deletions src/queries/displayValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const getMultipleError = (displayValue: TextMatch) =>
const getMissingError = (displayValue: TextMatch) =>
`Unable to find an element with displayValue: ${String(displayValue)}`;

const { getBy, getAllBy, queryBy, findBy, findAllBy } = makeQueries(
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
queryAllByDisplayValue,
getMissingError,
getMultipleError
Expand All @@ -72,7 +72,7 @@ export const bindByDisplayValueQueries = (
getByDisplayValue: getBy(instance),
getAllByDisplayValue: getAllBy(instance),
queryByDisplayValue: queryBy(instance),
queryAllByDisplayValue: queryAllByDisplayValue(instance),
queryAllByDisplayValue: queryAllBy(instance),
findByDisplayValue: findBy(instance),
findAllByDisplayValue: findAllBy(instance),
});
22 changes: 12 additions & 10 deletions src/queries/makeQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ export type FindAllByQuery<Predicate, Options> = (
waitForOptions?: WaitForOptions
) => Promise<ReactTestInstance[]>;

type QueryFactor<Query> = (instance: ReactTestInstance) => Query;

export type Queries<Predicate, Options> = {
getBy: QueryFactor<GetByQuery<Predicate, Options>>;
getAllBy: QueryFactor<GetAllByQuery<Predicate, Options>>;
queryBy: QueryFactor<QueryByQuery<Predicate, Options>>;
findBy: QueryFactor<FindByQuery<Predicate, Options>>;
findAllBy: QueryFactor<FindAllByQuery<Predicate, Options>>;
type UnboundQuery<Query> = (instance: ReactTestInstance) => Query;

export type UnboundQueries<Predicate, Options> = {
getBy: UnboundQuery<GetByQuery<Predicate, Options>>;
getAllBy: UnboundQuery<GetAllByQuery<Predicate, Options>>;
queryBy: UnboundQuery<QueryByQuery<Predicate, Options>>;
queryAllBy: UnboundQuery<QueryAllByQuery<Predicate, Options>>;
findBy: UnboundQuery<FindByQuery<Predicate, Options>>;
findAllBy: UnboundQuery<FindAllByQuery<Predicate, Options>>;
};

// The WaitForOptions has been moved to the second option param of findBy* methods with the adding of TextMatchOptions
Expand Down Expand Up @@ -76,10 +77,10 @@ Example:
};

export function makeQueries<Predicate, Options>(
queryAllByQuery: QueryFactor<QueryAllByQuery<Predicate, Options>>,
queryAllByQuery: UnboundQuery<QueryAllByQuery<Predicate, Options>>,
getMissingError: (predicate: Predicate) => string,
getMultipleError: (predicate: Predicate) => string
): Queries<Predicate, Options> {
): UnboundQueries<Predicate, Options> {
function getAllByQuery(instance: ReactTestInstance) {
return function getAllFn(predicate: Predicate, options?: Options) {
const results = queryAllByQuery(instance)(predicate, options);
Expand Down Expand Up @@ -160,6 +161,7 @@ export function makeQueries<Predicate, Options>(
getBy: getByQuery,
getAllBy: getAllByQuery,
queryBy: queryByQuery,
queryAllBy: queryAllByQuery,
findBy: findByQuery,
findAllBy: findAllByQuery,
};
Expand Down
4 changes: 2 additions & 2 deletions src/queries/placeholderText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getMultipleError = (placeholder: TextMatch) =>
const getMissingError = (placeholder: TextMatch) =>
`Unable to find an element with placeholder: ${String(placeholder)}`;

const { getBy, getAllBy, queryBy, findBy, findAllBy } = makeQueries(
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
queryAllByPlaceholderText,
getMissingError,
getMultipleError
Expand Down Expand Up @@ -79,7 +79,7 @@ export const bindByPlaceholderTextQueries = (
getByPlaceholderText: getBy(instance),
getAllByPlaceholderText: getAllBy(instance),
queryByPlaceholderText: queryBy(instance),
queryAllByPlaceholderText: queryAllByPlaceholderText(instance),
queryAllByPlaceholderText: queryAllBy(instance),
findByPlaceholderText: findBy(instance),
findAllByPlaceholderText: findAllBy(instance),

Expand Down
4 changes: 2 additions & 2 deletions src/queries/testId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const getMultipleError = (testId: TextMatch) =>
const getMissingError = (testId: TextMatch) =>
`Unable to find an element with testID: ${String(testId)}`;

const { getBy, getAllBy, queryBy, findBy, findAllBy } = makeQueries(
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
queryAllByTestId,
getMissingError,
getMultipleError
Expand All @@ -60,7 +60,7 @@ export const bindByTestIdQueries = (
getByTestId: getBy(instance),
getAllByTestId: getAllBy(instance),
queryByTestId: queryBy(instance),
queryAllByTestId: queryAllByTestId(instance),
queryAllByTestId: queryAllBy(instance),
findByTestId: findBy(instance),
findAllByTestId: findAllBy(instance),
});
4 changes: 2 additions & 2 deletions src/queries/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const getMultipleError = (text: TextMatch) =>
const getMissingError = (text: TextMatch) =>
`Unable to find an element with text: ${String(text)}`;

const { getBy, getAllBy, queryBy, findBy, findAllBy } = makeQueries(
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
queryAllByText,
getMissingError,
getMultipleError
Expand All @@ -117,7 +117,7 @@ export const bindByTextQueries = (
getByText: getBy(instance),
getAllByText: getAllBy(instance),
queryByText: queryBy(instance),
queryAllByText: queryAllByText(instance),
queryAllByText: queryAllBy(instance),
findByText: findBy(instance),
findAllByText: findAllBy(instance),
});