index.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {Fragment} from 'react';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import {ComboBox} from 'sentry/components/comboBox';
  4. describe('ComboBox', function () {
  5. it('renders', async function () {
  6. render(
  7. <ComboBox
  8. aria-label="Test Input"
  9. options={[
  10. {value: 'opt_one', label: 'Option One'},
  11. {value: 'opt_two', label: 'Option Two'},
  12. ]}
  13. />
  14. );
  15. expect(await screen.findByRole('combobox', {name: 'Test Input'})).toBeEnabled();
  16. });
  17. it('renders disabled', async function () {
  18. render(
  19. <ComboBox
  20. disabled
  21. aria-label="Test Input"
  22. options={[
  23. {value: 'opt_one', label: 'Option One'},
  24. {value: 'opt_two', label: 'Option Two'},
  25. ]}
  26. />
  27. );
  28. expect(await screen.findByRole('combobox', {name: 'Test Input'})).toBeDisabled();
  29. });
  30. it('can be dismissed', async function () {
  31. render(
  32. <Fragment>
  33. <ComboBox
  34. value="opt_one"
  35. aria-label="Input One"
  36. options={[
  37. {value: 'opt_one', label: 'Option One'},
  38. {value: 'opt_two', label: 'Option Two'},
  39. ]}
  40. />
  41. <ComboBox
  42. value="opt_three"
  43. aria-label="Input Two"
  44. options={[
  45. {value: 'opt_three', label: 'Option Three'},
  46. {value: 'opt_four', label: 'Option Four'},
  47. ]}
  48. />
  49. </Fragment>
  50. );
  51. const input1 = screen.getByRole('combobox', {name: 'Input One'});
  52. // const input2 = screen.getByRole('combobox', {name: 'Input Two'});
  53. // Can be dismissed by clicking outside
  54. await userEvent.click(input1);
  55. expect(screen.getByRole('option', {name: 'Option One'})).toBeInTheDocument();
  56. await userEvent.click(document.body);
  57. await waitFor(() => {
  58. expect(screen.queryByRole('option', {name: 'Option One'})).not.toBeInTheDocument();
  59. });
  60. // Can be dismissed by pressing Escape
  61. await userEvent.click(input1);
  62. await waitFor(() => {
  63. expect(screen.getByRole('option', {name: 'Option One'})).toBeInTheDocument();
  64. });
  65. await userEvent.keyboard('{Escape}');
  66. await waitFor(() => {
  67. expect(screen.queryByRole('option', {name: 'Option One'})).not.toBeInTheDocument();
  68. });
  69. });
  70. it('can search', async function () {
  71. render(
  72. <ComboBox
  73. aria-label="Test Input"
  74. options={[
  75. {value: 'opt_one', label: 'Option One'},
  76. {value: 'opt_two', label: 'Option Two'},
  77. ]}
  78. />
  79. );
  80. // click on the trigger button
  81. await userEvent.click(screen.getByRole('combobox'));
  82. // type 'Two' into the search box
  83. await userEvent.keyboard('Two');
  84. // only Option Two should be available, Option One should be filtered out
  85. expect(screen.getByRole('option', {name: 'Option Two'})).toBeInTheDocument();
  86. expect(screen.queryByRole('option', {name: 'Option One'})).not.toBeInTheDocument();
  87. });
  88. });
  89. describe('ListBox', function () {
  90. it('can select options with values containing quotes', async function () {
  91. const mock = jest.fn();
  92. render(
  93. <ComboBox
  94. aria-label="Test Input"
  95. options={[
  96. {value: '"opt_one"', label: 'Option One'},
  97. {value: '"opt_two"', label: 'Option Two'},
  98. ]}
  99. onChange={mock}
  100. />
  101. );
  102. // click on the trigger button
  103. await userEvent.click(screen.getByRole('combobox'));
  104. // select Option One & Option Two
  105. await userEvent.click(screen.getByRole('option', {name: 'Option One'}));
  106. expect(mock).toHaveBeenCalledWith({value: '"opt_one"', label: 'Option One'});
  107. await userEvent.clear(screen.getByRole('combobox'));
  108. await userEvent.click(screen.getByRole('option', {name: 'Option Two'}));
  109. expect(mock).toHaveBeenCalledWith({value: '"opt_two"', label: 'Option Two'});
  110. });
  111. });