-
Notifications
You must be signed in to change notification settings - Fork 952
Adjust test util withTestDoc() to accept initial data #675
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
base: main
Are you sure you want to change the base?
Conversation
Add option to allow the user to control where DocumentReference.get() and Query.get() fetches from. By default, it fetches from the server (if possible) and falls back to the local cache. It's now possible to alternatively fetch from the local cache only, or to fetch from the server only (though in the server only case, latency compensation is still enabled).
Similar to withTestCollection.
// more analogous to withTestCollection and eliminates the pattern of | ||
// `withTestDoc(..., docRef => { docRef.set(initialData) ...});` that otherwise is | ||
// quite common. | ||
export function withTestDocAndInitialData( |
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.
So TypeScript actually supports a mechanism for method overloads. It's a bit wacky (you basically list out all the overload signatures and then implement a function whose signature is a superset of all the overloads and then dynamically handle the various possible calls at runtime).
So that would look something like:
export type WithTestDocCallback = (doc: firestore.DocumentReference) => Promise<void>;
export function withTestDoc(
persistence: boolean,
fn: WithTestDocCallback
);
export function withTestDoc(
persistence: boolean,
initialData: firestore.DocumentData,
fn: WithTestDocCallback
);
export function withTestDoc(
persistence: boolean,
initialDataOrFn: firestore.DocumentData | WithTestDocCallback,
fn?: WithTestDocCallback
): Promise<void> {
let initialData = null;
if (fn) {
initialData = initialDataOrFn;
} else {
fn = initialDataOrFn as WithTestDocCallback;
}
return withTestDb(persistence, db => {
const docRef: firestore.DocumentReference = db
.collection('test-collection')
.doc();
if (initialData) {
return docRef.set(initialData).then(() => fn(docRef));
} else {
return fn(docRef);
}
});
}
And then all the places you're passing null
can go back to how they were before.
Sorry, this is what I had in my mind when I asked you to do the TODO and would have been a much easier migration. Though I like that you've gone ahead and cleaned up a bunch of tests to leverage initialData. That's a nice improvement. Thanks!
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.
Ah, I see. Done.
It's just as well I initially did it this way though. Otherwise, I would've been tempted to not do the cleanups. :)
Rich, when you change this to Master you will also notice that this now conflicts considerably with the changes that I merged in #667 I would suggest that you pick your changes over mine and fix the remaining lint errors manually. |
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.
LGTM
8be5d78
to
24efef6
Compare
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the ℹ️ Googlers: Go here for more info. |
24efef6
to
8be5d78
Compare
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
Similar to withTestCollection.