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.

Checkbox-test.tsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 * as React from 'react';
  23. import { renderComponent } from '../../../helpers/testReactTestingUtils';
  24. import Checkbox from '../Checkbox';
  25. describe.each([
  26. { children: null, describtion: 'with no children' },
  27. { children: <a>child</a>, describtion: 'with children' }
  28. ])('Checkbox $describtion', ({ children }) => {
  29. it('should call check function', async () => {
  30. const user = userEvent.setup();
  31. const onCheck = jest.fn();
  32. const rerender = renderCheckbox({
  33. label: 'me',
  34. children,
  35. onCheck,
  36. checked: false,
  37. title: 'title'
  38. });
  39. await user.click(screen.getByRole('checkbox', { name: 'me' }));
  40. expect(onCheck).toHaveBeenCalledWith(true, undefined);
  41. expect(screen.getByTitle('title')).toBeInTheDocument();
  42. rerender({ checked: true });
  43. await user.click(screen.getByRole('checkbox', { name: 'me' }));
  44. expect(onCheck).toHaveBeenCalledWith(false, undefined);
  45. });
  46. it('should accept partial state', () => {
  47. renderCheckbox({ label: 'me', thirdState: true, children, checked: false });
  48. expect(screen.getByRole('checkbox', { name: 'me' })).toHaveAttribute('aria-checked', 'mixed');
  49. });
  50. it('should render loading state', () => {
  51. jest.useFakeTimers();
  52. renderCheckbox({ label: 'me', children, loading: true });
  53. jest.runAllTimers();
  54. expect(screen.getByLabelText('me')).toMatchSnapshot();
  55. jest.useRealTimers();
  56. });
  57. });
  58. it('should render the checkbox on the right', () => {
  59. renderCheckbox({ label: 'me', children: <a>child</a>, right: true });
  60. expect(screen.getByRole('checkbox', { name: 'me' })).toMatchSnapshot();
  61. });
  62. function renderCheckbox(override?: Partial<Checkbox['props']>) {
  63. const { rerender } = renderComponent(
  64. <Checkbox checked={true} onCheck={jest.fn()} {...override} />
  65. );
  66. return function(reoverride?: Partial<Checkbox['props']>) {
  67. rerender(<Checkbox checked={true} onCheck={jest.fn()} {...override} {...reoverride} />);
  68. };
  69. }