radioGroupRating.spec.tsx 1.6 KB

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