Skip to content
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

chore: restructure ByTestId queries code by predicate type #608

Merged
merged 9 commits into from
Jan 13, 2021

Conversation

MattAgn
Copy link
Collaborator

@MattAgn MattAgn commented Nov 29, 2020

Summary

This PR aims to solve this issue.

Tasks:

  • group queries by their predicate type (byTestId, byText) instead of grouping them by their return
    type (different for get, find, query).
  • make query builders that use the queryAllBy query as the base to create the getBy, getAllBy, queryBy, findBy and findAllBy queries
  • group the tests by predicate type as well

Requirements

  • The API should not change

Test plan

No tests should be added since the API does not change, they only need to be moved to be grouped by predicate type

@MattAgn MattAgn marked this pull request as draft November 29, 2020 15:51
@MattAgn
Copy link
Collaborator Author

MattAgn commented Nov 29, 2020

@thymikee @mdjastrzebski here's what I did so far:

  • make new query builders based on queryAll
  • use them to generate byTestId queries
  • group related tests

Apart from that, I'm not too happy about the typing of the query builders I implemented (there is an any that I don't know how to replace with flow).

Is this kind of refactor what you had in mind?

@thymikee
Copy link
Member

thymikee commented Dec 9, 2020

Looks promising! Nice job

@thymikee
Copy link
Member

thymikee commented Dec 9, 2020

@MattAgn I've pushed to your fork with updated typings and rebase after the upgrade to latest RN with newer Flow version. Can you take a look? :)

@MattAgn
Copy link
Collaborator Author

MattAgn commented Dec 9, 2020

Thanks @thymikee! It's all good to me, thanks for the help on the typing!

For the error messages, I wonder if it would be a good idea to generify them as well to keep them coherent across the different queries. Like "No instances found with {ATTRIBUTE} = "myAttribute". From what I've seen, the message is already identical (except for the attribute) for most queries right?

@thymikee
Copy link
Member

thymikee commented Dec 9, 2020

I've changed the error messages here to reflect what's in the DOM testing library, as I find the wording nicer. We can copy this 1:1, no problem from our side. We can also change it any time and surely make generic where it makes sense

@thymikee thymikee marked this pull request as ready for review December 9, 2020 21:42
@thymikee thymikee changed the title WIP: Reorganize internal queries code by predicate type chore: restructure ByTestId queries code by predicate type Dec 9, 2020
Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks going into right direction. I'm glad you started working on this improvement 🚀

I would suggest looking into src/helpers/makeQuery.js which has similar functionality of building different query types from queryAll query. That code has a little more functions, as it's also responsible for building query aliases which is not relevant for this PR.

IMO best API would be to have a single makeQueries function taking in queryAllByXxx query (and maybe some more args) and returning object with all query types: {query,get,find}[All]ByXxx. With the current code you will have to repeat the query construction (as in byTestId.js lines 27-55) for every query type. Finally we could export the results with proper names and typing: getBy => getByTestId

src/helpers/makeQueries.js Show resolved Hide resolved
src/helpers/makeQueries.js Outdated Show resolved Hide resolved
@MattAgn
Copy link
Collaborator Author

MattAgn commented Dec 12, 2020

Thanks for the review @mdjastrzebski! I'll take care of the requested changes

Comment on lines +32 to +38
export type Queries<QueryArg> = {
getBy: GetByQuery<QueryArg>,
getAllBy: GetAllByQuery<QueryArg>,
queryBy: QueryByQuery<QueryArg>,
findBy: FindByQuery<QueryArg>,
findAllBy: FindAllByQuery<QueryArg>,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MattAgn I made Queries parametrized with QueryArg, should solve the problem of queries having different args.

Copy link
Collaborator Author

@MattAgn MattAgn Dec 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @thymikee! I tried it as well but couldn't make it work with the spread operator if we were to add options to the queries. Should we ignore the options for now and tackle the issue once the options are done?

Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exciting!

@thymikee
Copy link
Member

@mdjastrzebski ping :)

Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work! This code is well-structured and easy to read. 🥇🥇

I guess that as a follow-up PR(s) we could work reusing makeQueries to generate all other queries.

PS. Really sorry for long waiting time

import { makeQueries } from './makeQueries';
import type { Queries } from './makeQueries';

const getNodeByTestId = (node, testID) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding explicit types

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can address that in a followup if necessary (I suspect Flow infers that, as it's internal fn)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm, flow infers it
image

@thymikee thymikee merged commit d71a7d8 into callstack:master Jan 13, 2021
@MattAgn
Copy link
Collaborator Author

MattAgn commented Jan 15, 2021

@thymikee @mdjastrzebski thanks for the approval! (it was my first open source contribution and it's been a great experience!)
For the following work on makeQueries, would you rather have one PR with all queries or a bunch of PRs with a few queries addressed in each?

@mdjastrzebski
Copy link
Member

@MattAgn Now we have the common part of the code already solved, as well as pattern to follow. IMO I would go with one PR, as it boils down to defining queryAll for each query type and proper exports. Tests and TS typedefs probably don't need to change.

@thymikee wdyt?

@MattAgn MattAgn deleted the refactor/build-queries branch January 15, 2021 08:07
@thymikee
Copy link
Member

IMO I would go with one PR, as it boils down to defining queryAll for each query type and proper exports. Tests and TS typedefs probably don't need to change.

👍🏼. We can, however, rearrange tests later to comply to the new structure, if needed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants