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.

FacetItem-test.tsx 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 { render } from '../../helpers/testUtils';
  23. import { BaseFacetItem, FacetItemProps } from '../FacetItem';
  24. it('should render a disabled facet item', async () => {
  25. const user = userEvent.setup();
  26. const onClick = jest.fn();
  27. renderComponent({ disabled: true, onClick });
  28. expect(screen.getByRole('checkbox')).toHaveAttribute('aria-disabled', 'true');
  29. await user.click(screen.getByRole('checkbox'));
  30. expect(onClick).not.toHaveBeenCalled();
  31. });
  32. it('should render a non-disabled facet item', async () => {
  33. const user = userEvent.setup();
  34. const onClick = jest.fn();
  35. renderComponent({ active: true, onClick, stat: 3, value: 'foo' });
  36. expect(screen.getByRole('checkbox')).toHaveAttribute('aria-disabled', 'false');
  37. await user.click(screen.getByRole('checkbox'));
  38. expect(onClick).toHaveBeenCalledWith('foo', false);
  39. await user.keyboard('{Meta>}');
  40. await user.click(screen.getByRole('checkbox'));
  41. expect(onClick).toHaveBeenLastCalledWith('foo', true);
  42. });
  43. it('should add an aria label if the name is a string', () => {
  44. renderComponent({ name: 'Foo' });
  45. expect(screen.getByRole('checkbox')).toHaveAccessibleName('Foo');
  46. });
  47. it('should not add an aria label if the name is not a string', () => {
  48. renderComponent({ name: <div>Foo</div>, small: true });
  49. expect(screen.getByRole('checkbox')).not.toHaveAttribute('aria-label');
  50. });
  51. function renderComponent(props: Partial<FacetItemProps> = {}) {
  52. return render(
  53. <BaseFacetItem name="Test facet item" onClick={jest.fn()} value="Value" {...props} />,
  54. );
  55. }