compactSelect.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import CompactSelect from 'sentry/components/forms/compactSelect';
  3. describe('CompactSelect', function () {
  4. it('renders', function () {
  5. const {container} = render(
  6. <CompactSelect
  7. options={[
  8. {value: 'opt_one', label: 'Option One'},
  9. {value: 'opt_two', label: 'Option Two'},
  10. ]}
  11. />
  12. );
  13. expect(container).toSnapshot();
  14. });
  15. it('renders disabled', function () {
  16. render(
  17. <CompactSelect
  18. isDisabled
  19. options={[
  20. {value: 'opt_one', label: 'Option One'},
  21. {value: 'opt_two', label: 'Option Two'},
  22. ]}
  23. />
  24. );
  25. expect(screen.getByRole('button')).toBeDisabled();
  26. });
  27. it('renders with menu title', function () {
  28. render(
  29. <CompactSelect
  30. menuTitle="Menu title"
  31. options={[
  32. {value: 'opt_one', label: 'Option One'},
  33. {value: 'opt_two', label: 'Option Two'},
  34. ]}
  35. />
  36. );
  37. // click on the trigger button
  38. userEvent.click(screen.getByRole('button'));
  39. expect(screen.getByText('Menu title')).toBeInTheDocument();
  40. });
  41. it('updates trigger label on selection', function () {
  42. const mock = jest.fn();
  43. render(
  44. <CompactSelect
  45. options={[
  46. {value: 'opt_one', label: 'Option One'},
  47. {value: 'opt_two', label: 'Option Two'},
  48. ]}
  49. onChange={mock}
  50. />
  51. );
  52. // click on the trigger button
  53. userEvent.click(screen.getByRole('button'));
  54. // select Option One
  55. userEvent.click(screen.getByText('Option One'));
  56. expect(mock).toHaveBeenCalledWith({value: 'opt_one', label: 'Option One'});
  57. expect(screen.getByRole('button', {name: 'Option One'})).toBeInTheDocument();
  58. });
  59. it('can select multiple options', function () {
  60. const mock = jest.fn();
  61. render(
  62. <CompactSelect
  63. multiple
  64. options={[
  65. {value: 'opt_one', label: 'Option One'},
  66. {value: 'opt_two', label: 'Option Two'},
  67. ]}
  68. onChange={mock}
  69. />
  70. );
  71. // click on the trigger button
  72. userEvent.click(screen.getByRole('button'));
  73. // select Option One & Option Two
  74. userEvent.click(screen.getByText('Option One'));
  75. userEvent.click(screen.getByText('Option Two'));
  76. expect(mock).toHaveBeenCalledWith([
  77. {value: 'opt_one', label: 'Option One'},
  78. {value: 'opt_two', label: 'Option Two'},
  79. ]);
  80. expect(screen.getByRole('button', {name: 'Option One +1'})).toBeInTheDocument();
  81. });
  82. it('displays trigger button with prefix', function () {
  83. render(
  84. <CompactSelect
  85. triggerProps={{prefix: 'Prefix'}}
  86. value="opt_one"
  87. options={[
  88. {value: 'opt_one', label: 'Option One'},
  89. {value: 'opt_two', label: 'Option Two'},
  90. ]}
  91. />
  92. );
  93. expect(screen.getByRole('button', {name: 'Prefix Option One'})).toBeInTheDocument();
  94. });
  95. it('can search', function () {
  96. render(
  97. <CompactSelect
  98. isSearchable
  99. placeholder="Search here…"
  100. options={[
  101. {value: 'opt_one', label: 'Option One'},
  102. {value: 'opt_two', label: 'Option Two'},
  103. ]}
  104. />
  105. );
  106. // click on the trigger button
  107. userEvent.click(screen.getByRole('button'));
  108. // type 'Two' into the search box
  109. userEvent.click(screen.getByText('Search here…'));
  110. userEvent.keyboard('Two');
  111. // only Option Two should be available, Option One should be filtered out
  112. expect(screen.getByText('Option Two')).toBeInTheDocument();
  113. expect(screen.queryByText('Option One')).not.toBeInTheDocument();
  114. });
  115. it('triggers onClose when the menu is closed if provided', function () {
  116. const onCloseMock = jest.fn();
  117. render(
  118. <CompactSelect
  119. isSearchable
  120. onClose={onCloseMock}
  121. placeholder="Search here…"
  122. options={[
  123. {value: 'opt_one', label: 'Option One'},
  124. {value: 'opt_two', label: 'Option Two'},
  125. ]}
  126. />
  127. );
  128. // click on the trigger button
  129. userEvent.click(screen.getByRole('button'));
  130. expect(onCloseMock).not.toHaveBeenCalled();
  131. // close the menu
  132. userEvent.keyboard('{esc}');
  133. expect(onCloseMock).toHaveBeenCalled();
  134. });
  135. });