1 import { ProgressBarStatus } from '../components/TransferManager/ProgressBar';
2 import type { Transfer, TransfersStats } from '../components/TransferManager/transfer';
3 import { TransferState } from '../components/TransferManager/transfer';
15 isTransferInitializing,
22 describe('trasfer utils', () => {
23 const allStatesList = [
24 TransferState.Canceled,
25 TransferState.Skipped,
28 TransferState.Finalizing,
29 TransferState.Initializing,
31 TransferState.Pending,
32 TransferState.Progress,
35 describe('isTransferFinished', () => {
36 [TransferState.Error, TransferState.Canceled, TransferState.Done].forEach((state) => {
37 it(`should return true for transfer state ${state}`, () => {
38 expect(isTransferFinished({ state })).toBeTruthy();
45 ![TransferState.Error, TransferState.Canceled, TransferState.Skipped, TransferState.Done].includes(
50 it(`should return flase for transfer state ${state}`, () => {
51 expect(isTransferFinished({ state })).toBeFalsy();
56 describe('isTransferActive', () => {
57 [TransferState.Pending, TransferState.Progress, TransferState.Initializing, TransferState.Finalizing].forEach(
59 it(`should return true for transfer state ${state}`, () => {
60 expect(isTransferActive({ state })).toBeTruthy();
69 TransferState.Pending,
70 TransferState.Progress,
71 TransferState.Initializing,
72 TransferState.Finalizing,
76 it(`should return flase for transfer state ${state}`, () => {
77 expect(isTransferActive({ state })).toBeFalsy();
82 describe('isTransferFailed', () => {
83 [TransferState.Canceled, TransferState.Error].forEach((state) => {
84 it(`should return true for transfer state ${state}`, () => {
85 expect(isTransferFailed({ state })).toBeTruthy();
90 .filter((state) => ![TransferState.Canceled, TransferState.Error].includes(state))
92 it(`should return flase for transfer state ${state}`, () => {
93 expect(isTransferFailed({ state })).toBeFalsy();
98 describe('isTransferDone', () => {
99 it(`should return true for transfer state ${TransferState.Done}`, () => {
100 expect(isTransferDone({ state: TransferState.Done })).toBeTruthy();
104 .filter((state) => state !== TransferState.Done)
105 .forEach((state) => {
106 it(`should return flase for transfer state ${state}`, () => {
107 expect(isTransferDone({ state })).toBeFalsy();
112 describe('isTransferError', () => {
113 it(`should return true for transfer state ${TransferState.Error}`, () => {
114 expect(isTransferError({ state: TransferState.Error })).toBeTruthy();
118 .filter((state) => state !== TransferState.Error)
119 .forEach((state) => {
120 it(`should return flase for transfer state ${state}`, () => {
121 expect(isTransferError({ state })).toBeFalsy();
126 describe('isTransferCanceled', () => {
127 it(`should return true for transfer state ${TransferState.Canceled}`, () => {
128 expect(isTransferCanceled({ state: TransferState.Canceled })).toBeTruthy();
132 .filter((state) => state !== TransferState.Canceled)
133 .forEach((state) => {
134 it(`should return flase for transfer state ${state}`, () => {
135 expect(isTransferCanceled({ state })).toBeFalsy();
140 describe('isTransferSkipped', () => {
141 it(`should return true for transfer state ${TransferState.Skipped}`, () => {
142 expect(isTransferSkipped({ state: TransferState.Skipped })).toBe(true);
146 .filter((state) => state !== TransferState.Skipped)
147 .forEach((state) => {
148 it(`should return false for transfer state ${state}`, () => {
149 expect(isTransferSkipped({ state })).toBe(false);
154 describe('isTransferProgress', () => {
155 it(`should return true for transfer state ${TransferState.Progress}`, () => {
156 expect(isTransferProgress({ state: TransferState.Progress })).toBeTruthy();
160 .filter((state) => state !== TransferState.Progress)
161 .forEach((state) => {
162 it(`should return flase for transfer state ${state}`, () => {
163 expect(isTransferProgress({ state })).toBeFalsy();
168 describe('isTransferInitializing', () => {
169 it(`should return true for transfer state ${TransferState.Initializing}`, () => {
170 expect(isTransferInitializing({ state: TransferState.Initializing })).toBeTruthy();
174 .filter((state) => state !== TransferState.Initializing)
175 .forEach((state) => {
176 it(`should return flase for transfer state ${state}`, () => {
177 expect(isTransferInitializing({ state })).toBeFalsy();
182 describe('isTransferPaused', () => {
183 it(`should return true for transfer state ${TransferState.Paused}`, () => {
184 expect(isTransferPaused({ state: TransferState.Paused })).toBeTruthy();
188 .filter((state) => state !== TransferState.Paused)
189 .forEach((state) => {
190 it(`should return flase for transfer state ${state}`, () => {
191 expect(isTransferPaused({ state })).toBeFalsy();
196 describe('isTransferFinalizing', () => {
197 it(`should return true for transfer state ${TransferState.Finalizing}`, () => {
198 expect(isTransferFinalizing({ state: TransferState.Finalizing })).toBeTruthy();
202 .filter((state) => state !== TransferState.Finalizing)
203 .forEach((state) => {
204 it(`should return flase for transfer state ${state}`, () => {
205 expect(isTransferFinalizing({ state })).toBeFalsy();
210 describe('isTransferCancelError', () => {
211 ['TransferCancel', 'AbortError'].forEach((name) => {
212 it(`should return true for error with name ${name}`, () => {
215 message: `${name} error accured.`,
217 expect(isTransferCancelError(error)).toBeTruthy();
221 ['TypeError', 'SyntaxError'].forEach((name) => {
222 it(`should return false for error with name ${name}`, () => {
225 message: `${name} error accured.`,
227 expect(isTransferCancelError(error)).toBeFalsy();
232 describe('isTransferRetry', () => {
233 ['TransferRetry'].forEach((name) => {
234 it(`should return true for error with name ${name}`, () => {
237 message: `${name} error accured.`,
239 expect(isTransferRetry(error)).toBeTruthy();
243 ['TypeError', 'SyntaxError'].forEach((name) => {
244 it(`should return false for error with name ${name}`, () => {
247 message: `${name} error accured.`,
249 expect(isTransferRetry(error)).toBeFalsy();
254 describe('getProgressBarStatus', () => {
256 { state: TransferState.Done, status: ProgressBarStatus.Success },
257 { state: TransferState.Canceled, status: ProgressBarStatus.Disabled },
258 { state: TransferState.Error, status: ProgressBarStatus.Error },
259 { state: TransferState.Finalizing, status: ProgressBarStatus.Running },
260 { state: TransferState.Initializing, status: ProgressBarStatus.Running },
261 { state: TransferState.Paused, status: ProgressBarStatus.Running },
262 { state: TransferState.Pending, status: ProgressBarStatus.Running },
263 { state: TransferState.Progress, status: ProgressBarStatus.Running },
264 ].forEach(({ state, status }) => {
265 it(`should return progress bar status ${status} for transfer state ${state}`, () => {
266 expect(getProgressBarStatus(state)).toEqual(status);
271 it(`calculateProgress should calculate progress of active trasfers`, () => {
272 const size1 = 734003200;
273 const size2 = 83404340;
274 const progress1 = 279297577;
275 const progress2 = 8340324;
276 const progress = Math.floor(100 * ((progress1 + progress2) / (size1 + size2 || 1)));
277 const stats: TransfersStats = {
278 'drive-transfers-5740': {
279 averageSpeed: 2588514,
282 'drive-transfers-7456': {
283 averageSpeed: 734032,
288 const transfers: Transfer[] = [
290 id: 'drive-transfers-5740',
293 mimeType: 'application/octet-stream',
294 filename: 'Pregenerated File',
296 state: TransferState.Finalizing,
299 id: 'drive-transfers-7456',
302 mimeType: 'application/octet-stream',
303 filename: 'Pregenerated File 2',
305 state: TransferState.Progress,
309 expect(calculateProgress(stats, transfers)).toEqual(progress);