本文介绍了用酶测试`React.createRef` API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试以下类,该类使用 React.createRef api。

I would like to test the following class, which uses the React.createRef api.

尽管如此,快速搜索并没有发现任何这样做的例子。有人成功吗?我将如何嘲笑裁判?

A quick search didn't reveal any any examples of doing this though. Has anyone had success? How would I go about mocking the ref?

理想情况下,我想使用 shallow

Ideally I'd like to use shallow.

class Main extends React.Component<Props, State> {

  constructor(props) {
    super(props);
    this.state = {
      contentY: 0,
    };

    this.domRef = React.createRef();
  }

  componentDidMount() {
    window.addEventListener('scroll', this.handleScroll);
    handleScroll();
  }

  componentWillUnmount() {
   window.removeEventListener('scroll', this.handleScroll);
  }

  handleScroll = () => {
    const el = this.domRef.current;
    const contentY = el.offsetTop;
    this.setState({ contentY });
  };

  render() {
    return (
      <Wrapper innerRef={this.domRef}>
        <MainRender contentY={this.state.contentY} {...this.props} />
      </Wrapper>
    );
  }
}

更新

所以我可以使用回调引用对此进行测试,如下所示

So I can test this using callback refs as follows

 setRef = (ref) => {
   this.domRef = ref;
 }

 handleScroll = () => {
   const el = this.domRef;
   if (el) {
     const contentY = el.offsetTop;
     this.setState({ contentY });
   }
 };

 render() {
   return (
     <Wrapper ref={this.setRef}>
       <MainRender contentY={this.state.contentY} {...this.props} />
     </Wrapper>
   );
 }
}

然后测试类似

it("adds an event listener and sets currentY to offsetTop", () => {
    window.addEventListener = jest.fn();
    const component = shallow(<ScrollLis />)
    const mockRef = { offsetTop: 100 };
    component.instance().setRef(mockRef);
    component.instance().componentDidMount();
    expect(window.addEventListener).toBeCalled();
    component.update();
    const mainRender = component.find(MainRender);
    expect(mainRender.props().contentY).toBe(mockRef.offsetTop);
  });


推荐答案

没有特定的例程来引用被测试。引用只是具有当前键的对象。

There's no specific routine for refs to be tested. A ref is just an object with current key.

如果在早期访问它componentDidMount ,需要禁用生命周期挂钩进行测试。应该测试组件最初是否具有引用,然后可以对其进行模拟

In case it's accessed early in componentDidMount, lifecycle hooks need to be disabled for testing. A component should be tested that it initially has a ref, then it can be mocked

const wrapper = shallow(<Comp/>, { disableLifecycleMethods: true });
expect(wrapper.instance().domRef).toEqual({ current: null });
wrapper.instance().domRef.current = mockRef;
wrapper.instance().componentDidMount();

由于ref作为prop传递给另一个组件,因此可以对其进行测试。正确的引用:

Since a ref is passed to another component as a prop, it can be tested that it was provided with correct ref:

expect(wrapper.find(Wrapper).dive().props().innerRef).toBe(wrapper.instance().domRef);

然后在 Wrapper 测试中可以测试到ref 当前键已分配了正确的对象。

Then in Wrapper test can be tested that ref current key is assigned with correct object.

这篇关于用酶测试`React.createRef` API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:00