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

153
Views
Simular un método Lodash para devolver un valor específico

¿Cómo puedo simular el método get Lodash's para devolver un valor específico?

Tengo la siguiente función en mi código.

Código

 import React, { Fragment } from 'react'; import get from 'lodash/get'; import MyComponent from '../../MyComponent'; const A = () => { // this path is fine, tested working in browser. const getData = () => get(window.getStatus(), 'data.toJS()[\'base-path\'].current[\'frame-name\'].exclusions[\'period\']'); return ( <div> { getData() === 'customValue' ? <Fragment/> : <MyComponent/> } </div> ); }; export default A;

Estoy tratando de probar esto burlándome del valor de retorno para poder obtener customValue correcto.

Pero cuando me burlo de la siguiente manera en mi prueba. no funciona El valor es simplemente indefinido.

Prueba

 import get from 'lodash/get'; jest.mock('lodash/get'); global.getState = jest.fn(() => 'mock state'); describe('Test', () => { const render = (props) => shallow( <A {...props} /> ); it('should fail', () => { get.mockReturnValueOnce('customValue'); const r = render({ someKey: 'someValue', }); expect(r.find('MyComponent')).toBeTruthy(); }); }

Esta prueba termina pasando lo cual es incorrecto. Pasé customValue como el valor simulado.

Esta prueba debería haber generado Fragment (en lugar de MyComponent) debido a esto y falló.

Por favor, aconseje cómo podría lograr esto por favor.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aquí hay una forma de hacerlo:

 // getData.js import get from 'lodash/get'; export const getData = () => get(window.getStatus(), 'data.toJS()[\'base-path\'].current[\'frame-name\'].exclusions[\'period\']');
 import React, { Fragment } from 'react'; import { getData } from './getData'; import MyComponent from '../../MyComponent'; const A = () => { return ( <div> { getData() === 'customValue' ? <Fragment/> : <MyComponent/> } </div> ); }; export default A;

Ahora puede simular la función getData exportada por getData que usa A

Una idea alternativa es window.getStatus() . La razón por la que sugiero extraer y burlarse getData es porque, de todos modos, no es necesario que pertenezca a A .

No te burles de lodash , eso es una tontería .

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!