soumak77 / firebase-mock

Firebase mock library for writing unit tests
https://soumak77.github.io/firebase-mock
349 stars 97 forks source link

Question: How to define sub-collections of documents when defining MockFirestore? #103

Closed ilearnio closed 6 years ago

ilearnio commented 6 years ago

Currently we define mocked DB documents like so:

const db = new MockFirestore(null, {
  collectionName: {
    documentId: {
      name: 'foo',
      createdAt: new Date(0),
    },
    ...
  }
})

Is there any way to pass nested sub-collections? So it would look something like:

const db = new MockFirestore(null, {
  collectionName: {
    documentId: {
      name: 'foo',
      createdAt: new Date(0),

      subCollections: { // <-- Need to do something like this
        subCollectionName: {
          documentId: {
            ...
          }
        },
        ...
      }
    }
  }
})
soumak77 commented 6 years ago

@ilearnio your example is not the desired way to initialize documents. You should use the firestore API when needing to initialize any data. This is true for documents and collections. Please read the tutorial for more info or check out some of the unit tests in this library to see how they initialize data.