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

Firestore mock for DocumentReference.create() #135

Open
fabien-renaud opened this issue Feb 15, 2022 · 7 comments
Open

Firestore mock for DocumentReference.create() #135

fabien-renaud opened this issue Feb 15, 2022 · 7 comments

Comments

@fabien-renaud
Copy link

Summary

It seems DocumentReference.create() is not implemented yet.

TypeError: this.db.collection(...).doc(...).create is not a function

Basic example

I would like to test create() function as I actually do with set() function.

Actual

const { mockSet } = require('firestore-jest-mock/mocks/firestore');

test('testing stuff', () => {
  const firebase = require('firebase'); // or import firebase from 'firebase';
  const db = firebase.firestore();

  const data = {property1: 'value1', property2: 'value2'};

  return db
    .collection('users')
    .doc('id')
    .set(data)
    .then(doc => {
      expect(mockSet).toHaveBeenCalledWith(data);
      // Write other assertions here
    });
});

Expected

const { mockCreate } = require('firestore-jest-mock/mocks/firestore');

test('testing stuff', () => {
  const firebase = require('firebase'); // or import firebase from 'firebase';
  const db = firebase.firestore();

  const data = {property1: 'value1', property2: 'value2'};

  return db
    .collection('users')
    .doc('id')
    .create(data)
    .then(doc => {
      expect(mockCreate).toHaveBeenCalledWith(data);
      // Write other assertions here
    });
});

Motivation

DocumentReference.create() allow us to create a new document only if the documentReference does not exist yet. I actually use it in my application, but sadly can't manage to test it.

@fabien-renaud
Copy link
Author

Now that I think of it, it would be great do the same for WriteBatch.create() (batchCreate)

@sbatson5
Copy link
Owner

I agree! This would be a great addition. I can try to set aside some time this week to add it, unless you want to take a stab at the PR!

@fabien-renaud
Copy link
Author

Hi @sbatson5, did you have time to take a look? I put this issue aside at work, but I believe this feature would be helpful!

@sbatson5
Copy link
Owner

sbatson5 commented May 6, 2022

Hi @sbatson5, did you have time to take a look? I put this issue aside at work, but I believe this feature would be helpful!

I was going to work on this today, but I'm actually having trouble finding doc().create in the firestore documentation. Every example I see about creating a document has set as the method used. I just want to make sure we recreate it in the correct way, but I've personally never used create. Do you have a link to documentation of this function being used?

@superwoou
Copy link

Creates a document referred to by this DocumentReference with the

There is the documentation for create on google cloud firestore, not on firebase firestore.
This is only for firebase-admin. (Not for client)

@fabien-renaud
Copy link
Author

I have the exact same documentation @superwoou

@firemuzzy
Copy link

as of now it is in the google firestore must have been added to match
https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/documentreference

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

No branches or pull requests

4 participants