checkbox.spec.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {useState} from 'react';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import Checkbox from 'sentry/components/checkbox';
  4. describe('Checkbox', function () {
  5. const defaultProps = {
  6. checked: false,
  7. onChange: jest.fn(),
  8. };
  9. describe('snapshots', function () {
  10. it('unchecked state', async function () {
  11. const {container} = render(<Checkbox {...defaultProps} />);
  12. expect(await screen.findByRole('checkbox')).toBeInTheDocument();
  13. expect(container).toSnapshot();
  14. });
  15. it('checked state', async function () {
  16. const {container} = render(<Checkbox {...defaultProps} checked />);
  17. expect(await screen.findByRole('checkbox')).toBeInTheDocument();
  18. expect(container).toSnapshot();
  19. });
  20. it('indeterminate state', async function () {
  21. const {container} = render(<Checkbox {...defaultProps} checked="indeterminate" />);
  22. expect(await screen.findByRole('checkbox')).toBeInTheDocument();
  23. expect(container).toSnapshot();
  24. });
  25. });
  26. describe('behavior', function () {
  27. function CheckboxWithLabel() {
  28. const [checked, setChecked] = useState(false);
  29. return (
  30. <label>
  31. <Checkbox
  32. checked={checked}
  33. onChange={e => {
  34. setChecked(e.target.checked);
  35. }}
  36. />
  37. Label text
  38. </label>
  39. );
  40. }
  41. it('toggles on click', function () {
  42. render(<CheckboxWithLabel />);
  43. expect(screen.getByRole('checkbox')).not.toBeChecked();
  44. userEvent.click(screen.getByLabelText('Label text'));
  45. expect(screen.getByRole('checkbox')).toBeChecked();
  46. });
  47. });
  48. });