// MyCode.tsx const MyCode = (props) => { const { useEffect, useRef } = React; const { count, setCount } = useState(0) const elementRef = useRef(null); useEffect(() => { setCount(1); }, [elementRef]); const getFunc() { return { name: 'Mike', } } return ( <React.Fragment> <div className="section" ref={elementRef}> .... </div> </React.Fragment> ); } // MyCode.test.js import * as React from 'react'; import { render, mount, shallow, shallowWrapper } from 'enzyme'; import MyCode from '../MyCode'; describe('TEST START', function() { let wrapper; beforeEach(() => { wrapper = shallow( <MyCode />, ); }); it('TEST1', () => { expect(wrapper.find('.section').length).toEqual(1); }); it('TEST2', () => { // useEffect(() => { // setCount(1); // }, [elementRef]); }); it('TEST3', () => { // const getFunc() { // return { // name: 'Mike', // } // } }); });Tengo algunas preguntas. Es difícil escribir un código de prueba.
Q1. (TEST2) ¿Cómo puedo escribir useEffect en el código de prueba (broma)? Quiero obtener el valor de estado modificado.
Q2. (TEST3) ¿Cómo puedo obtener el valor de retorno de la función getFunc? No puedo hacerlo así.
expect(wrapper.getFunc()).toBe({ name: 'Mike' })ayúdame...