如何修复单元测试:酶装载在document.getElementById上返回null

时间:2019-07-18 02:45:20

标签: reactjs enzyme react-modal

我正在尝试测试门户组件,但是当我尝试安装它时,它说document.getElementById为null

我尝试创建一个div容器以将酶固定在其上,但仍然找不到

ContainerComponent.js

import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import Modal from 'react-bootstrap/Modal';

const modalRoot = document.getElementById('modal-root)

export default class ContainerComponent extends React.Component {

  constructor(props) {
    this.element = document.createElement('div');
  }

  componentDidMount() {
    modalRoot.appendChild(this.element)
  }
}

ContainerComponent.test.js

  describe('ContainerComponent Tests'), function () {
    it('should render' , function () {
        const modalRoot = document.createElement('div');
        modalRoot.setAttribute('id', 'modal-root');
        const body = global.document.querySelector('body');
        body.appendChild(modalRoot);
        const Wrapper = mount(<BasicModal /> <Child /> <BasicModal />
    })  

0 个答案:

没有答案