React-dnd: 使用 Enzyme 而不是 React TestUtils 进行测试

创建于 2017-11-26  ·  3评论  ·  资料来源: react-dnd/react-dnd

我正在编写一些测试,遵循此文档以及此问题https://github.com/react-dnd/react-dnd/issues/453

我的问题是关于测试渲染。

我可以得到文档给出的示例,将组件包装在 DragDropContext 中并使用TestUtils.renderIntoDocument渲染。

这将返回一个DragDropContextContainer对象。

但是,如果我尝试使用 Enyzme mount方法来呈现对象,它会返回一个$$typeof: Symbol(react.element)对象 - 这不会让我得到经理。

我更喜欢使用酶 - 因为这就是我在其他地方进行测试时使用的。

有人对此有任何帮助吗?

wontfix

最有用的评论

遇到了同样的问题。 我可以,并且希望您能够使用wrapper.instance()方法修复它。 此处还描述了https://github.com/react-dnd/react-dnd/issues/488。

例如

const Context = wrapInTestContext(Component)
const wrapper = mount(<Context />)
const manager = wrapper.instance().getManager()
const backend = manager.getBackend()

此外,我还需要获取子组件的实例来调用getHandlerId()

如果有帮助,这是我的测试文件之一: https :

所有3条评论

我刚刚开始工作,这是它的样子。 在打字稿中。

import { ThingIAmTesting } from '../file/where/it/lives';
describe('It is a test!', () => {
  let props;
  let component;
  const getComponent = () => {
    let OriginalComponent = (ThingIAmTesting as any).DecoratedComponent;
    let identity = el => el;
    if (!component) {
      component = Enzyme.shallow(
        <OriginalComponent {...props} connectDragSource={identity} />
       );
      return component
    }

    beforeEach (() => {
      props = {
         prop: 'stuff',
         }
      component = undefined;
     })
    it('should render', () => {
      let component = getComponent();
      expect(component).toMatchSnapshot();
    });
})

遇到了同样的问题。 我可以,并且希望您能够使用wrapper.instance()方法修复它。 此处还描述了https://github.com/react-dnd/react-dnd/issues/488。

例如

const Context = wrapInTestContext(Component)
const wrapper = mount(<Context />)
const manager = wrapper.instance().getManager()
const backend = manager.getBackend()

此外,我还需要获取子组件的实例来调用getHandlerId()

如果有帮助,这是我的测试文件之一: https :

此问题已自动标记为陈旧,因为它最近没有活动。 如果没有进一步的活动发生,它将被关闭。 感谢你的贡献。

此页面是否有帮助?
0 / 5 - 0 等级