Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
Jest: Function Unit Test in React Functional Component

I want to test the function inside React Functional Component (not Class Component)

Here is my component:

const Component: React.FC = () => {
  const [openMenu, setOpenMenu] = useState<boolean>(false);

  const toggleMenu = () => setOpenMenu((prev) => !prev);

  return (
    <div>
      <ChildComponent openMenu={openMenu} toggleMenu={toggleMenu} />
    </div>
  );
};

export default Component;

I tried to run simple test and it's failed

  test('should be able to toggle open menu state', async () => {
    const wrapper = shallow(
      <Provider store={store}>
        <MemoryRouter initialEntries={['/']}>
          <DefaultLayout />
        </MemoryRouter>
      </Provider>
    );

    // wrapper.invoke('toggleMenu')();
    expect(wrapper.state('openMenu')).toBe(false);
  });

What I want to test is toggleMenu function update the openMenu correctly. I saw some answer from stackoverflow able to test with instance() or invoke() but that only apply for class component. Is that anyway to test function and state inside component without fire any event?

  • Unit test React functional component's function
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!