@@ -14,33 +14,52 @@ import GasReducer, {
14
14
gasEstimatesLoadingStarted ,
15
15
gasEstimatesLoadingFinished ,
16
16
setPricesAndTimeEstimates ,
17
+ fetchGasEstimates ,
18
+ setApiEstimatesLastRetrieved ,
17
19
} from '../gas.duck.js'
18
20
19
21
describe ( 'Gas Duck' , ( ) => {
20
22
let tempFetch
21
- const fetchStub = sinon . stub ( ) . returns ( new Promise ( resolve => resolve ( {
22
- json : ( ) => new Promise ( resolve => resolve ( {
23
- average : 'mockAverage' ,
24
- avgWait : 'mockAvgWait' ,
25
- block_time : 'mockBlock_time' ,
26
- blockNum : 'mockBlockNum' ,
27
- fast : 'mockFast' ,
28
- fastest : 'mockFastest' ,
29
- fastestWait : 'mockFastestWait' ,
30
- fastWait : 'mockFastWait' ,
31
- safeLow : 'mockSafeLow' ,
32
- safeLowWait : 'mockSafeLowWait' ,
33
- speed : 'mockSpeed' ,
34
- } ) ) ,
35
- } ) ) )
23
+ let tempDateNow
24
+ const mockEthGasApiResponse = {
25
+ average : 'mockAverage' ,
26
+ avgWait : 'mockAvgWait' ,
27
+ block_time : 'mockBlock_time' ,
28
+ blockNum : 'mockBlockNum' ,
29
+ fast : 'mockFast' ,
30
+ fastest : 'mockFastest' ,
31
+ fastestWait : 'mockFastestWait' ,
32
+ fastWait : 'mockFastWait' ,
33
+ safeLow : 'mockSafeLow' ,
34
+ safeLowWait : 'mockSafeLowWait' ,
35
+ speed : 'mockSpeed' ,
36
+ }
37
+ const mockPredictTableResponse = [
38
+ { expectedTime : 100 , expectedWait : 10 , gasprice : 1 , somethingElse : 'foobar' } ,
39
+ { expectedTime : 50 , expectedWait : 5 , gasprice : 2 , somethingElse : 'foobar' } ,
40
+ { expectedTime : 20 , expectedWait : 4 , gasprice : 4 , somethingElse : 'foobar' } ,
41
+ { expectedTime : 10 , expectedWait : 2 , gasprice : 10 , somethingElse : 'foobar' } ,
42
+ { expectedTime : 1 , expectedWait : 0.5 , gasprice : 20 , somethingElse : 'foobar' } ,
43
+ ]
44
+ const fetchStub = sinon . stub ( ) . callsFake ( ( url ) => new Promise ( resolve => {
45
+ const dataToResolve = url . match ( / e t h g a s A P I / )
46
+ ? mockEthGasApiResponse
47
+ : mockPredictTableResponse
48
+ resolve ( {
49
+ json : ( ) => new Promise ( resolve => resolve ( dataToResolve ) ) ,
50
+ } )
51
+ } ) )
36
52
37
53
beforeEach ( ( ) => {
38
54
tempFetch = global . fetch
55
+ tempDateNow = global . Date . now
39
56
global . fetch = fetchStub
57
+ global . Date . now = ( ) => 2000000
40
58
} )
41
59
42
60
afterEach ( ( ) => {
43
61
global . fetch = tempFetch
62
+ global . Date . now = tempDateNow
44
63
} )
45
64
46
65
const mockState = {
@@ -70,6 +89,7 @@ describe('Gas Duck', () => {
70
89
errors : { } ,
71
90
gasEstimatesLoading : true ,
72
91
priceAndTimeEstimates : [ ] ,
92
+ priceAndTimeEstimatesLastRetrieved : 0 ,
73
93
74
94
}
75
95
const BASIC_GAS_ESTIMATE_LOADING_FINISHED = 'metamask/gas/BASIC_GAS_ESTIMATE_LOADING_FINISHED'
@@ -83,6 +103,7 @@ describe('Gas Duck', () => {
83
103
const SET_CUSTOM_GAS_PRICE = 'metamask/gas/SET_CUSTOM_GAS_PRICE'
84
104
const SET_CUSTOM_GAS_TOTAL = 'metamask/gas/SET_CUSTOM_GAS_TOTAL'
85
105
const SET_PRICE_AND_TIME_ESTIMATES = 'metamask/gas/SET_PRICE_AND_TIME_ESTIMATES'
106
+ const SET_API_ESTIMATES_LAST_RETRIEVED = 'metamask/gas/SET_API_ESTIMATES_LAST_RETRIEVED'
86
107
87
108
describe ( 'GasReducer()' , ( ) => {
88
109
it ( 'should initialize state' , ( ) => {
@@ -193,6 +214,16 @@ describe('Gas Duck', () => {
193
214
)
194
215
} )
195
216
217
+ it ( 'should set priceAndTimeEstimatesLastRetrieved when receivinga SET_API_ESTIMATES_LAST_RETRIEVED action' , ( ) => {
218
+ assert . deepEqual (
219
+ GasReducer ( mockState , {
220
+ type : SET_API_ESTIMATES_LAST_RETRIEVED ,
221
+ value : 1500000000000 ,
222
+ } ) ,
223
+ Object . assign ( { priceAndTimeEstimatesLastRetrieved : 1500000000000 } , mockState . gas )
224
+ )
225
+ } )
226
+
196
227
it ( 'should set errors when receiving a SET_CUSTOM_GAS_ERRORS action' , ( ) => {
197
228
assert . deepEqual (
198
229
GasReducer ( mockState , {
@@ -279,6 +310,75 @@ describe('Gas Duck', () => {
279
310
} )
280
311
} )
281
312
313
+ describe ( 'fetchGasEstimates' , ( ) => {
314
+ const mockDistpatch = sinon . spy ( )
315
+ it ( 'should call fetch with the expected params' , async ( ) => {
316
+ global . fetch . resetHistory ( )
317
+ await fetchGasEstimates ( 5 ) ( mockDistpatch , ( ) => ( { gas : Object . assign (
318
+ { } ,
319
+ initState ,
320
+ { priceAndTimeEstimatesLastRetrieved : 1000000 }
321
+ ) } ) )
322
+ assert . deepEqual (
323
+ mockDistpatch . getCall ( 0 ) . args ,
324
+ [ { type : GAS_ESTIMATE_LOADING_STARTED } ]
325
+ )
326
+ assert . deepEqual (
327
+ global . fetch . getCall ( 0 ) . args ,
328
+ [
329
+ 'https://ethgasstation.info/json/predictTable.json' ,
330
+ {
331
+ 'headers' : { } ,
332
+ 'referrer' : 'http://ethgasstation.info/json/' ,
333
+ 'referrerPolicy' : 'no-referrer-when-downgrade' ,
334
+ 'body' : null ,
335
+ 'method' : 'GET' ,
336
+ 'mode' : 'cors' ,
337
+ } ,
338
+ ]
339
+ )
340
+
341
+ assert . deepEqual (
342
+ mockDistpatch . getCall ( 1 ) . args ,
343
+ [ { type : SET_API_ESTIMATES_LAST_RETRIEVED , value : 2000000 } ]
344
+ )
345
+
346
+ assert . deepEqual (
347
+ mockDistpatch . getCall ( 2 ) . args ,
348
+ [ {
349
+ type : SET_PRICE_AND_TIME_ESTIMATES ,
350
+ value : [
351
+ {
352
+ expectedTime : '25' ,
353
+ expectedWait : 5 ,
354
+ gasprice : 2 ,
355
+ } ,
356
+ {
357
+ expectedTime : '20' ,
358
+ expectedWait : 4 ,
359
+ gasprice : 4 ,
360
+ } ,
361
+ {
362
+ expectedTime : '10' ,
363
+ expectedWait : 2 ,
364
+ gasprice : 10 ,
365
+ } ,
366
+ {
367
+ expectedTime : '2.5' ,
368
+ expectedWait : 0.5 ,
369
+ gasprice : 20 ,
370
+ } ,
371
+ ] ,
372
+
373
+ } ]
374
+ )
375
+ assert . deepEqual (
376
+ mockDistpatch . getCall ( 3 ) . args ,
377
+ [ { type : GAS_ESTIMATE_LOADING_FINISHED } ]
378
+ )
379
+ } )
380
+ } )
381
+
282
382
describe ( 'gasEstimatesLoadingStarted' , ( ) => {
283
383
it ( 'should create the correct action' , ( ) => {
284
384
assert . deepEqual (
@@ -351,6 +451,15 @@ describe('Gas Duck', () => {
351
451
} )
352
452
} )
353
453
454
+ describe ( 'setApiEstimatesLastRetrieved' , ( ) => {
455
+ it ( 'should create the correct action' , ( ) => {
456
+ assert . deepEqual (
457
+ setApiEstimatesLastRetrieved ( 1234 ) ,
458
+ { type : SET_API_ESTIMATES_LAST_RETRIEVED , value : 1234 }
459
+ )
460
+ } )
461
+ } )
462
+
354
463
describe ( 'resetCustomGasState' , ( ) => {
355
464
it ( 'should create the correct action' , ( ) => {
356
465
assert . deepEqual (
0 commit comments