2

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.

4

1 回答 1

0

如何将逻辑从 get 移到单独的文件中?之后,您可以模拟此功能。

于 2021-12-09T21:21:54.490 回答