selectField.spec.jsx 3.5 KB

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