reactjs – 测试React Select组件

前端之家收集整理的这篇文章主要介绍了reactjs – 测试React Select组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
https://github.com/JedWatson/react-select

我想使用React-Select react组件,但我需要添加测试.

我已经尝试了谷歌发现的几个选项,但似乎没有任何效果.我有下面的代码,但它不会导致更改事件.我已经能够添加一个焦点事件,它增加了’is-focusedsed’类,但’is-open’类仍然缺失.

我用过:https://github.com/JedWatson/react-select/blob/master/test/Select-test.js作为参考

我曾尝试仅在输入字段上使用更改事件,但这也没有帮助.我注意到select有一个onInputChange = {this.change}.

测试

  1. import Home from '../../src/components/home';
  2. import { mount } from 'enzyme'
  3.  
  4. describe('Home',() => {
  5.  
  6. it("renders home",() => {
  7.  
  8. const component = mount(<Home/>);
  9.  
  10. // default class on .Select div
  11. // "Select foobar Select--single is-searchable"
  12.  
  13. const select = component.find('.Select');
  14.  
  15. // After focus event
  16. // "Select foobar Select--single is-searchable is-focussed"
  17. // missing is-open
  18. TestUtils.Simulate.focus(select.find('input'));
  19.  
  20. //this is not working
  21. TestUtils.Simulate.keyDown(select.find('.Select-control'),{ keyCode: 40,key: 'ArrowDown' });
  22. TestUtils.Simulate.keyDown(select.find('.Select-control'),{ keyCode: 13,key: 'Enter' });
  23.  
  24. // as per code below I expect the h2 to have the select value in it eg 'feaure'
  25.  
  26. });
  27. });

被测组件

  1. import React,{ Component } from 'react';
  2. import Select from 'react-select';
  3.  
  4. class Home extends Component {
  5. constructor(props) {
  6. super(props);
  7.  
  8. this.state = {
  9. message: "Please select option"};
  10. this.change = this.change.bind(this);
  11. }
  12.  
  13. change(event) {
  14.  
  15. if(event.value) {
  16. this.setState({message: event.label});
  17. }
  18. }
  19.  
  20. render () {
  21.  
  22. const options = [ {label: 'bug',value: 1},{label: 'feature',value: 2 },{label: 'documents',value: 3},{label: 'discussion',value: 4}];
  23.  
  24. return (
  25. <div className='content xs-full-height'>
  26. <div>
  27. <h2>{this.state.message}</h2>
  28.  
  29. <Select
  30. name="select"
  31. value={this.state.message}
  32. options={options}
  33. onInputChange={this.change}
  34. onChange={this.change}
  35. />
  36.  
  37. </div>
  38. </div>
  39. );
  40. }
  41. }
  42.  
  43. export default Home;

命令行
要运行测试我做:

  1. >> npm run test

并在package.js我有这个脚本:

  1. "test": "mocha --compilers js:babel-core/register -w test/browser.js ./new",

测试设置

和browser.js是:

  1. import 'babel-register';
  2. import jsdom from 'jsdom';
  3.  
  4. const exposedProperties = ['window','navigator','document'];
  5.  
  6. global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
  7. global.window = document.defaultView;
  8. Object.keys(document.defaultView).forEach((property) => {
  9. if (typeof global[property] === 'undefined') {
  10. exposedProperties.push(property);
  11. global[property] = document.defaultView[property];
  12. }
  13. });
  14.  
  15. global.navigator = {
  16. userAgent: 'node.js'
  17. };

我也尝试过使用此处概述的测​​试方法https://github.com/StephenGrider/ReduxSimpleStarter

任何帮助将不胜感激

https://github.com/JedWatson/react-select/issues/1357

Only solution I found was to simulate a selection through key-down
events:

  1. wrapper.find('.Select-control').simulate('keyDown',{ keyCode: 40 });
  2. // you can use 'input' instead of '.Select-control'
  3. wrapper.find('.Select-control').simulate('keyDown',{ keyCode: 13 });
  4. expect(size).to.eql('your first value in the list')

猜你在找的React相关文章