radioGroupRating.spec.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {
  3. RadioGroupRating,
  4. RadioGroupRatingProps,
  5. } from 'sentry/components/radioGroupRating';
  6. const options: RadioGroupRatingProps['options'] = {
  7. 0: {
  8. label: 'Very Dissatisfied',
  9. description: "Not disappointed (It isn't really useful)",
  10. },
  11. 1: {
  12. label: 'Dissatisfied',
  13. },
  14. 2: {
  15. label: 'Neutral',
  16. },
  17. 3: {
  18. label: 'Satisfied',
  19. },
  20. 4: {
  21. description: "Very disappointed (It's a deal breaker)",
  22. label: 'Very Satisfied',
  23. },
  24. };
  25. describe('RadioGroupRating', function () {
  26. it('render numerical labels', async function () {
  27. const handleChange = jest.fn();
  28. render(
  29. <RadioGroupRating
  30. name="feelingIfFeatureNotAvailableRating"
  31. options={options}
  32. onChange={handleChange}
  33. label="How satisfied are you with this feature?"
  34. />
  35. );
  36. expect(
  37. screen.getByText('How satisfied are you with this feature?')
  38. ).toBeInTheDocument();
  39. expect(screen.getAllByRole('radio')).toHaveLength(Object.keys(options).length);
  40. Object.keys(options).forEach((key, index) => {
  41. expect(screen.getByText(index + 1)).toBeInTheDocument();
  42. expect(
  43. screen.getByLabelText(`Select option ${options[key].label}`)
  44. ).toBeInTheDocument();
  45. const description = options[key].description;
  46. if (description) {
  47. expect(screen.getByText(description)).toBeInTheDocument();
  48. }
  49. });
  50. // Click on the first option
  51. await userEvent.click(screen.getByLabelText(`Select option ${options[0].label}`));
  52. expect(handleChange).toHaveBeenCalledWith('0');
  53. });
  54. });