mercredi 25 mars 2020

How to test a function passed to another component?

How do I write a unit test to cover the logic in fooFunction?

The logic could be moved out to another file in order to achieve test coverage, but what if I don't want to?

FooComponent.jsx:

import React, { useState } from 'react';
import FooContext from './FooContext';
import BarComponent from './BarComponent';

function FooComponent() {
  const [value, setValue] = useState();

  const fooFunction = inputValue => {
    setValue(inputValue * 2);
  };

  return (
    <div>
      <span>Hello world!</span>
      <BarComponent inputFunc={fooFunc} />
    </div>
  );
}

export default FooComponent;

How do I test it? Is the only option to not declare the function inside the FooComponent scope?

Aucun commentaire:

Enregistrer un commentaire