Description
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
- Single definition of each predicate, so avoiding recent
getByTestId
/getAllByTestId
differences. - Easier creation of additional predicates, you just need to create
queryAll
version and all other queries will generated for you bybuildQueries
function. - Reduced code side
Downsides
None found yet.
Considerations
- This change should not change the API
- We should keep the same unit tests, but we might reorganize them by predicate vs by return type to match new queries file structure.
- Queries performance should be the same, because
react-test-renderer
find
usesfindAll
under the hood without any query optimization. - Typescript typings tests should probably stay the same, i.e. repeated use-case statements, and not be generated for code correctness reasons.