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

[SDK-2106] Memoize auth methods #150

Merged
merged 3 commits into from
Oct 30, 2020
Merged

[SDK-2106] Memoize auth methods #150

merged 3 commits into from
Oct 30, 2020

Conversation

adamjmcgrath
Copy link
Contributor

@adamjmcgrath adamjmcgrath commented Oct 28, 2020

(will base on master after #146 is merged)

Description

Memoizing the auth methods means that downstream components that use them in dependent keys of other hooks wont get unnecessary updates when auth state changes.

*Easier to review if you hide whitespace changes https://github.com/auth0/auth0-react/pull/150/files?w=1

References

Fixes #131

Testing

  • This change adds test coverage for new/changed/fixed functionality

Checklist

  • I have added documentation for new/changed functionality in this PR or in auth0.com/docs
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used, if not master

@adamjmcgrath adamjmcgrath added the review:small Small review label Oct 28, 2020
@adamjmcgrath adamjmcgrath requested a review from a team as a code owner October 28, 2020 13:40
@adamjmcgrath adamjmcgrath marked this pull request as draft October 28, 2020 13:41
Base automatically changed from update-user-after-get-token to master October 28, 2020 15:43
Copy link
Member

@frederikprijck frederikprijck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this, I added a comment but I don't think it is blocking.

const memoized = result.current.getIdTokenClaims;
rerender();
expect(result.current.getIdTokenClaims).toBe(memoized);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even tho this is mostly just testing that we are using useCallback and that it is working as expected, I actually like this test.
Would it make sense to add a test for the other functions as well?

Could probably make it a single test:

[
  { name: 'getIdTokenClaims', cb: (provider) => provider.getIdTokenClaims },
  { name: 'loginWithRedirect', cb: (provider) => provider.loginWithRedirect },
].forEach(({name, cb}) => 
  it(`should memoize the ${name} method`, async () => {
    const wrapper = createWrapper();
    const { waitForNextUpdate, result, rerender } = renderHook(
      () => useContext(Auth0Context),
      { wrapper }
    );
    await waitForNextUpdate();
    const memoized = cb(result.current);
    rerender();
    expect(cb(result.current)).toBe(memoized);
  })
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea - I'd do this but my IDE doesn't like dynamically creating tests - it doesn't let me run them from the gui. (I know that shouldn't stop me from doing it, but I really like running my tests like that 🤷)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review:small Small review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider memoizing Auth0context functions
2 participants