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.

ToggleButton-test.tsx 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 userEvent from '@testing-library/user-event';
  22. import { render } from '../../../helpers/testUtils';
  23. import { FCProps } from '../../../types/misc';
  24. import { getTabPanelId } from '../../helpers';
  25. import { ToggleButton, ToggleButtonsOption } from '../ToggleButton';
  26. it('should render all options', async () => {
  27. const user = userEvent.setup();
  28. const onChange = jest.fn();
  29. const options: Array<ToggleButtonsOption<number>> = [
  30. { value: 1, label: 'first' },
  31. { value: 2, label: 'disabled', disabled: true },
  32. { value: 3, label: 'has counter', counter: 7 },
  33. ];
  34. renderToggleButtons({ onChange, options, value: 1 });
  35. expect(screen.getAllByRole('radio')).toHaveLength(3);
  36. await user.click(screen.getByText('first'));
  37. expect(onChange).not.toHaveBeenCalled();
  38. await user.click(screen.getByText('has counter'));
  39. expect(onChange).toHaveBeenCalledWith(3);
  40. });
  41. it('should work in tablist mode', () => {
  42. const onChange = jest.fn();
  43. const options: Array<ToggleButtonsOption<number>> = [
  44. { value: 1, label: 'first' },
  45. { value: 2, label: 'second' },
  46. { value: 3, label: 'third' },
  47. ];
  48. renderToggleButtons({ onChange, options, value: 1, role: 'tablist' });
  49. expect(screen.getAllByRole('tab')).toHaveLength(3);
  50. expect(screen.getByRole('tab', { name: 'second' })).toHaveAttribute(
  51. 'aria-controls',
  52. getTabPanelId(2),
  53. );
  54. });
  55. function renderToggleButtons(props: Partial<FCProps<typeof ToggleButton>> = {}) {
  56. return render(<ToggleButton onChange={jest.fn()} options={[]} {...props} />);
  57. }