You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiSelectMenu-test.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { screen } from '@testing-library/react';
  21. import userEvent from '@testing-library/user-event';
  22. import { renderWithContext } from '../../../helpers/testUtils';
  23. import { MultiSelectMenu } from '../MultiSelectMenu';
  24. const elements = ['foo', 'bar', 'baz'];
  25. beforeEach(() => {
  26. jest.useFakeTimers();
  27. });
  28. afterEach(() => {
  29. jest.runOnlyPendingTimers();
  30. jest.useRealTimers();
  31. });
  32. it('should allow selecting and deselecting a new option', async () => {
  33. const user = userEvent.setup({ delay: null });
  34. const onSelect = jest.fn();
  35. const onUnselect = jest.fn();
  36. renderMultiselect({ elements, onSelect, onUnselect, allowNewElements: true });
  37. await user.keyboard('new option');
  38. jest.runAllTimers(); // skip the debounce
  39. expect(await screen.findByText('new option')).toBeInTheDocument();
  40. await user.click(screen.getByText('new option'));
  41. expect(onSelect).toHaveBeenCalledWith('new option');
  42. renderMultiselect({
  43. elements,
  44. onUnselect,
  45. allowNewElements: true,
  46. selectedElements: ['new option'],
  47. });
  48. await user.click(screen.getByText('new option'));
  49. expect(onUnselect).toHaveBeenCalledWith('new option');
  50. });
  51. it('should ignore the left and right arrow keys', async () => {
  52. const user = userEvent.setup({ delay: null });
  53. const onSelect = jest.fn();
  54. renderMultiselect({ elements, onSelect });
  55. /* eslint-disable testing-library/no-node-access */
  56. await user.keyboard('{arrowdown}');
  57. expect(screen.getAllByRole('checkbox')[1].parentElement).toHaveClass('active');
  58. await user.keyboard('{arrowleft}');
  59. expect(screen.getAllByRole('checkbox')[1].parentElement).toHaveClass('active');
  60. await user.keyboard('{arrowright}');
  61. expect(screen.getAllByRole('checkbox')[1].parentElement).toHaveClass('active');
  62. await user.keyboard('{arrowdown}');
  63. expect(screen.getAllByRole('checkbox')[1].parentElement).not.toHaveClass('active');
  64. expect(screen.getAllByRole('checkbox')[2].parentElement).toHaveClass('active');
  65. await user.keyboard('{arrowup}');
  66. await user.keyboard('{arrowup}');
  67. expect(screen.getAllByRole('checkbox')[0].parentElement).toHaveClass('active');
  68. await user.keyboard('{arrowup}');
  69. expect(screen.getAllByRole('checkbox')[2].parentElement).toHaveClass('active');
  70. expect(screen.getAllByRole('checkbox')[2]).not.toBeChecked();
  71. await user.keyboard('{enter}');
  72. expect(onSelect).toHaveBeenCalledWith('baz');
  73. });
  74. it('should show no results', () => {
  75. renderMultiselect();
  76. expect(screen.getByText('no results')).toBeInTheDocument();
  77. });
  78. function renderMultiselect(props: Partial<MultiSelectMenu['props']> = {}) {
  79. return renderWithContext(
  80. <MultiSelectMenu
  81. createElementLabel="create thing"
  82. elements={[]}
  83. filterSelected={jest.fn()}
  84. listSize={10}
  85. noResultsLabel="no results"
  86. onSearch={jest.fn(() => Promise.resolve())}
  87. onSelect={jest.fn()}
  88. onUnselect={jest.fn()}
  89. placeholder=""
  90. searchInputAriaLabel="search"
  91. selectedElements={[]}
  92. {...props}
  93. />,
  94. );
  95. }