How to mock firebase?

23 Views Asked by At

Maybe some of you guys, knows the proper way to mock @react-native-firebase/firestore? Until now to mock it I added this code in jest-setup.ts:

import {jest} from '@jest/globals';

jest.mock('@react-native-firebase/firestore', () =>
  jest.fn().mockReturnValue({
    collection: jest.fn().mockReturnValue({
      doc: jest.fn().mockReturnValue({
        collection: [101922, 1535, 105778, 105398, 30002],
        update: jest.fn(),
      }),
    }),
  }),
);

Actually it works fine when I'm using firestore like this:

firestore().collection('userCollection').doc(user?.user.uid)

But I added a button that onPress uses FieldValue like this:

const firebase = firestore().collection('userCollection').doc(user?.user.uid);

<Button
  testID="remove_button"
  onPress={() => {
    firebase
      .update({
        collection: firestore.FieldValue.arrayRemove(item.id),
      })
      .then(response =>
        console.log('FIRESTORE_ITEM_WAS_REMOVED', response),
      )
      .catch(error =>
        console.error('FIRESTORE_REMOVE_ITEM_ERROR', error),
      );
  }}
>
    Remove
</Button>

As I'm using firestore.FieldValue.arrayRemove(item.id) and have no its value in mock I'm getting an error

TypeError: Cannot read properties of undefined (reading 'arrayUnion')

So my question is how to properly mock firebase?

0

There are 0 best solutions below