selectControl.spec.jsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React from 'react';
  2. import {mountWithTheme, shallow} from 'sentry-test/enzyme';
  3. import SelectControl from 'app/components/forms/selectControl';
  4. describe('SelectControl', function() {
  5. it('renders with react-select "options"', function() {
  6. const 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. const 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. const 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. const mock = jest.fn();
  42. const Foo = <div>Foo</div>;
  43. const Bar = <div>Bar</div>;
  44. const wrapper = mountWithTheme(
  45. <SelectControl
  46. choices={[[{id: 'foo', name: 'Foo'}, Foo], [{id: 'bar', name: 'Bar'}, Bar]]}
  47. name="fieldName"
  48. onChange={mock}
  49. />,
  50. TestStubs.routerContext()
  51. );
  52. expect(wrapper.find('StyledSelect').prop('options')).toEqual([
  53. {value: {id: 'foo', name: 'Foo'}, label: Foo},
  54. {value: {id: 'bar', name: 'Bar'}, label: Bar},
  55. ]);
  56. wrapper.find('input').simulate('focus');
  57. wrapper.find('.Select-control').simulate('mouseDown', {button: 0});
  58. expect(
  59. wrapper
  60. .find('div.Select-option')
  61. .first()
  62. .prop('children')
  63. ).toEqual(Foo);
  64. wrapper
  65. .find('Option')
  66. .first()
  67. .simulate('mouseDown');
  68. expect(mock).toHaveBeenCalledWith({
  69. value: {id: 'foo', name: 'Foo'},
  70. label: Foo,
  71. });
  72. });
  73. });