dataDownload.spec.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(<DataDownload params={mockRouteParams} />);
  22. expect(getValid).toHaveBeenCalledTimes(1);
  23. });
  24. it("should render the 'Error' view when appropriate", function () {
  25. const errors = {
  26. download: {
  27. status: 403,
  28. statusText: 'Forbidden',
  29. responseJSON: {
  30. detail: 'You are not allowed',
  31. },
  32. },
  33. };
  34. getDataExportDetails({errors}, 403);
  35. render(<DataDownload params={mockRouteParams} />);
  36. expect(screen.getByText('403 -')).toBeInTheDocument(); // Either the code or the mock is mistaken about the data return format
  37. });
  38. it("should render the 'Early' view when appropriate", function () {
  39. const status = DownloadStatus.Early;
  40. getDataExportDetails({status});
  41. render(<DataDownload params={mockRouteParams} />);
  42. expect(
  43. screen.getByText(textWithMarkupMatcher('What are you doing here?'))
  44. ).toBeInTheDocument();
  45. expect(screen.getByText(/were you invited/)).toBeInTheDocument();
  46. });
  47. it("should render the 'Expired' view when appropriate", function () {
  48. const status = DownloadStatus.Expired;
  49. const response = {status, query: {type: ExportQueryType.IssuesByTag}};
  50. getDataExportDetails(response);
  51. render(<DataDownload params={mockRouteParams} />);
  52. expect(screen.getByText('This is awkward.')).toBeInTheDocument();
  53. expect(screen.getByRole('button', {name: 'Start a New Download'})).toHaveAttribute(
  54. 'href',
  55. `/organizations/${mockRouteParams.orgId}/issues/`
  56. );
  57. });
  58. it("should render the 'Valid' view when appropriate", function () {
  59. const status = DownloadStatus.Valid;
  60. getDataExportDetails({dateExpired, status});
  61. render(<DataDownload params={mockRouteParams} />);
  62. expect(screen.getByText('All done.')).toBeInTheDocument();
  63. expect(screen.getByRole('button', {name: 'Download CSV'})).toHaveAttribute(
  64. 'href',
  65. `/api/0/organizations/${mockRouteParams.orgId}/data-export/${mockRouteParams.dataExportId}/?download=true`
  66. );
  67. expect(
  68. screen.getByText(
  69. textWithMarkupMatcher("That link won't last forever — it expires:Oct 17, 2:41 AM")
  70. )
  71. ).toBeInTheDocument();
  72. });
  73. it('should render the Open in Discover button when needed', function () {
  74. const status = DownloadStatus.Valid;
  75. getDataExportDetails({
  76. dateExpired,
  77. status,
  78. query: {
  79. type: ExportQueryType.Discover,
  80. info: {},
  81. },
  82. });
  83. render(<DataDownload params={mockRouteParams} />);
  84. expect(screen.getByRole('button', {name: 'Open in Discover'})).toBeInTheDocument();
  85. });
  86. it('should not render the Open in Discover button when not needed', function () {
  87. const status = DownloadStatus.Valid;
  88. getDataExportDetails({
  89. dateExpired,
  90. status,
  91. query: {
  92. type: ExportQueryType.IssuesByTag,
  93. info: {},
  94. },
  95. });
  96. render(<DataDownload params={mockRouteParams} />);
  97. expect(
  98. screen.queryByRole('button', {name: 'Open in Discover'})
  99. ).not.toBeInTheDocument();
  100. });
  101. });