单元测试React组件参考

时间:2016-10-05 16:22:08

标签: unit-testing reactjs enzyme

现在是一个看起来像这样的组件:

  class Notification extends Component {
      constructor(props, context) {
        super(props,context);
      }


      render() {
        return (<NotificationSystem ref="notificationSystem"/>);
      }
   }

   export default Notification;

我的单元测试是:

    it('should make react notification system referencable', () => {
        const wrapper = mount(renderComponent(Notification));
        expect(wrapper.ref('notificationSystem').length).toEqual(1);
    })

我的renderComponent是:

function renderComponent(ComponentClass, props = {}, state = {}) {
    let store = createStore(reducer, state);
    return (
     <Provider store={store}>
        <ComponentClass { ...props } /> 
     </Provider>     
    );
}

目前正在运行此测试的是:

Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's `render` method, or you have multiple copies of React loaded 

知道如何正确测试组件是否可引用?

0 个答案:

没有答案