featureFeedback.spec.tsx 3.6 KB

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