Estoy usando Jest y react-testing-library en la aplicación React. Tengo que probar el siguiente escenario:
function Parent () { const [country, setCountry] = useState(true) return ( <Child setCountry={setCountry} /> ) } function Child () { const handleValueClear = () => { setCountry(false) const data = { id : 'IND', name : 'India' } dispatch(handleCountryDetails(data)) } return ( <div> <button onclick={handleValueClear}>Click me</button> </div> ) }¿Cómo simular y probar useState en broma?
tu componente
import React from 'react' function Parent () { const [country, setCountry] = React.useState(true) return ( <Child setCountry={setCountry} /> ) } function Child () { const handleValueClear = () => { setCountry(false) const data = { id : 'IND', name : 'India' } dispatch(handleCountryDetails(data)) } return ( <div> <button onclick={handleValueClear}>Click me</button> </div> ) }Archivo de broma para padres
test('useState mock', () => { const initialState = true React.useState = jest.fn().mockReturnValue([initialState , {}]) const wrapper = shallow(<Parent />) // In this point your state set and you can test the rest }