鍍金池/ 問答/網(wǎng)絡安全  HTML/ jest+enzyme取不到節(jié)點?

jest+enzyme取不到節(jié)點?

報錯如下

 Expected value to be (using Object.is):
      1
    Received:
      0

      12 |       </IntlProvider>
      13 |     );
    > 14 |     expect(wrapper.find('.fullscreen-modal').length).toBe(1);
      15 |   });
      16 | });
      

spec.js如下

import React from 'react';
import { shallow } from 'enzyme';
import { IntlProvider } from 'react-intl';
import AllocatingModal from './allocatingModal';

describe('AllocatingModal', () => { // eslint-disable-line no-undef
  it('render', () => { // eslint-disable-line no-undef
    const wrapper = shallow(
      <IntlProvider locale="zh">
        <AllocatingModal />
      </IntlProvider>
    );
    expect(wrapper.find('.fullscreen-modal').length).toBe(1);
  });
});

jsx如下

<Modal
        maskClosable={false}
        title={title}
        width="100%"
        wrapClassName="fullscreen-modal"
        closable={false}
        visible={this.props.visible}
        footer={null}
      >

明明有 為什么取不到呢?

回答
編輯回答
陌璃

首先確保visibletrue,其次Modal是直接渲染在document.body下面的,因此find無法找到對應的節(jié)點。

antd 4.x已經(jīng)用React.creatPortal解決了這個問題,可以直接用find

2017年10月10日 04:34