sbatson5 / firestore-jest-mock

Jest Helper library for mocking Cloud Firestore
https://www.npmjs.com/package/firestore-jest-mock
178 stars 59 forks source link

Mock Function For collectionGroup Firestore Function #10

Closed DrewSwitzer closed 4 years ago

DrewSwitzer commented 4 years ago

I've been using firestore-jest-mock to mock all of my firestore functions for a project I'm currently working on and it's working great! I've run into one little snag, and that is the collectionGroup Firestore function has not been mocked yet.

What problem does this feature solve?

Include Firestore collectionGroup function as a mocked function.

The collectionGroup function documentation can be found at the following link:

Google API's Firestore

What does the proposed API look like?

There should not be any change to the way mockFirebase is used. If I create a mockFirebase database as seen in the following example:

mockFirebase({
    database: { 
        items: [  
            {id: '1', type: 'item', name: 'This is an item.', owner: 'doge', status: 'much wow'},  
            {id: '2', type: 'item', name: 'This is an item.', owner: 'doge', status: 'so scare'},  
            {id: '3', type: 'item', name: 'This is an item.', owner: 'doge', status: 'many science'},
            {id: '3', type: 'other', name: 'This is not an item.', owner: 'cat', status: 'meow'}   
        ]  
    }  
});

mockFirebase should then mock function calls I am testing. In the following example I would expect the mocked functions to result in the function returning item objects 1, 2, and 3 from the above mockFirebase database example.

const db = firebase.firestore();
const query = db.collectionGroup('items').where('type', '==', 'item);
query.get().then( querySnapshot => {
    return querySnapshot.docs;
}
sbatson5 commented 4 years ago

Glad to hear someone is using the library and finds it useful.

I should have some time next week to add this in. Just a product of us not using the API yet in our of projects

sbatson5 commented 4 years ago

👋 We addressed this in https://github.com/Upstatement/firestore-jest-mock/pull/11 @DrewSwitzer

For clarification, our where function in this testing library doesn't actually filter out your mock database. There are too many variations and edge cases around it in firestore to recreate it here. This library instead returns your whole collection in the form of a querySnapshot and allows you to assert that you called where correctly (i.e. testing your code and not firestore's).

Let me know if this helps!