Skip to content

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

rsgowman
Copy link
Member

Similar to withTestCollection.

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).
// more analogous to withTestCollection and eliminates the pattern of
// `withTestDoc(..., docRef => { docRef.set(initialData) ...});` that otherwise is
// quite common.
export function withTestDocAndInitialData(
Copy link
Contributor

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!

Copy link
Member Author

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. :)

@mikelehen mikelehen assigned rsgowman and unassigned mikelehen Apr 11, 2018
@schmidt-sebastian
Copy link
Contributor

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.

Copy link
Contributor

@mikelehen mikelehen left a comment

Choose a reason for hiding this comment

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

LGTM

@rsgowman rsgowman changed the base branch from rsgowman/get_options to master April 13, 2018 13:57
@zijianjoy zijianjoy force-pushed the rsgowman/withTestDoc branch from 8be5d78 to 24efef6 Compare May 24, 2019 22:32
@googlebot
Copy link

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 cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

ℹ️ Googlers: Go here for more info.

@wilhuff wilhuff force-pushed the rsgowman/withTestDoc branch from 24efef6 to 8be5d78 Compare May 25, 2019 01:15
@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

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

Successfully merging this pull request may close these issues.

6 participants