dataDownload.spec.tsx 4.2 KB

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