Skip to content

Tracking toHaveBeenCalled() for hook's arguments? #885

Description

@stoplion

I'm trying to spy on a mocked function that is passed into a hook as a n argument. The function is analytics.identify() and oktaAuth.token.getUserInfo().

Expected number of calls: >= 1
Received number of calls:    0

It is receiving 0 calls.

export async function usePersonalizeAnalytics({
  analytics,
  oktaAuth,
}: {
  analytics: Analytics | null;
  oktaAuth: OktaAuth | null;
}) {
  useEffect(() => {
    console.log("IN EFFECT CALLLLLLLLLLED");

    async function personalizeAnalytics() {
      if (!analytics || !oktaAuth) return;

      const isAuthenticated = await oktaAuth.isAuthenticated();
      if (!isAuthenticated) return false;

      const userClaims = await oktaAuth.token.getUserInfo();
      console.log(userClaims);
      console.log(isAuthenticated);

      const name = userClaims.name;
      const email = userClaims.email;
      const sub = userClaims.sub;
      debugger;
      analytics?.identify(`${sub}`, {
        name: `${name}`,
        email: `${email}`,
      });
    }

    personalizeAnalytics();
  }, [analytics, oktaAuth]);
}
const analyticsStub = {
    identify: jest.fn(),
};

const oktaAuthStub = {
    isAuthenticated: jest.fn(),
    token: {
      getUserInfo: jest.fn(),
    },
};


test('....', () => {
    const analytics = analyticsStub;
    const oktaAuth = oktaAuthStub;

    oktaAuth.isAuthenticated.mockResolvedValue(true);
    oktaAuth.token.getUserInfo.mockResolvedValue({
          name: "test",
          email: "test@gmail.com",
          sub: "test",
    });

   renderHook(() =>
      useCustomHook({
         analytics,
         oktaAuth
      }),
    );

   expect(oktaAuthStub.token.getUserInfo).toHaveBeenCalled();
})

Is there a specific setup need to track toHaveBeenCalled() for hook's arguments?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions