selectControl.spec.jsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React from 'react';
  2. import {mount, shallow} from 'enzyme';
  3. import SelectControl from 'app/components/forms/selectControl';
  4. describe('SelectControl', function() {
  5. it('renders with react-select "options"', function() {
  6. let wrapper = shallow(<SelectControl options={[{value: 'foo', label: 'Foo'}]} />);
  7. expect(wrapper.find('StyledSelect').prop('options')).toEqual([
  8. {value: 'foo', label: 'Foo'},
  9. ]);
  10. });
  11. it('renders with react-select "multi"', function() {
  12. let wrapper = shallow(<SelectControl multiple />);
  13. expect(wrapper.find('StyledSelect').prop('multi')).toEqual(true);
  14. wrapper = shallow(<SelectControl multi />);
  15. expect(wrapper.find('StyledSelect').prop('multi')).toEqual(true);
  16. wrapper = shallow(<SelectControl />);
  17. expect(wrapper.find('StyledSelect').prop('multi')).toBeUndefined();
  18. });
  19. it('renders with select2 flat "choices"', function() {
  20. let wrapper = shallow(<SelectControl choices={['a', 'b', 'c']} name="fieldName" />);
  21. expect(wrapper.find('StyledSelect').prop('options')).toEqual([
  22. {value: 'a', label: 'a'},
  23. {value: 'b', label: 'b'},
  24. {value: 'c', label: 'c'},
  25. ]);
  26. });
  27. it('renders with select2 paired "choices"', function() {
  28. let wrapper = shallow(
  29. <SelectControl
  30. choices={[['a', 'abc'], ['b', 'bcd'], ['c', 'cde']]}
  31. name="fieldName"
  32. />
  33. );
  34. expect(wrapper.find('StyledSelect').prop('options')).toEqual([
  35. {value: 'a', label: 'abc'},
  36. {value: 'b', label: 'bcd'},
  37. {value: 'c', label: 'cde'},
  38. ]);
  39. });
  40. it('renders with complex objects with paired "choices"', function() {
  41. let mock = jest.fn();
  42. let Foo = <div>Foo</div>;
  43. let Bar = <div>Bar</div>;
  44. let wrapper = mount(
  45. <SelectControl
  46. choices={[[{id: 'foo', name: 'Foo'}, Foo], [{id: 'bar', name: 'Bar'}, Bar]]}
  47. name="fieldName"
  48. onChange={mock}
  49. />
  50. );
  51. expect(wrapper.find('StyledSelect').prop('options')).toEqual([
  52. {value: {id: 'foo', name: 'Foo'}, label: Foo},
  53. {value: {id: 'bar', name: 'Bar'}, label: Bar},
  54. ]);
  55. wrapper.find('input').simulate('focus');
  56. wrapper.find('.Select-control').simulate('mouseDown', {button: 0});
  57. expect(
  58. wrapper
  59. .find('div.Select-option')
  60. .first()
  61. .prop('children')
  62. ).toEqual(Foo);
  63. wrapper
  64. .find('Option')
  65. .first()
  66. .simulate('mouseDown');
  67. expect(mock).toHaveBeenCalledWith({
  68. value: {id: 'foo', name: 'Foo'},
  69. label: Foo,
  70. });
  71. });
  72. });