Reorganize internal queries code by predicate typ
See original GitHub issueDescribe 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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:15 (10 by maintainers)
Top GitHub Comments
@mdjastrzebski should we close this issue now that our PR is merged?
Seems like the transition to by predicate code organisation is mostly done:
makeQueries
) in order to define predicate onceTo finish the transition I think we should do some final smaller tasks:
GetByAPI
,QueryByAPI
andFindByAPI
types and functions (xxxByAPI
) with per query type (ByXxx
) interfacesmakeA11Query
) to use standard query builder(makeQueries
) in order to allow more flexibity with query parameters and also improve code coherence, reduce over-abstraction.src/helpers
tosrc/queries
@thymikee @MattAgn @AugustinLF wdyt?