selectField.spec.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {selectByValue} from 'sentry-test/select-new';
  3. import {Form, SelectField} from 'app/components/forms';
  4. describe('SelectField', function () {
  5. it('renders without form context', function () {
  6. const wrapper = mountWithTheme(
  7. <SelectField
  8. options={[
  9. {label: 'a', value: 'a'},
  10. {label: 'b', value: 'b'},
  11. ]}
  12. name="fieldName"
  13. value="a"
  14. />
  15. );
  16. expect(wrapper).toSnapshot();
  17. });
  18. it('renders with flat choices', function () {
  19. const wrapper = mountWithTheme(
  20. <SelectField choices={['a', 'b', 'c']} name="fieldName" />,
  21. {
  22. context: {
  23. form: {
  24. data: {
  25. fieldName: 'fieldValue',
  26. },
  27. errors: {},
  28. },
  29. },
  30. }
  31. );
  32. expect(wrapper).toSnapshot();
  33. });
  34. it('renders with paired choices', function () {
  35. const wrapper = mountWithTheme(
  36. <SelectField
  37. choices={[
  38. ['a', 'abc'],
  39. ['b', 'bcd'],
  40. ['c', 'cde'],
  41. ]}
  42. name="fieldName"
  43. />,
  44. {
  45. context: {
  46. form: {
  47. data: {
  48. fieldName: 'fieldValue',
  49. },
  50. errors: {},
  51. },
  52. },
  53. }
  54. );
  55. expect(wrapper).toSnapshot();
  56. });
  57. it('can change value and submit', function () {
  58. const mock = jest.fn();
  59. const wrapper = mountWithTheme(
  60. <Form onSubmit={mock}>
  61. <SelectField
  62. options={[
  63. {label: 'a', value: 'a'},
  64. {label: 'b', value: 'b'},
  65. ]}
  66. name="fieldName"
  67. />
  68. </Form>
  69. );
  70. selectByValue(wrapper, 'a', {name: 'fieldName'});
  71. wrapper.find('Form').simulate('submit');
  72. expect(mock).toHaveBeenCalledWith(
  73. {fieldName: 'a'},
  74. expect.anything(),
  75. expect.anything()
  76. );
  77. });
  78. it('can set the value to empty string via props with no options', function () {
  79. const mock = jest.fn();
  80. const wrapper = mountWithTheme(
  81. <SelectField
  82. options={[
  83. {label: 'a', value: 'a'},
  84. {label: 'b', value: 'b'},
  85. ]}
  86. name="fieldName"
  87. onChange={mock}
  88. />
  89. );
  90. // Select a value so there is an option selected.
  91. selectByValue(wrapper, 'a', {name: 'fieldName'});
  92. expect(mock).toHaveBeenCalledTimes(1);
  93. expect(mock).toHaveBeenLastCalledWith('a');
  94. // Update props to remove value and options.
  95. wrapper.setProps({value: '', options: []});
  96. wrapper.update();
  97. expect(wrapper.find('SelectPicker').props().value).toEqual('');
  98. // second update.
  99. expect(mock).toHaveBeenCalledTimes(2);
  100. expect(mock).toHaveBeenLastCalledWith('');
  101. });
  102. describe('Multiple', function () {
  103. it('selects multiple values and submits', function () {
  104. const mock = jest.fn();
  105. const wrapper = mountWithTheme(
  106. <Form onSubmit={mock}>
  107. <SelectField
  108. multiple
  109. options={[
  110. {label: 'a', value: 'a'},
  111. {label: 'b', value: 'b'},
  112. ]}
  113. name="fieldName"
  114. />
  115. </Form>
  116. );
  117. selectByValue(wrapper, 'a', {name: 'fieldName'});
  118. wrapper.find('Form').simulate('submit');
  119. expect(mock).toHaveBeenCalledWith(
  120. {fieldName: ['a']},
  121. expect.anything(),
  122. expect.anything()
  123. );
  124. });
  125. });
  126. });