@@ -13,8 +13,10 @@ import {
13
13
fromOption ,
14
14
fromEither ,
15
15
fromPredicate ,
16
+ progress ,
17
+ fromProgressEvent ,
16
18
} from '../remote-data' ;
17
- import { identity , compose } from 'fp-ts/lib/function' ;
19
+ import { identity , compose , Function1 } from 'fp-ts/lib/function' ;
18
20
import { sequence , traverse } from 'fp-ts/lib/Traversable' ;
19
21
import { none , option , some } from 'fp-ts/lib/Option' ;
20
22
import { array } from 'fp-ts/lib/Array' ;
@@ -30,6 +32,7 @@ describe('RemoteData', () => {
30
32
const pendingRD : RemoteData < string , number > = pending ;
31
33
const successRD : RemoteData < string , number > = success ( 1 ) ;
32
34
const failureRD : RemoteData < string , number > = failure ( 'foo' ) ;
35
+ const progressRD : RemoteData < string , Function1 < number , number > > = progress ( { loaded : 1 , total : none } ) ;
33
36
describe ( 'Functor' , ( ) => {
34
37
describe ( 'should map over value' , ( ) => {
35
38
it ( 'initial' , ( ) => {
@@ -124,24 +127,28 @@ describe('RemoteData', () => {
124
127
it ( 'initial' , ( ) => {
125
128
expect ( initialRD . ap ( initial ) ) . toBe ( initialRD ) ;
126
129
expect ( initialRD . ap ( pending ) ) . toBe ( initialRD ) ;
130
+ expect ( initialRD . ap ( progressRD ) ) . toBe ( initialRD ) ;
127
131
expect ( initialRD . ap ( failedF ) ) . toBe ( initialRD ) ;
128
132
expect ( initialRD . ap ( f ) ) . toBe ( initialRD ) ;
129
133
} ) ;
130
134
it ( 'pending' , ( ) => {
131
135
expect ( pendingRD . ap ( initial ) ) . toBe ( initial ) ;
132
136
expect ( pendingRD . ap ( pending ) ) . toBe ( pendingRD ) ;
137
+ expect ( pendingRD . ap ( progressRD ) ) . toBe ( progressRD ) ;
133
138
expect ( pendingRD . ap ( failedF ) ) . toBe ( pendingRD ) ;
134
139
expect ( pendingRD . ap ( f ) ) . toBe ( pendingRD ) ;
135
140
} ) ;
136
141
it ( 'failure' , ( ) => {
137
142
expect ( failureRD . ap ( initial ) ) . toBe ( initial ) ;
138
143
expect ( failureRD . ap ( pending ) ) . toBe ( pending ) ;
144
+ expect ( failureRD . ap ( progressRD ) ) . toBe ( progressRD ) ;
139
145
expect ( failureRD . ap ( failedF ) ) . toBe ( failedF ) ;
140
146
expect ( failureRD . ap ( f ) ) . toBe ( failureRD ) ;
141
147
} ) ;
142
148
it ( 'success' , ( ) => {
143
149
expect ( successRD . ap ( initial ) ) . toBe ( initial ) ;
144
150
expect ( successRD . ap ( pending ) ) . toBe ( pending ) ;
151
+ expect ( successRD . ap ( progressRD ) ) . toBe ( progressRD ) ;
145
152
expect ( successRD . ap ( failedF ) ) . toBe ( failedF ) ;
146
153
expect ( successRD . ap ( f ) ) . toEqual ( success ( double ( 1 ) ) ) ;
147
154
} ) ;
@@ -341,6 +348,33 @@ describe('RemoteData', () => {
341
348
expect ( concat ( successRD , failureRD ) ) . toBe ( successRD ) ;
342
349
expect ( concat ( success ( 1 ) , success ( 1 ) ) ) . toEqual ( success ( semigroupSum . concat ( 1 , 1 ) ) ) ;
343
350
} ) ;
351
+ describe ( 'progress' , ( ) => {
352
+ it ( 'should concat pendings without progress' , ( ) => {
353
+ expect ( concat ( pending , pending ) ) . toEqual ( pending ) ;
354
+ } ) ;
355
+ it ( 'should concat pending and progress' , ( ) => {
356
+ const withProgress : RemoteData < string , number > = progress ( { loaded : 1 , total : none } ) ;
357
+ expect ( concat ( pending , withProgress ) ) . toBe ( withProgress ) ;
358
+ } ) ;
359
+ it ( 'should concat progress without total' , ( ) => {
360
+ const withProgress : RemoteData < string , number > = progress ( { loaded : 1 , total : none } ) ;
361
+ expect ( concat ( withProgress , withProgress ) ) . toEqual ( progress ( { loaded : 2 , total : none } ) ) ;
362
+ } ) ;
363
+ it ( 'should concat progress without total and progress with total' , ( ) => {
364
+ const withProgress : RemoteData < string , number > = progress ( { loaded : 1 , total : none } ) ;
365
+ const withProgressAndTotal : RemoteData < string , number > = progress ( { loaded : 1 , total : some ( 2 ) } ) ;
366
+ expect ( concat ( withProgress , withProgressAndTotal ) ) . toEqual ( progress ( { loaded : 2 , total : none } ) ) ;
367
+ } ) ;
368
+ it ( 'should combine progresses with total' , ( ) => {
369
+ const expected = progress ( {
370
+ loaded : ( 2 * 10 + 2 * 30 ) / ( 40 * 40 ) ,
371
+ total : some ( 10 + 30 ) ,
372
+ } ) ;
373
+ expect (
374
+ concat ( progress ( { loaded : 2 , total : some ( 10 ) } ) , progress ( { loaded : 2 , total : some ( 30 ) } ) ) ,
375
+ ) . toEqual ( expected ) ;
376
+ } ) ;
377
+ } ) ;
344
378
} ) ;
345
379
} ) ;
346
380
describe ( 'Monoid' , ( ) => {
@@ -378,6 +412,41 @@ describe('RemoteData', () => {
378
412
expect ( combine . apply ( null , values ) ) . toEqual ( failure ( 'bar' ) ) ;
379
413
expect ( combine . apply ( null , values . reverse ( ) ) ) . toEqual ( failure ( 'bar' ) ) ;
380
414
} ) ;
415
+ describe ( 'progress' , ( ) => {
416
+ it ( 'should combine pendings without progress' , ( ) => {
417
+ const values = [ pending , pending ] ;
418
+ expect ( combine . apply ( null , values ) ) . toBe ( pending ) ;
419
+ expect ( combine . apply ( null , values . reverse ( ) ) ) . toBe ( pending ) ;
420
+ } ) ;
421
+ it ( 'should combine pending and progress' , ( ) => {
422
+ const withProgress = progress ( { loaded : 1 , total : none } ) ;
423
+ const values = [ pending , withProgress ] ;
424
+ expect ( combine . apply ( null , values ) ) . toBe ( withProgress ) ;
425
+ expect ( combine . apply ( null , values . reverse ( ) ) ) . toBe ( withProgress ) ;
426
+ } ) ;
427
+ it ( 'should combine progress without total' , ( ) => {
428
+ const withProgress = progress ( { loaded : 1 , total : none } ) ;
429
+ const values = [ withProgress , withProgress ] ;
430
+ expect ( combine . apply ( null , values ) ) . toEqual ( progress ( { loaded : 2 , total : none } ) ) ;
431
+ expect ( combine . apply ( null , values . reverse ( ) ) ) . toEqual ( progress ( { loaded : 2 , total : none } ) ) ;
432
+ } ) ;
433
+ it ( 'should combine progress without total and progress with total' , ( ) => {
434
+ const withProgress = progress ( { loaded : 1 , total : none } ) ;
435
+ const withProgressAndTotal = progress ( { loaded : 1 , total : some ( 2 ) } ) ;
436
+ const values = [ withProgress , withProgressAndTotal ] ;
437
+ expect ( combine . apply ( null , values ) ) . toEqual ( progress ( { loaded : 2 , total : none } ) ) ;
438
+ expect ( combine . apply ( null , values . reverse ( ) ) ) . toEqual ( progress ( { loaded : 2 , total : none } ) ) ;
439
+ } ) ;
440
+ it ( 'should combine progresses with total' , ( ) => {
441
+ const values = [ progress ( { loaded : 2 , total : some ( 10 ) } ) , progress ( { loaded : 2 , total : some ( 30 ) } ) ] ;
442
+ const expected = progress ( {
443
+ loaded : ( 2 * 10 + 2 * 30 ) / ( 40 * 40 ) ,
444
+ total : some ( 10 + 30 ) ,
445
+ } ) ;
446
+ expect ( combine . apply ( null , values ) ) . toEqual ( expected ) ;
447
+ expect ( combine . apply ( null , values . reverse ( ) ) ) . toEqual ( expected ) ;
448
+ } ) ;
449
+ } ) ;
381
450
} ) ;
382
451
describe ( 'fromOption' , ( ) => {
383
452
const error = new Error ( 'foo' ) ;
@@ -405,6 +474,17 @@ describe('RemoteData', () => {
405
474
expect ( factory ( true ) ) . toEqual ( success ( true ) ) ;
406
475
} ) ;
407
476
} ) ;
477
+ describe ( 'fromProgressEvent' , ( ) => {
478
+ const e = new ProgressEvent ( 'test' ) ;
479
+ it ( 'lengthComputable === false' , ( ) => {
480
+ expect ( fromProgressEvent ( { ...e , loaded : 123 } ) ) . toEqual ( progress ( { loaded : 123 , total : none } ) ) ;
481
+ } ) ;
482
+ it ( 'lengthComputable === true' , ( ) => {
483
+ expect ( fromProgressEvent ( { ...e , loaded : 123 , lengthComputable : true , total : 1000 } ) ) . toEqual (
484
+ progress ( { loaded : 123 , total : some ( 1000 ) } ) ,
485
+ ) ;
486
+ } ) ;
487
+ } ) ;
408
488
} ) ;
409
489
describe ( 'instance methods' , ( ) => {
410
490
describe ( 'getOrElse' , ( ) => {
0 commit comments