// 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',
// }
// }
});
});
I have some questions.
It is difficult to write a test code.
Q1. How can I write useEffect in test code (jest)? I want to get the changed state value.
Q2. How can I get the return value of the getFunc function?
I can’t do it like this.
expect(wrapper.getFunc()).toBe({ name: 'Mike' })
help me…