cancelSubscription.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {SubscriptionFixture} from 'getsentry-test/fixtures/subscription';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import CancelSubscription from 'getsentry/views/cancelSubscription';
  5. describe('CancelSubscription', function () {
  6. const organization = OrganizationFixture();
  7. const subscription = SubscriptionFixture({
  8. organization,
  9. plan: 's1',
  10. isFree: false,
  11. canCancel: true,
  12. canSelfServe: true,
  13. });
  14. beforeEach(function () {
  15. MockApiClient.clearMockResponses();
  16. MockApiClient.addMockResponse({
  17. url: `/customers/${organization.slug}/`,
  18. method: 'GET',
  19. body: subscription,
  20. });
  21. MockApiClient.addMockResponse({
  22. url: `/subscriptions/${organization.slug}/`,
  23. method: 'GET',
  24. body: subscription,
  25. });
  26. MockApiClient.addMockResponse({
  27. url: `/organizations/${organization.slug}/promotions/trigger-check/`,
  28. method: 'POST',
  29. body: {},
  30. });
  31. MockApiClient.addMockResponse({
  32. url: `/organizations/${organization.slug}/prompts-activity/`,
  33. method: 'PUT',
  34. body: {},
  35. });
  36. });
  37. it('renders', async function () {
  38. render(<CancelSubscription />);
  39. expect(
  40. await screen.findByRole('heading', {name: 'Cancel Subscription'})
  41. ).toBeInTheDocument();
  42. expect(
  43. await screen.findByRole('radio', {
  44. name: 'The project/product/company is shutting down.',
  45. })
  46. ).toBeInTheDocument();
  47. });
  48. it('can not cancel free plans', async function () {
  49. MockApiClient.addMockResponse({
  50. url: `/customers/${organization.slug}/`,
  51. method: 'GET',
  52. body: SubscriptionFixture({organization, plan: 'f1', isFree: true}),
  53. });
  54. render(<CancelSubscription />);
  55. expect(
  56. await screen.findByText(/your plan is not eligible to be cancelled/i)
  57. ).toBeInTheDocument();
  58. expect(screen.queryByRole('radio')).not.toBeInTheDocument();
  59. });
  60. it('displays followup textarea when option is selected', async function () {
  61. render(<CancelSubscription />);
  62. const radio = await screen.findByRole('radio', {
  63. name: 'The project/product/company is shutting down.',
  64. });
  65. expect(radio).toBeInTheDocument();
  66. expect(screen.queryByRole('textbox', {name: 'followup'})).not.toBeInTheDocument();
  67. await userEvent.click(radio);
  68. expect(screen.getByRole('textbox', {name: /Sorry to hear that/})).toBeInTheDocument();
  69. });
  70. it('calls cancel API', async function () {
  71. const mock = MockApiClient.addMockResponse({
  72. url: `/customers/${organization.slug}/`,
  73. method: 'DELETE',
  74. });
  75. render(<CancelSubscription />);
  76. const radio = await screen.findByRole('radio', {name: 'Other'});
  77. expect(radio).toBeInTheDocument();
  78. await userEvent.click(radio);
  79. await userEvent.type(screen.getByRole('textbox'), 'Cancellation reason');
  80. await userEvent.click(screen.getByRole('button', {name: /Cancel Subscription/}));
  81. expect(mock).toHaveBeenCalledWith(
  82. `/customers/${organization.slug}/`,
  83. expect.objectContaining({
  84. data: {
  85. reason: 'other',
  86. followup: 'Cancellation reason',
  87. },
  88. })
  89. );
  90. });
  91. });