dataDownload.spec.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {RouteComponentPropsFixture} from 'sentry-fixture/routeComponentPropsFixture';
  3. import {render, screen, waitForElementToBeRemoved} from 'sentry-test/reactTestingLibrary';
  4. import {textWithMarkupMatcher} from 'sentry-test/utils';
  5. import {ExportQueryType} from 'sentry/components/dataExport';
  6. import DataDownload, {DownloadStatus} from 'sentry/views/dataExport/dataDownload';
  7. describe('DataDownload', function () {
  8. beforeEach(MockApiClient.clearMockResponses);
  9. const dateExpired = new Date();
  10. const organization = OrganizationFixture();
  11. const mockRouteParams = {
  12. orgId: organization.slug,
  13. dataExportId: '721',
  14. };
  15. const getDataExportDetails = (body, statusCode = 200) =>
  16. MockApiClient.addMockResponse({
  17. url: `/organizations/${mockRouteParams.orgId}/data-export/${mockRouteParams.dataExportId}/`,
  18. body,
  19. statusCode,
  20. });
  21. it('should send a request to the data export endpoint', function () {
  22. const getValid = getDataExportDetails(DownloadStatus.VALID);
  23. render(<DataDownload {...RouteComponentPropsFixture()} params={mockRouteParams} />);
  24. expect(getValid).toHaveBeenCalledTimes(1);
  25. });
  26. it("should render the 'Error' view when appropriate", async function () {
  27. const errors = {
  28. download: {
  29. status: 403,
  30. statusText: 'Forbidden',
  31. responseJSON: {
  32. detail: 'You are not allowed',
  33. },
  34. },
  35. };
  36. getDataExportDetails({errors}, 403);
  37. render(<DataDownload {...RouteComponentPropsFixture()} params={mockRouteParams} />);
  38. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  39. expect(screen.getByText('403 -')).toBeInTheDocument(); // Either the code or the mock is mistaken about the data return format
  40. });
  41. it("should render the 'Early' view when appropriate", async function () {
  42. const status = DownloadStatus.EARLY;
  43. getDataExportDetails({status});
  44. render(<DataDownload {...RouteComponentPropsFixture()} params={mockRouteParams} />);
  45. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  46. expect(
  47. screen.getByText(textWithMarkupMatcher('What are you doing here?'))
  48. ).toBeInTheDocument();
  49. expect(screen.getByText(/were you invited/)).toBeInTheDocument();
  50. });
  51. it("should render the 'Expired' view when appropriate", async function () {
  52. const status = DownloadStatus.EXPIRED;
  53. const response = {status, query: {type: ExportQueryType.ISSUES_BY_TAG}};
  54. getDataExportDetails(response);
  55. render(<DataDownload {...RouteComponentPropsFixture()} params={mockRouteParams} />);
  56. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  57. expect(screen.getByText('This is awkward.')).toBeInTheDocument();
  58. expect(screen.getByRole('button', {name: 'Start a New Download'})).toHaveAttribute(
  59. 'href',
  60. `/organizations/${mockRouteParams.orgId}/issues/`
  61. );
  62. });
  63. it("should render the 'Valid' view when appropriate", async function () {
  64. const status = DownloadStatus.VALID;
  65. getDataExportDetails({dateExpired, status});
  66. render(<DataDownload {...RouteComponentPropsFixture()} params={mockRouteParams} />);
  67. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  68. expect(screen.getByText('All done.')).toBeInTheDocument();
  69. expect(screen.getByRole('button', {name: 'Download CSV'})).toHaveAttribute(
  70. 'href',
  71. `/api/0/organizations/${mockRouteParams.orgId}/data-export/${mockRouteParams.dataExportId}/?download=true`
  72. );
  73. expect(
  74. screen.getByText(
  75. textWithMarkupMatcher("That link won't last forever — it expires:Oct 17, 2:41 AM")
  76. )
  77. ).toBeInTheDocument();
  78. });
  79. it('should render the Open in Discover button when needed', async function () {
  80. const status = DownloadStatus.VALID;
  81. getDataExportDetails({
  82. dateExpired,
  83. status,
  84. query: {
  85. type: ExportQueryType.DISCOVER,
  86. info: {},
  87. },
  88. });
  89. render(<DataDownload {...RouteComponentPropsFixture()} params={mockRouteParams} />);
  90. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  91. expect(screen.getByRole('button', {name: 'Open in Discover'})).toBeInTheDocument();
  92. });
  93. it('should not render the Open in Discover button when not needed', function () {
  94. const status = DownloadStatus.VALID;
  95. getDataExportDetails({
  96. dateExpired,
  97. status,
  98. query: {
  99. type: ExportQueryType.ISSUES_BY_TAG,
  100. info: {},
  101. },
  102. });
  103. render(<DataDownload {...RouteComponentPropsFixture()} params={mockRouteParams} />);
  104. expect(
  105. screen.queryByRole('button', {name: 'Open in Discover'})
  106. ).not.toBeInTheDocument();
  107. });
  108. });