How do you mock Firebase Firestore methods using Jest?

前端 未结 7 1763
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 17:12

I have a series of functions, each performing various firestore interactions. How do I use Jest to mock these firestore calls? I would like to avoid using a library.

相关标签:
7条回答
  • 2020-12-17 18:15

    Here is how i mocked firebase for jest.

    'use strict'
    
    const collection = jest.fn(() => {
      return {
        doc: jest.fn(() => {
          return {
            collection: collection,
            update: jest.fn(() => Promise.resolve(true)),
            onSnapshot: jest.fn(() => Promise.resolve(true)),
            get: jest.fn(() => Promise.resolve(true))
          }
        }),
        where: jest.fn(() => {
          return {
            get: jest.fn(() => Promise.resolve(true)),
            onSnapshot: jest.fn(() => Promise.resolve(true)),
          }
        })
      }
    });
    
    const Firestore = () => {
      return {
        collection
      }
    }
    
    Firestore.FieldValue = {
      serverTimestamp: jest.fn()
    }
    
    export default class RNFirebase {
    
      static initializeApp = jest.fn();
    
      static auth = jest.fn(() => {
        return {
          createUserAndRetrieveDataWithEmailAndPassword: jest.fn(() => Promise.resolve(true)),
          sendPasswordResetEmail: jest.fn(() => Promise.resolve(true)),
          signInAndRetrieveDataWithEmailAndPassword: jest.fn(() => Promise.resolve(true)),
          fetchSignInMethodsForEmail: jest.fn(() => Promise.resolve(true)),
          signOut: jest.fn(() => Promise.resolve(true)),
          onAuthStateChanged: jest.fn(),
          currentUser: {
            sendEmailVerification: jest.fn(() => Promise.resolve(true))
          }
        }
      });
    
      static firestore = Firestore;
    
      static notifications = jest.fn(() => {
        return {
            onNotification: jest.fn(),
            onNotificationDisplayed: jest.fn(),
            onNotificationOpened: jest.fn()
        }
      });
    
      static messaging = jest.fn(() => {
        return {
            hasPermission: jest.fn(() => Promise.resolve(true)),
            subscribeToTopic: jest.fn(),
            unsubscribeFromTopic: jest.fn(),
            requestPermission: jest.fn(() => Promise.resolve(true)),
            getToken: jest.fn(() => Promise.resolve('RN-Firebase-Token'))
        }
      });
    
      static storage = jest.fn(() => {
        return {
          ref: jest.fn(() => {
            return {
              child: jest.fn(() => {
                return {
                  put: jest.fn(() => Promise.resolve(true))
                }
              })
            }
          })
        }
      })
    
    }
    
    0 讨论(0)
提交回复
热议问题