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

If useAuth is a singleton, how to unit test the component that uses it properly? #13

Open
chenxeed opened this issue Aug 18, 2019 · 1 comment

Comments

@chenxeed
Copy link

Hi,

my question might be specific to the general way of testing Context API in React that acts as a singleton.

I believe it's a great help to showcase how is this useAuth library is going to be tested since it's (seems like) a singleton. Thanks!

@Swizec
Copy link
Owner

Swizec commented Aug 20, 2019

This is a great question @chenxeed. useAuth might look like a singleton, but it sort of isn't.

Every time you render an AuthProvider, we create everything from scratch internally. Auth0 client is instantiated, state object is set up, etc. From that point onward, all children components share that same state and Auth0 client via context.

So if you wanted to test this, my recommendation is to render a new AuthProvider for every test. That ensures you get a clean test every time.

If you want to test that multiple components correctly share that state, then I'm not really sure. I suspect you're then going into the territory of testing React itself.

That said, I should probably add some proper tests to this library :D

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

No branches or pull requests

2 participants