Forum

Test cases coverage for firebase google sign up function.

0
Topic starter

How can we cover the test case for the firebase sign up  function so that in test cases the state is not blank?

import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
signInWithGooglePopup = () => signInWithPopup(auth, googleProvider);

  logGoogleUser = async () => {
    const { user } =  await this.signInWithGooglePopup();
    
    this.setState({ googleUser: user.providerData[0] });
  };
This topic was modified 8 months ago by sakibmemon
1 Answer
0
Topic starter

You can try the code below to cover the Firebase functions and ensure the state is not blank.

1. Mock the firebase in test file

jest.mock("firebase/auth", () => {
  return {
    getAuth: jest.fn(),
    GoogleAuthProvider: jest.fn(() => ({
      setCustomParameters: jest.fn(),
    })),
    signInWithPopup: jest.fn(),
    signInWithGooglePopup: jest.fn(),
    logGoogleUser: jest.fn(),
  };
});

jest.mock("firebase/app", () => ({ initializeApp: jest.fn() }));

2. And add mock implementation code in test case handler.

when("the user clicks on the Google login button", async () => {
      instance = LogInWrapper.instance() as CustomGoogleLogInButton;
      const loginBtn = LogInWrapper.findWhere(
        (node: any) => node.prop("data-testid") === `loginWithGoogle`
      ).at(0);
      const mockUser = {
        providerData: [
          {
            displayName: "John Doe",
          },
        ],
      };

      (signInWithPopup as jest.Mock).mockImplementation(() =>
        Promise.resolve({ user: mockUser })
      );
      loginBtn.simulate("click");
      await new Promise(setImmediate);
    });