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

125
Vistas
función de exportación simulada y variable en la biblioteca de pruebas de reacción

Necesito probar si el botón Eliminar está presente. La estructura de los archivos es la siguiente:

//a.js

 import b from './b'; const xyz = props => { const {var1, var2, func1, func2} = b(props); return( <Button buttonType='primary'onClick={func1}>Add</Button> { var2.length > 0 && <Button buttonType='negative' onClick={func2}>Remove</Button> }) } export default xyz;

//b.js

 const b= (props) => { const { data } = props; let [var1, setVar1] = useState(initialState); let [var2, setVar2] = useState([]); const func1 = async () => { //some data } const func2 = async () => { //some data } return { var1, var2, func1, func2} } export default b;

//a.spec.js

 import xyz from './a'; import { render, fireEvent } from '@testing-library/react'; //I am trying to get delete button in this test case but it is failing because I couldn't mock the b file properly. it('should display Delete button', () => { const { getByRole } = render(<xyz sdk={mockSk.app}/>); expect(getByRole('button', { name: 'Delete' })).toBeInTheDocument(); });

No estoy seguro de cómo simular el archivo b para poder tener un valor ficticio en var2 a cambio. cumplirá la condición en a.js para mostrar el botón Eliminar. las cosas que he probado en las suites de prueba son:

 const mockDeleteData = { data: [{ userId: 1, id: 1, title: 'My First Album' }] } let mockSk = { app: { onConfigure: jest.fn(), getParameters: jest.fn().mockReturnValueOnce({}), setReady: jest.fn(), getCurrentState: jest.fn() } };

it('debería mostrar el botón Eliminar', () => {

 1) jest.mock('./b'); //trying to mock b file but not sure how to assign dummy value to var2 //2) jest.spyOn(b, 'b.func1'); // trying random things to see if it is working. 3) b.mockResolvedValue({ // just checking if it returning this but it return undefined data: [ { userId: 1, id: 1, title: 'My First Album' }, { userId: 1, id: 2, title: 'Album: The Sequel' } ] }); 4) b.default.var2 = mockDeleteData; // returns undefined instead of mockdata 5)const b= require('./b'); const handleClick = jest.spyOn(b, 'func1'); handleClick.mockReturnValue(mockDeleteData); const { getByRole } = render(<xyz sdk={mockSk.app} />); expect(getByRole('button', { name: 'Delete' })).toBeInTheDocument(); });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Este patrón suele funcionar para mí.

 import * as bModule from './b' jest.spyOn(bModule, 'b').mockReturnValue( (_props) => ({var1: value1, var2: value2, func1: jest.fn(), func2: jest.fn()} )

Es posible que no necesite incluir var1 , func1 o func2 en su objeto de devolución simulado, si no hace que su código se arroje y no desea probarlos. Tampoco necesita incluir la entrada _props si no la necesita para establecer ningún objeto de retorno.

Normalmente no uso export default , por lo que primero puede comenzar con export const b = (props) => /*...*/ y ver si lo hace funcionar primero.

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