multipart-uploader.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { MultipartUploader, UploadStatus } from './multipart-uploader';
  2. class MockMultipartUploader extends MultipartUploader {
  3. async initUpload(): Promise<void> {
  4. return;
  5. }
  6. async uploadPart(part: Buffer, partNumber: number): Promise<void> {
  7. return;
  8. }
  9. async completeUpload(): Promise<void> {
  10. return;
  11. }
  12. async abortUpload(): Promise<void> {
  13. return;
  14. }
  15. async getUploadedFileSize(): Promise<number> {
  16. return 0;
  17. }
  18. // Expose the protected method for testing
  19. testValidateUploadStatus(desired: UploadStatus): void {
  20. this.validateUploadStatus(desired);
  21. }
  22. setCurrentStatus(status: UploadStatus): void {
  23. this.currentStatus = status;
  24. }
  25. }
  26. describe('MultipartUploader', () => {
  27. let uploader: MockMultipartUploader;
  28. beforeEach(() => {
  29. uploader = new MockMultipartUploader('test-upload-key', 10485760);
  30. });
  31. describe('validateUploadStatus', () => {
  32. describe('When current status is equal to desired status', () => {
  33. it('should not throw error', () => {
  34. uploader.setCurrentStatus(UploadStatus.ABORTED);
  35. expect(() =>
  36. uploader.testValidateUploadStatus(UploadStatus.ABORTED),
  37. ).not.toThrow();
  38. });
  39. });
  40. describe('When current status is not equal to desired status', () => {
  41. const cases = [
  42. {
  43. current: UploadStatus.BEFORE_INIT,
  44. desired: UploadStatus.IN_PROGRESS,
  45. errorMessage: 'Multipart upload has not been initiated',
  46. },
  47. {
  48. current: UploadStatus.BEFORE_INIT,
  49. desired: UploadStatus.COMPLETED,
  50. errorMessage: 'Multipart upload has not been initiated',
  51. },
  52. {
  53. current: UploadStatus.BEFORE_INIT,
  54. desired: UploadStatus.ABORTED,
  55. errorMessage: 'Multipart upload has not been initiated',
  56. },
  57. {
  58. current: UploadStatus.IN_PROGRESS,
  59. desired: UploadStatus.BEFORE_INIT,
  60. errorMessage: 'Multipart upload is already in progress',
  61. },
  62. {
  63. current: UploadStatus.IN_PROGRESS,
  64. desired: UploadStatus.COMPLETED,
  65. errorMessage: 'Multipart upload is still in progress',
  66. },
  67. {
  68. current: UploadStatus.IN_PROGRESS,
  69. desired: UploadStatus.ABORTED,
  70. errorMessage: 'Multipart upload is still in progress',
  71. },
  72. {
  73. current: UploadStatus.COMPLETED,
  74. desired: UploadStatus.BEFORE_INIT,
  75. errorMessage: 'Multipart upload has already been completed',
  76. },
  77. {
  78. current: UploadStatus.COMPLETED,
  79. desired: UploadStatus.IN_PROGRESS,
  80. errorMessage: 'Multipart upload has already been completed',
  81. },
  82. {
  83. current: UploadStatus.COMPLETED,
  84. desired: UploadStatus.ABORTED,
  85. errorMessage: 'Multipart upload has already been completed',
  86. },
  87. {
  88. current: UploadStatus.ABORTED,
  89. desired: UploadStatus.BEFORE_INIT,
  90. errorMessage: 'Multipart upload has been aborted',
  91. },
  92. {
  93. current: UploadStatus.ABORTED,
  94. desired: UploadStatus.IN_PROGRESS,
  95. errorMessage: 'Multipart upload has been aborted',
  96. },
  97. {
  98. current: UploadStatus.ABORTED,
  99. desired: UploadStatus.COMPLETED,
  100. errorMessage: 'Multipart upload has been aborted',
  101. },
  102. ];
  103. describe.each(
  104. cases,
  105. )('When current status is $current and desired status is $desired', ({
  106. current,
  107. desired,
  108. errorMessage,
  109. }) => {
  110. beforeEach(() => {
  111. uploader.setCurrentStatus(current);
  112. });
  113. it(`should throw expected error: "${errorMessage}"`, () => {
  114. expect(() => uploader.testValidateUploadStatus(desired)).toThrow(
  115. errorMessage,
  116. );
  117. });
  118. });
  119. });
  120. });
  121. });