sbatson5/firestore-jest-mock

Firestore mock for DocumentReference.create()

Opened this issue · 7 comments

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.

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

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!

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!

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?

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)

I have the exact same documentation @superwoou

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