Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stoplion opened this issue Aug 16, 2022 · 0 comments
Open

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

stoplion opened this issue Aug 16, 2022 · 0 comments
Labels
question Further information is requested

Comments

@stoplion
Copy link

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: "[email protected]",
          sub: "test",
    });

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

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

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

@stoplion stoplion added the question Further information is requested label Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant