選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DropdownMenu-test.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 { act, screen, waitFor } from '@testing-library/react';
  21. import { noop } from 'lodash';
  22. import { render, renderWithRouter } from '../../helpers/testUtils';
  23. import {
  24. DropdownMenu,
  25. ItemButton,
  26. ItemCheckbox,
  27. ItemCopy,
  28. ItemDangerButton,
  29. ItemDivider,
  30. ItemHeader,
  31. ItemLink,
  32. ItemNavLink,
  33. ItemRadioButton,
  34. } from '../DropdownMenu';
  35. import { Tooltip } from '../Tooltip';
  36. import { MenuIcon } from '../icons/MenuIcon';
  37. beforeEach(() => {
  38. jest.useFakeTimers();
  39. });
  40. afterEach(() => {
  41. jest.runOnlyPendingTimers();
  42. jest.useRealTimers();
  43. });
  44. it('should render a full menu correctly', () => {
  45. renderDropdownMenu();
  46. expect(screen.getByRole('menuitem', { name: 'My header' })).toBeInTheDocument();
  47. expect(screen.getByRole('menuitem', { name: 'Test menu item' })).toBeInTheDocument();
  48. expect(screen.getByRole('menuitem', { name: 'Test disabled item' })).toHaveClass('disabled');
  49. });
  50. it('menu items should work with tooltips', async () => {
  51. const { user } = render(
  52. <Tooltip overlay="test tooltip">
  53. <ItemButton onClick={jest.fn()}>button</ItemButton>
  54. </Tooltip>,
  55. {},
  56. { delay: null },
  57. );
  58. expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
  59. await user.hover(screen.getByRole('menuitem'));
  60. expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
  61. act(() => {
  62. jest.runAllTimers();
  63. });
  64. expect(screen.getByRole('tooltip')).toBeVisible();
  65. await user.unhover(screen.getByRole('menuitem'));
  66. expect(screen.getByRole('tooltip')).toBeVisible();
  67. act(() => {
  68. jest.runAllTimers();
  69. });
  70. await waitFor(() => {
  71. expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
  72. });
  73. });
  74. function renderDropdownMenu() {
  75. return renderWithRouter(
  76. <DropdownMenu>
  77. <ItemHeader>My header</ItemHeader>
  78. <ItemNavLink to="/test">Test menu item</ItemNavLink>
  79. <ItemDivider />
  80. <ItemLink disabled to="/test-disabled">
  81. Test disabled item
  82. </ItemLink>
  83. <ItemButton icon={<MenuIcon />} onClick={noop}>
  84. Button
  85. </ItemButton>
  86. <ItemDangerButton onClick={noop}>DangerButton</ItemDangerButton>
  87. <ItemCopy copyValue="copy" tooltipOverlay="overlay">
  88. Copy
  89. </ItemCopy>
  90. <ItemCheckbox checked onCheck={noop}>
  91. Checkbox item
  92. </ItemCheckbox>
  93. <ItemRadioButton checked={false} onCheck={noop} value="radios">
  94. Radio item
  95. </ItemRadioButton>
  96. </DropdownMenu>,
  97. );
  98. }