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

How to test a custom hook using useWorker? #126

Open
NandoSangenetto opened this issue Sep 12, 2022 · 2 comments
Open

How to test a custom hook using useWorker? #126

NandoSangenetto opened this issue Sep 12, 2022 · 2 comments
Labels
question Further information is requested

Comments

@NandoSangenetto
Copy link

NandoSangenetto commented Sep 12, 2022

I've been playing with useWorker, and it has been working on the browser.

I tried to create a test for a custom hook that is using the useWorker and got many errors.
I've tried to install jsdom-worker, but I keep receiving errors.

I'm going to share the errors:
image
☝️ with jsdom-worker

image

☝️ without `jsdom-worker`

What kind of setup do I need to do to make it work using jest?

@NandoSangenetto NandoSangenetto added the question Further information is requested label Sep 12, 2022
@NandoSangenetto
Copy link
Author

NandoSangenetto commented Sep 12, 2022

More context, this is the hook I'm testing:

const transactionTransformer = (input: InputType) => {
  // expensive stuff happening here
  return transactionList;
};

const useRecentTransactions = (params?: UseRecentTransactionsParams): UseRecentTransactionsType => {
  const [transactions, setTransactions] = useState<Array<RecentTransaction>>([]);
  const { lastMessage, connectionStatus } = useNetwork({ url: params?.url });
  const [transactionWorker] = useWorker(transactionTransformer);

  useEffect(() => {
    const fetchTransactions = async () => {
      if (connectionStatus === 'Open' && lastMessage !== undefined) {
        const transactionList = await transactionWorker(lastMessage);

        setTransactions((prevTransaction) =>
                transactionList.concat(prevTransaction).slice(0, NUMBER_OF_TRANSACTIONS));
      }
    };

    fetchTransactions();
  }, [connectionStatus, lastMessage, setTransactions, transactionWorker]);

  return {
    transactions,
    connectionStatus,
  };
};

And the test file:

 it('should transform blocks and operations into transactions', async () => {
    const { result } = renderHook(() => useRecentTransactions({ url: WEBSOCKET_URL }), {
      wrapper: ({ children }) => <ContainerProvider>{children}</ContainerProvider>,
    });

    console.log(result.current);
});

@NandoSangenetto
Copy link
Author

It seems related to developit/jsdom-worker#16

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

No branches or pull requests

1 participant