Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda