question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reorganize internal queries code by predicate typ

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
MattAgncommented, Jul 20, 2022

@mdjastrzebski should we close this issue now that our PR is merged?

2reactions
mdjastrzebskicommented, May 4, 2022

Seems like the transition to by predicate code organisation is mostly done:

  • common query builder (makeQueries) in order to define predicate once
  • put each query predicate in separate file instead of grouping queries by query verb (get, query, …)
  • tests grouped by predicate instead of query verb

To finish the transition I think we should do some final smaller tasks:

  • Replace GetByAPI, QueryByAPI and FindByAPI types and functions (xxxByAPI) with per query type (ByXxx) interfaces
  • Migrate A11y query builder (makeA11Query) to use standard query builder(makeQueries) in order to allow more flexibity with query parameters and also improve code coherence, reduce over-abstraction.
  • Move query implementations from src/helpers to src/queries

@thymikee @MattAgn @AugustinLF wdyt?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Predicates: Joins, Filters, and Pushability - Sean D. Stuber
The first would be queries where there is confusion between join predicates and filtering predicates. The second type of problem is with the ......
Read more >
Predicate Migration: Optimizing Queries with Expensive ...
It merely provides a framework in which we can treat all predicates uniformly. Note that the global cost of a predicate in a...
Read more >
Reorganize Hash PBGs by Using RTOS - TechDocs - Broadcom Inc.
Use this extended query to select tablespaces for reorganization at the partition ... Type. S RTOSHG2X. in the Add EQF predicate field and...
Read more >
Query optimization techniques in SQL Server: tips and tricks
In this blog post we will show you step by step some tips and tricks for successful Query optimization techniques in SQL Server....
Read more >
Join predicate pushing - Oracle Communities
Hi ,. I have a complex query which joins three tables . Initially it joins two tables A and B using Hash join...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found