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.

Dropdown-test.tsx 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 { renderWithRouter } from '../../helpers/testUtils';
  22. import { ButtonSecondary } from '../../sonar-aligned/components/buttons';
  23. import { ActionsDropdown, Dropdown } from '../Dropdown';
  24. describe('Dropdown', () => {
  25. it('renders', async () => {
  26. const { user } = renderDropdown();
  27. expect(screen.getByRole('button')).toBeInTheDocument();
  28. await user.click(screen.getByRole('button'));
  29. expect(screen.getByRole('menu')).toBeInTheDocument();
  30. });
  31. it('toggles with render prop', async () => {
  32. const { user } = renderDropdown({
  33. children: ({ onToggleClick }) => <ButtonSecondary onClick={onToggleClick} />,
  34. });
  35. await user.click(screen.getByRole('button'));
  36. expect(screen.getByRole('menu')).toBeVisible();
  37. });
  38. it('closes when clicking outside of menu', async () => {
  39. const { user } = renderDropdown();
  40. await user.click(screen.getByRole('button'));
  41. expect(screen.getByRole('menu')).toBeInTheDocument();
  42. await user.click(document.body);
  43. expect(screen.queryByRole('menu')).not.toBeInTheDocument();
  44. });
  45. it('does not close when clicking ouside of menu', async () => {
  46. const { user } = renderDropdown({ withClickOutHandler: false });
  47. await user.click(screen.getByRole('button'));
  48. expect(screen.getByRole('menu')).toBeInTheDocument();
  49. await user.click(document.body);
  50. expect(screen.getByRole('menu')).toBeInTheDocument();
  51. });
  52. it('closes when other target gets focus', async () => {
  53. const { user } = renderDropdown();
  54. await user.click(screen.getByRole('button'));
  55. expect(screen.getByRole('menu')).toBeInTheDocument();
  56. await user.tab();
  57. expect(screen.queryByRole('menu')).not.toBeInTheDocument();
  58. });
  59. it('does not close when other target gets focus', async () => {
  60. const { user } = renderDropdown({ withFocusOutHandler: false });
  61. await user.click(screen.getByRole('button'));
  62. expect(screen.getByRole('menu')).toBeInTheDocument();
  63. await user.tab();
  64. expect(screen.getByRole('menu')).toBeInTheDocument();
  65. });
  66. function renderDropdown(props: Partial<Dropdown['props']> = {}) {
  67. const { children, ...rest } = props;
  68. return renderWithRouter(
  69. <Dropdown id="test-menu" overlay={<div id="overlay" />} {...rest}>
  70. {children ?? <ButtonSecondary />}
  71. </Dropdown>,
  72. );
  73. }
  74. });
  75. describe('ActionsDropdown', () => {
  76. it('renders', () => {
  77. setup();
  78. expect(screen.getByRole('button')).toHaveAccessibleName('menu');
  79. });
  80. function setup() {
  81. return renderWithRouter(
  82. <ActionsDropdown id="test-menu">
  83. <div id="overlay" />
  84. </ActionsDropdown>,
  85. );
  86. }
  87. });