radioGroup.spec.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import RadioGroup from 'sentry/components/forms/controls/radioGroup';
  3. describe('RadioGroup', function () {
  4. it('renders', function () {
  5. const {container} = render(
  6. <RadioGroup
  7. label="test"
  8. value="choice_one"
  9. choices={[
  10. ['choice_one', 'Choice One'],
  11. ['choice_two', 'Choice Two'],
  12. ['choice_three', 'Choice Three'],
  13. ]}
  14. onChange={jest.fn()}
  15. />
  16. );
  17. expect(container).toSnapshot();
  18. });
  19. it('renders disabled', function () {
  20. const {container} = render(
  21. <RadioGroup
  22. label="test"
  23. value="choice_one"
  24. disabled
  25. choices={[['choice_one', 'Choice One']]}
  26. onChange={jest.fn()}
  27. />
  28. );
  29. expect(container).toSnapshot();
  30. expect(screen.getByRole('radio', {name: 'Select Choice One'})).toBeDisabled();
  31. });
  32. it('renders disabled choice', async function () {
  33. const {container} = render(
  34. <RadioGroup
  35. label="test"
  36. value="choice_one"
  37. choices={[
  38. ['choice_one', 'Choice One'],
  39. ['choice_two', 'Choice Two'],
  40. ]}
  41. disabledChoices={[['choice_two', 'Reason why choice two is disabled']]}
  42. onChange={jest.fn()}
  43. />
  44. );
  45. expect(container).toSnapshot();
  46. expect(screen.getByRole('radio', {name: 'Select Choice One'})).toBeEnabled();
  47. expect(screen.getByRole('radio', {name: 'Select Choice Two'})).toBeDisabled();
  48. userEvent.hover(screen.getByRole('radio', {name: 'Select Choice Two'}));
  49. expect(
  50. await screen.findByText('Reason why choice two is disabled')
  51. ).toBeInTheDocument();
  52. });
  53. it('can select a different item', function () {
  54. const {container} = render(
  55. <RadioGroup
  56. label="test"
  57. value="choice_three"
  58. choices={[
  59. ['choice_one', 'Choice One'],
  60. ['choice_two', 'Choice Two'],
  61. ['choice_three', 'Choice Three'],
  62. ]}
  63. onChange={jest.fn()}
  64. />
  65. );
  66. expect(container).toSnapshot();
  67. });
  68. it('calls onChange when clicked', function () {
  69. const mock = jest.fn();
  70. render(
  71. <RadioGroup
  72. label="test"
  73. value="choice_one"
  74. choices={[
  75. ['choice_one', 'Choice One'],
  76. ['choice_two', 'Choice Two'],
  77. ['choice_three', 'Choice Three'],
  78. ]}
  79. onChange={mock}
  80. />
  81. );
  82. userEvent.click(screen.getByRole('radio', {name: 'Select Choice Three'}));
  83. expect(mock).toHaveBeenCalledTimes(1);
  84. });
  85. });