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

87
Views
Cómo espiar componenteWillMount usando broma y enzima

Estoy tratando de probar si se llamó a componentWillMount y para eso mi prueba es

 test('calls `componentWillMount` before rendering', () => { let fn = jest.fn(SomeComponent.prototype.componentWillMount) mount(<SomeComponent />) expect(fn).toHaveBeenCalled() })

Pero aunque se llama al método componentWillMount, la prueba no pasa. ¿Que me estoy perdiendo aqui?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

No sé si las otras respuestas han ayudado con su pregunta, pero no debería necesitar probar el componente WillMount. React ya debería hacer esa prueba por ti.

Más relevante para su prueba sería probar las funciones o acciones que está poniendo en ese método para su componente.

Si está realizando alguna llamada a la API, ejecutando una función basada en accesorios o cualquier otra cosa, eso es lo que debe probar. Simule la función/acción/código que activa el componentWillMount , y haga afirmaciones y expectativas al respecto.

Ejemplo:

Componente:

 class YourComponent extends Component { componentWillMount() { /*this fetch function is actually what you want to test*/ this.props.fetch('data') } render() { /* whatever your component renders*/ } }

Pruebas:

 test('should call fetch when mounted', () => { let mockFetch = jest.fn() const wrapper = mount(<SomeComponent fetch={mockFetch}/>); expect(wrapper).toBeDefined(); expect(mockFetch).toHaveBeenCalled(); expect(mockFetch.mock.calls[0]).toEqual(['data']) });
over 4 years ago · Santiago Trujillo Report

0

Prueba esto:

 test('calls `componentWillMount` before rendering', () => { const onWillMount = jest.fn(); SomeComponent.prototype.componentWillMount = onWillMount; mount(<SomeComponent />); expect(onWillMount).toBeCalled(); });
over 4 years ago · Santiago Trujillo Report

0

Primero spy el método componentWillMount del componentWillMount , pero también usaría .and.CallThrough() para evitar que se burle de su contenido. Espero que esto ayude:

 it('should check that the componentWillMount method is getting called', () => { spyOn(SomeComponent.prototype, 'componentWillMount').and.callThrough(); const wrapper = mount(<SomeComponent />); expect(wrapper).toBeDefined(); expect(SomeComponent.prototype.componentWillMount).toHaveBeenCalledTimes(1); });
over 4 years ago · Santiago Trujillo 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!