Testing a Recoil selector with async Query

Viewed 820

I am using Recoil for state management and have problem testing my selector. It seems mocking a function inside a selector is not possible. My code is similar to the code below:

const currentUserNameQuery = selector({
  key: 'CurrentUserName',
  get: async ({get}) => {
    const response = await myDBQuery({
      userID: get(currentUserIDState),
    });
    return response.name;
  },
});

function CurrentUserInfo() {
  const userName = useRecoilValue(currentUserNameQuery);
  return <div>{userName}</div>;
}

Now, I want to mock the return value of myDBQuery but it seems I cannot do that when using selector. I was wondering if there is any way to mock returned result of a function inside selector.

1 Answers

How about moving the logic from get into a separate file? After that, you can mock this function.

Related