dataDownload.spec.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {ExportQueryType} from 'sentry/components/dataExport';
  3. import DataDownload, {DownloadStatus} from 'sentry/views/dataExport/dataDownload';
  4. describe('DataDownload', function () {
  5. beforeEach(MockApiClient.clearMockResponses);
  6. const dateExpired = new Date();
  7. const organization = TestStubs.Organization();
  8. const mockRouteParams = {
  9. orgId: organization.slug,
  10. dataExportId: 721,
  11. };
  12. const getDataExportDetails = (body, statusCode = 200) =>
  13. MockApiClient.addMockResponse({
  14. url: `/organizations/${mockRouteParams.orgId}/data-export/${mockRouteParams.dataExportId}/`,
  15. body,
  16. statusCode,
  17. });
  18. it('should send a request to the data export endpoint', function () {
  19. const getValid = getDataExportDetails(DownloadStatus.Valid);
  20. mountWithTheme(<DataDownload params={mockRouteParams} />);
  21. expect(getValid).toHaveBeenCalled();
  22. });
  23. it("should render the 'Error' view when appropriate", function () {
  24. const errors = {
  25. download: {
  26. status: 403,
  27. statusText: 'Forbidden',
  28. responseJSON: {
  29. detail: 'You are not allowed',
  30. },
  31. },
  32. };
  33. getDataExportDetails({errors}, 403);
  34. const wrapper = mountWithTheme(<DataDownload params={mockRouteParams} />);
  35. expect(wrapper.state('errors')).toBeDefined();
  36. expect(wrapper.state('errors').download.status).toBe(403);
  37. });
  38. it("should render the 'Early' view when appropriate", function () {
  39. const status = DownloadStatus.Early;
  40. getDataExportDetails({status});
  41. const wrapper = mountWithTheme(<DataDownload params={mockRouteParams} />);
  42. expect(wrapper.state('download')).toEqual({status});
  43. expect(wrapper.state('download').dateExpired).toBeUndefined();
  44. expect(wrapper.find('Header').text()).toBe('What are you doing here?');
  45. });
  46. it("should render the 'Expired' view when appropriate", function () {
  47. const status = DownloadStatus.Expired;
  48. const response = {status, query: {type: ExportQueryType.IssuesByTag}};
  49. getDataExportDetails(response);
  50. const wrapper = mountWithTheme(<DataDownload params={mockRouteParams} />);
  51. expect(wrapper.state('download')).toEqual(response);
  52. expect(wrapper.state('download').dateExpired).toBeUndefined();
  53. expect(wrapper.find('Header').text()).toBe('This is awkward.');
  54. const buttonWrapper = wrapper.find('a[aria-label="Start a New Download"]');
  55. expect(buttonWrapper.prop('href')).toBe(
  56. `/organizations/${mockRouteParams.orgId}/issues/`
  57. );
  58. });
  59. it("should render the 'Valid' view when appropriate", function () {
  60. const status = DownloadStatus.Valid;
  61. getDataExportDetails({dateExpired, status});
  62. const wrapper = mountWithTheme(<DataDownload params={mockRouteParams} />);
  63. expect(wrapper.state('download')).toEqual({dateExpired, status});
  64. expect(wrapper.find('Header').text()).toBe('All done.');
  65. const buttonWrapper = wrapper.find('a[aria-label="Download CSV"]');
  66. expect(buttonWrapper.text()).toBe('Download CSV');
  67. expect(buttonWrapper.prop('href')).toBe(
  68. `/api/0/organizations/${mockRouteParams.orgId}/data-export/${mockRouteParams.dataExportId}/?download=true`
  69. );
  70. expect(wrapper.find('DateTime').prop('date')).toEqual(new Date(dateExpired));
  71. });
  72. it('should render the Open in Discover button when needed', function () {
  73. const status = DownloadStatus.Valid;
  74. getDataExportDetails({
  75. dateExpired,
  76. status,
  77. query: {
  78. type: ExportQueryType.Discover,
  79. info: {},
  80. },
  81. });
  82. const wrapper = mountWithTheme(<DataDownload params={mockRouteParams} />);
  83. const buttonWrapper = wrapper.find('button[aria-label="Open in Discover"]');
  84. expect(buttonWrapper.exists()).toBeTruthy();
  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. const wrapper = mountWithTheme(<DataDownload params={mockRouteParams} />);
  97. const buttonWrapper = wrapper.find('button[aria-label="Open in Discover"]');
  98. expect(buttonWrapper.exists()).toBeFalsy();
  99. });
  100. });