trialStarter.spec.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import type {ComponentProps} from 'react';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import {TeamFixture} from 'sentry-fixture/team';
  5. import {SubscriptionFixture} from 'getsentry-test/fixtures/subscription';
  6. import {act, render, screen} from 'sentry-test/reactTestingLibrary';
  7. import TrialStarter from 'getsentry/components/trialStarter';
  8. import SubscriptionStore from 'getsentry/stores/subscriptionStore';
  9. type RendererProps = Parameters<ComponentProps<typeof TrialStarter>['children']>[0];
  10. describe('TrialStarter', function () {
  11. const org = OrganizationFixture();
  12. const sub = SubscriptionFixture({organization: org});
  13. SubscriptionStore.set(org.slug, sub);
  14. it('starts a trial', async function () {
  15. const handleTrialStarted = jest.fn();
  16. // eslint-disable-next-line no-empty-pattern
  17. const renderer = jest.fn(({}: RendererProps) => <div>render text</div>);
  18. MockApiClient.addMockResponse({
  19. url: `/organizations/${org.slug}/`,
  20. body: org,
  21. });
  22. MockApiClient.addMockResponse({
  23. url: `/organizations/${org.slug}/projects/`,
  24. body: [ProjectFixture()],
  25. });
  26. MockApiClient.addMockResponse({
  27. url: `/organizations/${org.slug}/teams/`,
  28. body: [TeamFixture()],
  29. });
  30. render(
  31. <TrialStarter
  32. organization={org}
  33. source="test-abc"
  34. onTrialStarted={handleTrialStarted}
  35. >
  36. {renderer}
  37. </TrialStarter>
  38. );
  39. expect(screen.getByText('render text')).toBeInTheDocument();
  40. expect(renderer).toHaveBeenCalled();
  41. // Setup to start subscription
  42. const startTrialMock = MockApiClient.addMockResponse({
  43. url: `/customers/${org.slug}/`,
  44. method: 'PUT',
  45. });
  46. const reloadSubsMock = MockApiClient.addMockResponse({
  47. url: `/subscriptions/${org.slug}/`,
  48. method: 'GET',
  49. body: sub,
  50. });
  51. // Start trial
  52. await act(() => renderer.mock.calls.at(-1)![0].startTrial());
  53. expect(startTrialMock).toHaveBeenCalled();
  54. // Trial started completed
  55. expect(handleTrialStarted).toHaveBeenCalled();
  56. expect(reloadSubsMock).toHaveBeenCalled();
  57. const startedCall = renderer.mock.calls.at(-1)![0];
  58. expect(startedCall.trialStarting).toBe(false);
  59. expect(startedCall.trialStarted).toBe(true);
  60. expect(startedCall.trialFailed).toBe(false);
  61. });
  62. it('handles failing to start a trial', async function () {
  63. const handleTrialFailed = jest.fn();
  64. // eslint-disable-next-line no-empty-pattern
  65. const renderer = jest.fn(({}: RendererProps) => null);
  66. render(
  67. <TrialStarter
  68. organization={org}
  69. source="test-abc"
  70. onTrialFailed={handleTrialFailed}
  71. >
  72. {renderer}
  73. </TrialStarter>
  74. );
  75. // Setup to start subscription
  76. const startTrialMock = MockApiClient.addMockResponse({
  77. url: `/customers/${org.slug}/`,
  78. method: 'PUT',
  79. statusCode: 400,
  80. });
  81. // Start trial
  82. await act(() => renderer.mock.calls.at(-1)![0].startTrial());
  83. expect(startTrialMock).toHaveBeenCalled();
  84. // Trial started completed (skip second render call)
  85. const startedCall = renderer.mock.calls.at(-1)![0];
  86. expect(handleTrialFailed).toHaveBeenCalled();
  87. expect(startedCall.trialStarting).toBe(false);
  88. expect(startedCall.trialStarted).toBe(false);
  89. expect(startedCall.trialFailed).toBe(true);
  90. });
  91. });