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

147
Views
Cómo pasar la función a un enlace personalizado mientras se escribe una prueba unitaria en Jest/React

Tengo que escribir una prueba unitaria de un gancho personalizado usado en index.js ( ver esta implementación ) en el que estoy pasando una función como parámetro. No sé cómo pasar una función a un gancho en la prueba unitaria. Soy un principiante.

¿Cómo puedo lograr una cobertura del 100% para mi prueba unitaria?

useBrowserFwdBackButton.ts (archivo en el que está disponible el enlace personalizado)

 import React from 'react'; const useBrowserFwdBackButton = (fn: any) => { const cb = React.useRef(fn); // init with fn, so that type checkers won't assume that current might be undefined React.useEffect(() => { cb.current = fn; }, [fn]); React.useEffect(() => { const onForwardBackButtonEvent = (...args: any[]) => cb.current?.(...args); window.addEventListener('popstate', onForwardBackButtonEvent); return () => window.removeEventListener('popstate', onForwardBackButtonEvent); }, []); }; export default useBrowserFwdBackButton;

index.js (desde donde está llamando el gancho desde el componente funcional)

 useBrowserFwdBackButton((e) => { e.preventDefault(); if (attachmentListLength !== 0) { dispatch(deleteGoogleDocument(attachmentList)); } });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Suponiendo que está utilizando jest y @testing-library/react-hooks para la prueba de ganchos

puede crear una función simulada con jest.fn() por ejemplo.

 const mockFunction = jest.fn(); const { result } = renderHook(() => useBrowserFwdBackButton(mockFunction)); const { .... } = result.current; // since it's attached to windows event, you need to trigger the "popstate" event // expects here expect(mockFunction).toBeCalledTimes(1);

información adicional (evento de ventana de activación) Cómo activar un evento de ventana durante la prueba unitaria usando vue-test-utils

about 4 years ago · Juan Pablo Isaza Report
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!