Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
Labels
questionFurther information is requested
@stoplion

Description

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?

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

        AltStyle によって変換されたページ (->オリジナル) /