featureFeedback.spec.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {
  3. render,
  4. screen,
  5. userEvent,
  6. waitForElementToBeRemoved,
  7. } from 'sentry-test/reactTestingLibrary';
  8. import * as indicators from 'sentry/actionCreators/indicator';
  9. import {FeatureFeedback, FeatureFeedbackProps} from 'sentry/components/featureFeedback';
  10. import GlobalModal from 'sentry/components/globalModal';
  11. import ModalStore from 'sentry/stores/modalStore';
  12. import {RouteContext} from 'sentry/views/routeContext';
  13. describe('FeatureFeedback', function () {
  14. const {router} = initializeOrg();
  15. function TestComponent({
  16. feedbackTypes,
  17. }: {
  18. feedbackTypes?: FeatureFeedbackProps['feedbackTypes'];
  19. }) {
  20. return (
  21. <RouteContext.Provider
  22. value={{
  23. router,
  24. location: router.location,
  25. params: {},
  26. routes: [],
  27. }}
  28. >
  29. <FeatureFeedback featureName="test" feedbackTypes={feedbackTypes} />
  30. <GlobalModal />
  31. </RouteContext.Provider>
  32. );
  33. }
  34. beforeAll(async function () {
  35. // transpile the modal upfront so the test runs fast
  36. await import('sentry/components/featureFeedback/feedbackModal');
  37. });
  38. async function openModal() {
  39. expect(screen.getByText('Give Feedback')).toBeInTheDocument();
  40. userEvent.click(screen.getByText('Give Feedback'));
  41. expect(await screen.findByText('Select type of feedback')).toBeInTheDocument();
  42. }
  43. it('shows the modal on click', async function () {
  44. render(<TestComponent />);
  45. await openModal();
  46. expect(
  47. await screen.findByRole('heading', {name: 'Submit Feedback'})
  48. ).toBeInTheDocument();
  49. });
  50. it('submits modal on click', async function () {
  51. jest.spyOn(indicators, 'addSuccessMessage');
  52. render(<TestComponent />);
  53. await openModal();
  54. // Form fields
  55. expect(screen.getByText('Select type of feedback')).toBeInTheDocument();
  56. expect(screen.getByPlaceholderText('What did you expect?')).toBeInTheDocument();
  57. // Form actions
  58. expect(screen.getByRole('button', {name: 'Cancel'})).toBeInTheDocument();
  59. expect(screen.getByRole('button', {name: 'Submit Feedback'})).toBeDisabled();
  60. // User enters additional feedback message
  61. userEvent.paste(
  62. screen.getByPlaceholderText('What did you expect?'),
  63. 'this is a feedback message'
  64. );
  65. userEvent.keyboard('{enter}');
  66. // Submit button is still disabled
  67. expect(screen.getByRole('button', {name: 'Submit Feedback'})).toBeDisabled();
  68. userEvent.click(screen.getByText('Select type of feedback'));
  69. // Available feedback types
  70. expect(screen.getByText("I don't like this feature")).toBeInTheDocument();
  71. expect(screen.getByText('Other reason')).toBeInTheDocument();
  72. expect(screen.getByText('I like this feature')).toBeInTheDocument();
  73. // Select feedback type
  74. userEvent.click(screen.getByText('I like this feature'));
  75. // Submit button is now enabled because the required field was selected
  76. expect(screen.getByRole('button', {name: 'Submit Feedback'})).toBeEnabled();
  77. userEvent.click(screen.getByRole('button', {name: 'Submit Feedback'}));
  78. expect(indicators.addSuccessMessage).toHaveBeenCalledWith(
  79. 'Thanks for taking the time to provide us feedback!'
  80. );
  81. });
  82. it('renders provided feedbackTypes', async function () {
  83. render(
  84. <TestComponent
  85. feedbackTypes={['Custom feedback type A', 'Custom feedback type B']}
  86. />
  87. );
  88. await openModal();
  89. userEvent.click(screen.getByText('Select type of feedback'));
  90. // Available feedback types
  91. expect(screen.getByText('Custom feedback type A')).toBeInTheDocument();
  92. expect(screen.getByText('Custom feedback type B')).toBeInTheDocument();
  93. });
  94. it('Close modal on click', async function () {
  95. render(<TestComponent />);
  96. await openModal();
  97. userEvent.click(screen.getByRole('button', {name: 'Cancel'}));
  98. ModalStore.reset();
  99. await waitForElementToBeRemoved(() =>
  100. screen.queryByRole('heading', {name: 'Submit Feedback'})
  101. );
  102. });
  103. });