dimanche 26 avril 2020

Best way to test a react hook with a fetch inside of it?

In my App.jsx class.. I want to test the hook inside of App. What would be the best way to do this?

import React, { useState, useEffect } from 'react';
import Pages from './pages';

function App() {
  const [user, setUser] = useState(null);
  useEffect(() => {
    fetch('https://gitconnected.com/v1/portfolio/treyhuffine')
      .then((res) => res.json())
      .then((updateUser) => {
        setUser(updateUser);
      });
  }, []);
  if (!user) {
    return <div />;
  }

  return <Pages user={user} />;
}

export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

I've looked into https://react-hooks-testing-library.com/usage/basic-hooks

but I'm having trouble integrating that into my test suite.

Aucun commentaire:

Enregistrer un commentaire