-
Notifications
You must be signed in to change notification settings - Fork 272
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
Conversation
@thymikee @mdjastrzebski here's what I did so far:
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? |
Looks promising! Nice job |
631e043
to
a4e1213
Compare
@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? :) |
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? |
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 |
There was a problem hiding this 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
Thanks for the review @mdjastrzebski! I'll take care of the requested changes |
export type Queries<QueryArg> = { | ||
getBy: GetByQuery<QueryArg>, | ||
getAllBy: GetAllByQuery<QueryArg>, | ||
queryBy: QueryByQuery<QueryArg>, | ||
findBy: FindByQuery<QueryArg>, | ||
findAllBy: FindAllByQuery<QueryArg>, | ||
}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exciting!
9d25a39
to
0cc15ab
Compare
0cc15ab
to
cc05823
Compare
@mdjastrzebski ping :) |
There was a problem hiding this 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) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding explicit types
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thymikee @mdjastrzebski thanks for the approval! (it was my first open source contribution and it's been a great experience!) |
👍🏼. We can, however, rearrange tests later to comply to the new structure, if needed :) |
Summary
This PR aims to solve this issue.
Tasks:
type (different for get, find, query).
Requirements
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