3

I have a basic react component using recoil, like:

import { useRecoilValue } from 'recoil'
import { isAuthenticated } from 'authentication'

export function CreateMenu() {

 const auth = useRecoilValue(isAuthenticated)

 return (
   <div>
     {auth && (<div>hey you're authenticated'</div>)
   </div>
)

isAuthenticated is something like:

import {selector} from 'recoil'

export const isAuthenticated = selector<boolean>({
  key: 'authselector',
  get: ({get}) => {
     return get(authToken)?.isAhtenticated
}
})

Now, I want to mock the above isAuthenticated, so I can test both states of auth, I have tried several methods with jest (to which I'm new, as I'm more used to mocha / sinon approach). But so far I haven't been able to figure out how to do it. Reading through (very skimpy) documentation on recoil mocking.. I could move the useRecoilValue() thing to a separate hook, that I could mock, but that feels wrong to me.

any ideas how to approach this?

TBowmo
  • 75
  • 1
  • 11
  • Show the code of the `authToken` – Lin Du Mar 06 '23 at 02:54
  • authToken origins from an API fetch for a standard jwt token, but I feel that this is not essential to the overall test. I just want to see that my component handles correctly that I am authenticated (which is either a true or a false). This is a very simplified example. I have now reworked, so I have a hook that I can stub instead. – TBowmo Mar 07 '23 at 08:28
  • But I have similar logic elsewhere, where I use several selectors, deriving data from atoms, which fetches from API, where all is combined into a table / data-view. I would like to be able to test without details about the atom implementation. I just need to provide data to the upper rendering layer in my unit test. And as I see it, it's a bit of a hassle to do that with jest. – TBowmo Mar 07 '23 at 08:28

0 Answers0