Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

409
Vistas
Pass ref as context in react-testing-library with jest

I am trying my hand at unit testing and here is where I am stuck. The refs for inputs are saved in a global context and am trying to mock the context so I can test the components properly.

Issue is once of my components calls the focus() property from the ref and I am not sure how to mock the ref properly so even my test can use the focus() property.

This is my testing code:

import { render as rtlRender, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import { Provider } from "react-redux";
import store from "../redux/store";
import { GlobalContextProvider } from "../common/GlobalContext";
import { SalesContextProvider } from "../Sales/SalesContext";

import PatientAutocomplete from "../Sales/PatientAutocomplete";

const dummySalesContext = {
  patientRef: null,
};


const dummyGlobalContext = {
  currentComponent: "demo",
  setEnableGlobalNavigation: jest.fn(),
};

const render = (component) =>
  rtlRender(<Provider store={store()}>{component}</Provider>);

test("renders learn react link", () => {
  render(
    <GlobalContextProvider value={dummyGlobalContext}>
      <SalesContextProvider value={dummySalesContext}>
        <PatientAutocomplete
          searchTerm="Demo"
          patientSelected={null}
          close={null}
        />
      </SalesContextProvider>
    </GlobalContextProvider>
  );
  const linkElement = screen.getByText(/Add patient/i);
  expect(linkElement).toBeInTheDocument();
});

This is the error that is thrown: TypeError: Cannot read property 'focus' of undefined

 }, [searchTerm, debounceFetchPatients]);
        useEffect(() => {
           if (!showEditPatient) patientRef.current.focus({ preventScroll: true });
                                                     ^
        }, [showEditPatient]);
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda