productUnavailableCTA.spec.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {SubscriptionFixture} from 'getsentry-test/fixtures/subscription';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {
  5. act,
  6. render,
  7. renderGlobalModal,
  8. screen,
  9. userEvent,
  10. waitFor,
  11. } from 'sentry-test/reactTestingLibrary';
  12. import type {Organization} from 'sentry/types/organization';
  13. import {PlanFixture} from 'getsentry/__fixtures__/plan';
  14. import {PreviewDataFixture} from 'getsentry/__fixtures__/previewData';
  15. import {ProductUnavailableCTA} from 'getsentry/components/productUnavailableCTA';
  16. import type {Reservations} from 'getsentry/components/upgradeNowModal/types';
  17. import usePreviewData from 'getsentry/components/upgradeNowModal/usePreviewData';
  18. import SubscriptionStore from 'getsentry/stores/subscriptionStore';
  19. import {PlanTier} from 'getsentry/types';
  20. jest.mock('getsentry/components/upgradeNowModal/usePreviewData');
  21. function renderMockRequests({
  22. planTier,
  23. organization,
  24. canSelfServe,
  25. }: {
  26. organization: Organization;
  27. planTier: PlanTier;
  28. canSelfServe?: boolean;
  29. }) {
  30. const subscription = SubscriptionFixture({
  31. organization,
  32. planTier,
  33. canSelfServe,
  34. });
  35. act(() => SubscriptionStore.set(organization.slug, subscription));
  36. MockApiClient.addMockResponse({
  37. url: `/subscriptions/${organization.slug}/`,
  38. body: {
  39. planTier,
  40. },
  41. });
  42. const isAncientPlan = [PlanTier.MM1, PlanTier.MM2].includes(planTier);
  43. if (isAncientPlan) {
  44. const requestUpdatePlan = MockApiClient.addMockResponse({
  45. url: `/organizations/${organization.slug}/plan-upgrade-request/`,
  46. method: 'POST',
  47. });
  48. return {requestUpdatePlan};
  49. }
  50. const requestUpdatePlanDueToReplay = MockApiClient.addMockResponse({
  51. url: `/organizations/${organization.slug}/replay-onboard-request/`,
  52. method: 'POST',
  53. });
  54. return {requestUpdatePlanDueToReplay};
  55. }
  56. describe('ProductUnavailableCTA', function () {
  57. describe('with no billing access', function () {
  58. it('renders no alert', function () {
  59. const organization = OrganizationFixture({
  60. features: ['performance-view', 'session-replay'],
  61. });
  62. renderMockRequests({
  63. planTier: PlanTier.AM2,
  64. organization,
  65. });
  66. const {container} = render(<ProductUnavailableCTA organization={organization} />);
  67. expect(container).toBeEmptyDOMElement();
  68. });
  69. it('without performance and session replay', async function () {
  70. const {organization, router} = initializeOrg();
  71. const mockRequests = renderMockRequests({
  72. planTier: PlanTier.MM1,
  73. organization,
  74. });
  75. render(<ProductUnavailableCTA organization={organization} />, {
  76. router,
  77. });
  78. expect(
  79. await screen.findByText(/request an owner in your organization to update/i)
  80. ).toBeInTheDocument();
  81. expect(screen.getByText(/use performance and session replay/i)).toBeInTheDocument();
  82. await userEvent.click(screen.getByRole('button', {name: /request update/i}));
  83. await waitFor(() => {
  84. expect(mockRequests?.requestUpdatePlan).toHaveBeenCalledWith(
  85. `/organizations/org-slug/plan-upgrade-request/`,
  86. expect.objectContaining({
  87. method: 'POST',
  88. })
  89. );
  90. });
  91. });
  92. it('without session replay', async function () {
  93. const {organization, router} = initializeOrg({
  94. organization: {
  95. features: ['performance-view'],
  96. },
  97. });
  98. const mockRequests = renderMockRequests({
  99. planTier: PlanTier.AM1,
  100. organization,
  101. });
  102. render(<ProductUnavailableCTA organization={organization} />, {
  103. router,
  104. });
  105. expect(
  106. await screen.findByText(/request an owner in your organization to update/i)
  107. ).toBeInTheDocument();
  108. expect(screen.getByText(/use session replay/i)).toBeInTheDocument();
  109. await userEvent.click(screen.getByRole('button', {name: /request update/i}));
  110. await waitFor(() => {
  111. expect(mockRequests.requestUpdatePlanDueToReplay).toHaveBeenCalledWith(
  112. `/organizations/${organization.slug}/replay-onboard-request/`,
  113. expect.objectContaining({
  114. method: 'POST',
  115. data: {
  116. name: 'am1-non-beta',
  117. },
  118. })
  119. );
  120. });
  121. });
  122. });
  123. describe('with billing access', function () {
  124. it('renders no alert', function () {
  125. const organization = OrganizationFixture({
  126. access: ['org:billing'],
  127. features: ['performance-view', 'session-replay'],
  128. });
  129. renderMockRequests({
  130. planTier: PlanTier.AM2,
  131. organization,
  132. });
  133. const {container} = render(<ProductUnavailableCTA organization={organization} />);
  134. expect(container).toBeEmptyDOMElement();
  135. });
  136. it('without performance and session replay', async function () {
  137. const {router, organization} = initializeOrg({
  138. organization: {
  139. access: ['org:billing'] as any, // TODO(ts): Fix this type for organizations on a plan
  140. },
  141. });
  142. renderMockRequests({
  143. planTier: PlanTier.MM1,
  144. organization,
  145. });
  146. render(<ProductUnavailableCTA organization={organization} />, {
  147. router,
  148. });
  149. expect(await screen.findByText(/update your organization/i)).toBeInTheDocument();
  150. expect(screen.getByText(/use performance and session replay/i)).toBeInTheDocument();
  151. expect(screen.getByRole('button', {name: /manage subscription/i})).toHaveAttribute(
  152. 'href',
  153. '/settings/org-slug/billing/overview/?referrer=replay_onboard_mmx-cta'
  154. );
  155. });
  156. it('without session replay', async function () {
  157. const {organization, router} = initializeOrg({
  158. organization: {
  159. access: ['org:billing'] as any, // TODO(ts): Fix this type for organizations on a plan
  160. features: ['performance-view'],
  161. },
  162. });
  163. // can self-serve
  164. renderMockRequests({
  165. planTier: PlanTier.AM1,
  166. organization,
  167. canSelfServe: true,
  168. });
  169. const MockUsePreviewData = usePreviewData as jest.MockedFunction<
  170. typeof usePreviewData
  171. >;
  172. const mockReservations: Reservations = {
  173. reservedErrors: 50000,
  174. reservedTransactions: 0,
  175. reservedReplays: 500,
  176. reservedAttachments: 0,
  177. reservedMonitorSeats: 0,
  178. reservedUptime: 0,
  179. };
  180. const mockPlan = PlanFixture({});
  181. const mockPreview = PreviewDataFixture({});
  182. MockUsePreviewData.mockReturnValue({
  183. loading: false,
  184. error: false,
  185. plan: mockPlan,
  186. previewData: mockPreview,
  187. reservations: mockReservations,
  188. });
  189. const {rerender} = render(<ProductUnavailableCTA organization={organization} />, {
  190. router,
  191. });
  192. expect(await screen.findByText(/update your organization/i)).toBeInTheDocument();
  193. expect(screen.getByText(/use session replay/i)).toBeInTheDocument();
  194. renderGlobalModal();
  195. await userEvent.click(screen.getByRole('button', {name: /update plan/i}));
  196. expect(
  197. await screen.findByRole('heading', {name: /enable session replays now/i})
  198. ).toBeInTheDocument();
  199. // can not self-serve
  200. renderMockRequests({
  201. planTier: PlanTier.AM1,
  202. organization,
  203. canSelfServe: false,
  204. });
  205. rerender(<ProductUnavailableCTA organization={organization} />);
  206. expect(
  207. await screen.findByRole('button', {name: /manage subscription/i})
  208. ).toBeInTheDocument();
  209. });
  210. });
  211. });