使用笑话和酶进行单元测试时。发生以下错误
警告:测试中对ForwardRef的更新未包含在act(...)中。
在测试时,应将引起React状态更新的代码包装到act(...):
此处的组件代码:
import React from "react";
import _cloneDeep from "lodash/cloneDeep";
import _isFunction from "lodash/isFunction";
import FullScreenLoader from "../../../Shared/Components/FullScreenLoader";
import CustomerDetail from './CustomerDetail';
import {
loadCustomerDetails
} from '../../../Services/CustomerService';
import {
CUSTOMER_REQUEST,
CUSTOMER_SUCCESS
} from '../../../Utilities/Constants';
const CustomerDetailContainer = React.forwardRef((props, ref) => {
let _isMounted = React.useRef(false);
const [customerDetail, setCustomerDetail] = React.useState({});
const getCustomerDetails = () => {
props.dispatch({ type: CUSTOMER_REQUEST });
loadCustomerDetails(props.match.params.id, props.login.userAWSAttributes['custom:hb_id']).then(res => {
props.dispatch({ type: CUSTOMER_SUCCESS });
let customerDetailResponse = res[0];
if (_isMounted.current) {
setStateOnMount(customerDetailResponse, setCustomerDetail);
}
});
};
/*eslint-disable */
React.useEffect(() => {
_isMounted.current = true;
getCustomerDetails();
}, []);
/*eslint-enable */
const setStateOnMount = (value, callback) => {
if (_isMounted.current) {
_isFunction(callback) && callback(_cloneDeep(value));
}
};
return (
<>
{
Object.keys(customerDetail).length !== 0 ?
<CustomerDetail customerDetail={customerDetail}
/>
: <FullScreenLoader />
}
</>
);
});
export default CustomerDetailContainer;
我尝试关注
import CustomerDetailContainer from '../CustomerDetailContainer';
import mockProps from '../../../../Assets/testData/CustomerDetailTestData';
import CustomerDetail from '../CustomerDetail';
import { mount } from 'enzyme';
jest.mock('../../../../Services/CustomerService', () => ({
...jest.requireActual('../../../../Services/CustomerService'),
loadCustomerDetails: jest.fn(() => {
return Promise.resolve([mockProps.customerDetail]);
})
}));
describe('<CustomerDetailContainer />', () => {
let wrapper;
beforeEach(() => {
wrapper = mount(<CustomerDetailContainer />);
});
afterEach(() => {
wrapper.unmount();
});
it('load customerDetail', () => {
expect(customerDetailContainerWrapper.find(CustomerDetail).exists()).toBeTruthy();
});
});
版本
“反应”:“ 16.10.2”, “酶”:“ 3.10.0”, “ enzyme-adapter-react-16”:“ 1.15.1”, “笑话酶”:“ 7.1.1”
任何帮助表示赞赏。