简体   繁体   English

用酶反应js单元测试

[英]React js unit testing with enzyme

Hi guys i am new to react can anyone help me to write unit test for the below code .. i want to test if link is redirecting properly.. 嗨,大家好,我是新来的人,谁能帮助我为下面的代码编写单元测试..我想测试链接是否正确重定向。

Here is my code .. 这是我的代码..

import React { Component } from 'react';
import {Link} from 'react-router';
import './App.css';

class Home extends Component {
  render() {
    return (
      <Link to='/college/masters/cs' className="student">  
        <div className="centered">
          <h2 className="Branch">Branch</h2>
        </div>
      </Link>
    );
  }
}

My test 我的测试

import React from 'react';
import { mount, shallow } from 'enzyme';
import {expect} from 'chai';
import 'ignore-styles';
import Home from '../src/Home';

describe('<Home/>', function () {
  it('should have a Link', function () {
    const wrapper = shallow(<Home/>);

    expect(wrapper.find('Link')).to.have.length(1);
  });
});

Please help me to write test if link is redirecting properly.. 如果链接正确重定向,请帮助我编写测试。

Thank you 谢谢

Well, try this out. 好吧,尝试一下。 It should work. 它应该工作。 You merely need to check whether you are passing the valid to property with a relevant value to the Link component. 您只需要检查是否将有效值传递to具有相关值的属性到Link组件。 That's it. 而已。 You don't need to check whether it takes you to the given url, since it is the functionality of the Link component and they should have tests to verify that. 您不需要检查是否将您带到给定的url,因为它是Link组件的功能,他们应该进行测试以验证这一点。 If you need that then what you are writing is not a unit test, it is called an e2e . 如果您需要的是编写的不是单元测试,则称为e2e You may need selenium or so in doing that. 为此,您可能需要硒。

expect(wrapper.find('Link').props().to).to.eql('/college/masters/cs')

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

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