selectField.spec.tsx 3.2 KB

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