Auth0 Home Blog Docs

How to unit test single page application authentication

Hello,
I want to unit test a react application.
I followed the quickstart guides.
I want to be able to unit test a component that renders an element according to auth.isAuthenticated().
How can i mock the user’s authentication?
i tried to follow this guide but if i understood correctly it does not cover single page applications.

Hey there @orayya!

I’m not experienced with that but as far as I know, that’s the only doc we offer for local testing and development. Let me ask internally and I’ll get back to you with the info that I managed to get!

2 Likes

Hi @konrad.sopala,
did you manage to find some helpful information? :grinning:

Hey there I just pinged our develop support team to see if they have something to share! Let me get back to you in a few hours!

1 Like

You will probably want to mock the Auth class (https://github.com/auth0-samples/auth0-react-samples/blob/master/01-Login/src/Auth/Auth.js) so that isAuthenticated() returns whatever value helps for your test. E.g. if you want to test what happens when the user is not authenticated, create an mock object that simply returns false from that method:

var mockAuth = {
  isAuthenticated: () => false
};

Then provide that instance to your component when testing it.

1 Like

Thank you! Ill try it!

1 Like

Let us know if you have any further questions down the road!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.