简体   繁体   English

如何使用Enzyme和Jasmine模拟组件中的道具以进行React单元测试?

[英]How to simulate props in an component for React unit test with Enzyme and Jasmine?

var data = {aList: ['1','2']};    
describe('react', function() {
    it('has a class name', function() {
        expect(shallow(<myComp
          aList={data.aList}/>).is('.comp-something')).toBe(true);
      });
    });

I try to pass values into the component, but seems this is wrong way to do it. 我尝试将值传递到组件中,但是看来这是错误的方法。

I am using enzyme , jsdom , jasmine , gulp for implementing unit test. 我使用的enzymejsdomjasminegulp执行单元测试。

Is there some other way to do this properly? 还有其他方法可以正确执行此操作吗? Per my understanding, shallow is enough as myComp is just a static component without any life cycle function involved. 根据我的理解, shallow就足够了,因为myComp只是一个静态组件,不涉及任何生命周期函数。

How about this? 这个怎么样?

describe('react', function() {
  it('has a class name', function() {
    expect(shallow(<myComp
          aList={data.aList}/>).find('.comp-something').length).toBe(1);
  });
});

Found out that, it is an issue with jsdom incompatibility with node v0.12 . 发现这是jsdomnode v0.12不兼容的问题。 So it will not run properly, so that's why enzyme was acting up! 因此它将无法正常运行,这就是为什么酶起作用的原因! as well as is is not a syntax for jasmine. 以及is不是茉莉语法。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM