Skip to content

Reorganize internal queries code by predicate typ #325

Closed
@mdjastrzebski

Description

@mdjastrzebski

Describe the Issue

Current queries are organized in files by return type (getByAPI.js, queryByAPI.js, findByAPI.js) with exception for A11yAPI.js which is organized by predicate (byA11yLabel, byA11yHint, etc). Generally getBy and getAllBy queries are used as core query, with queryBy/queryAllBy and findBy/findAllBy as manually written wrappers around getBy/getAllBy quries.

There is a lot of repeated code, especially in duplicate getBy and getAllBy queries as well as manual queryBy/queryAllBy/findBy/... wrappers.

This presents an opportunity to refactor queries so that for each predicate type (ByText, byA11yLabel, etc) there is only function encoding the search algorithm, while all other functions are generated by some generic code.

In Testing Library this is achieved by implementing query predicates as queryAll function first, and then using generic buildQueries function that returnes getBy/getAllBy/etc variants.

Benefits

  1. Single definition of each predicate, so avoiding recent getByTestId/getAllByTestId differences.
  2. Easier creation of additional predicates, you just need to create queryAll version and all other queries will generated for you by buildQueries function.
  3. Reduced code side

Downsides

None found yet.

Considerations

  1. This change should not change the API
  2. We should keep the same unit tests, but we might reorganize them by predicate vs by return type to match new queries file structure.
  3. Queries performance should be the same, because react-test-renderer find uses findAll under the hood without any query optimization.
  4. Typescript typings tests should probably stay the same, i.e. repeated use-case statements, and not be generated for code correctness reasons.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions