Shaun Saker
Shaun Saker

Reputation: 487

How to mock/stub react-native-firebase?

Does anyone know why this won't work?

My api call:

import firebase from 'react-native-firebase';

export default function getAuth() {
  return new Promise((resolve) => {
    firebase.auth().onAuthStateChanged((user) => {
      resolve(user);
    });
  });
}

My unit test that fails on, "Expected one assertion to be called but received zero assertion calls".

import firebase from 'react-native-firebase';

import getAuth from '../'; // eslint-disable-line

jest.mock('react-native-firebase', () => {
  return {
    auth: () => {
      return {
        onAuthStateChanged: () => {
          return Promise.resolve({
            name: 'Shaun',
          });
        },
      };
    },
  };
});

it('resolves a promise containing the user', async () => {
  expect.assertions(1);
  const response = await getAuth();
  expect(response).toEqual({ name: 'Shaun' });
});

Upvotes: 2

Views: 1575

Answers (1)

Khoa
Khoa

Reputation: 2932

  • You returned a resolved promise => nothing would be called after that.
  • Your onAuthStateChanged is a function with callback

Instead, replace the implement of onAuthStateChanged by this:

onAuthStateChanged: jest.fn(cb => cb({ name: 'Shaun' }));

enter image description here

Upvotes: 3

Related Questions