Estoy usando broma para probar un botón simple en React y sigue fallando. Mi última iteración se queja de renderizar. Soy nuevo en las pruebas y he estado en eso por un tiempo y no puedo resolver esto. ¿Que me estoy perdiendo aqui?
Aquí está mi App.js
function clickMe() { alert("Hello") } function App() { return ( <div className="App"> <button id="btn" onClick={clickMe}>Click Me!!</button> </div> ) } export default App;Aquí está mi App.test.js
import React from 'react' import {render} from 'react-dom' import App from './App' test("Click", () => { const {container} = render(<App />) const button = getByTestId(container, 'btn') fireEvent.click(button) })Puede simular algunos eventos con la biblioteca de enzimas
Para esto, primero instale esto por npm, luego importe eso
import {shallow} from 'enzyme';Después de usar esta estructura para simular un clic en un fondo
Crear un envoltorio
let wrapper = shallow(<App />); beforeEach( ()=>{ wrapper = shallow(<CounterApp />); });Esto crea una simulación de componentes de renderizado y le brinda la capacidad de simular eventos, este método funciona en mi proyecto counterApp
test('Click', ()=>{ wrapper.find('button').at(0).simulate('click'); expect(//The action of your botton).toBe(//the result expected); });No es exactamente lo mismo, pero algo como esto también funcionó.
import { shallow } from 'enzyme' import { configure } from 'enzyme' import Adapter from 'enzyme-adapter-react-16' import App from './App' configure({ adapter: new Adapter() }) it('renders the link inside the output area', () => { const output = shallow(<App />) expect(output.find('div').find('button').length).toEqual(1) })