dataDownload.spec.tsx 4.2 KB

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