1- import { runInAction , values } from 'mobx' ;
1+ import { keys , runInAction , values } from 'mobx' ;
22import * as AUCT from 'types/generated/auctioneer_pb' ;
33import { grpc } from '@improbable-eng/grpc-web' ;
44import { waitFor } from '@testing-library/react' ;
55import * as config from 'config' ;
6- import { hex } from 'util/strings' ;
6+ import { b64 , hex } from 'util/strings' ;
77import { injectIntoGrpcUnary } from 'util/tests' ;
88import {
99 poolBatchSnapshot ,
@@ -30,8 +30,9 @@ describe('BatchStore', () => {
3030 grpcMock . unary . mockImplementation ( ( desc , opts ) => {
3131 let res : any ;
3232 if ( desc . methodName === 'BatchSnapshots' ) {
33+ const count = ( opts . request . toObject ( ) as any ) . numBatchesBack ;
3334 res = {
34- batchesList : [ ...Array ( 20 ) ] . map ( ( _ , i ) => ( {
35+ batchesList : [ ...Array ( count ) ] . map ( ( _ , i ) => ( {
3536 ...poolBatchSnapshot ,
3637 batchId : `${ index + i } -${ poolBatchSnapshot . batchId } ` ,
3738 prevBatchId : `${ index + i } -${ poolBatchSnapshot . prevBatchId } ` ,
@@ -51,38 +52,39 @@ describe('BatchStore', () => {
5152
5253 it ( 'should fetch batches' , async ( ) => {
5354 expect ( store . batches . size ) . toBe ( 0 ) ;
54- expect ( store . orderedIds . length ) . toBe ( 0 ) ;
5555
5656 await store . fetchBatches ( ) ;
5757 expect ( store . batches . size ) . toBe ( BATCH_QUERY_LIMIT ) ;
58- expect ( store . orderedIds . length ) . toBe ( BATCH_QUERY_LIMIT ) ;
5958 } ) ;
6059
6160 it ( 'should append start from the oldest batch when fetching batches multiple times' , async ( ) => {
6261 expect ( store . batches . size ) . toBe ( 0 ) ;
63- expect ( store . orderedIds . length ) . toBe ( 0 ) ;
6462
6563 await store . fetchBatches ( ) ;
6664 expect ( store . batches . size ) . toBe ( BATCH_QUERY_LIMIT ) ;
67- expect ( store . orderedIds . length ) . toBe ( BATCH_QUERY_LIMIT ) ;
6865
6966 // calling a second time should append new batches to the list
7067 await store . fetchBatches ( ) ;
7168 expect ( store . batches . size ) . toBe ( BATCH_QUERY_LIMIT * 2 ) ;
72- expect ( store . orderedIds . length ) . toBe ( BATCH_QUERY_LIMIT * 2 ) ;
7369 } ) ;
7470
7571 it ( 'should handle a number of batches less than the query limit' , async ( ) => {
7672 // mock the BatchSnapshot response to return 5 batches with the last one having a
7773 // blank prevBatchId to signify that there are no more batches available
78- grpcMock . unary . mockImplementation ( ( _ , opts ) => {
79- const res = {
80- batchesList : [ ...Array ( 5 ) ] . map ( ( _ , i ) => ( {
81- ...poolBatchSnapshot ,
82- batchId : `${ i } -${ poolBatchSnapshot . batchId } ` ,
83- prevBatchId : index < 4 ? `${ i } -${ poolBatchSnapshot . prevBatchId } ` : '' ,
84- } ) ) ,
85- } ;
74+ grpcMock . unary . mockImplementation ( ( desc , opts ) => {
75+ let res : any ;
76+ if ( desc . methodName === 'BatchSnapshots' ) {
77+ res = {
78+ batchesList : [ ...Array ( 5 ) ] . map ( ( _ , i ) => ( {
79+ ...poolBatchSnapshot ,
80+ batchId : b64 ( `${ hex ( poolBatchSnapshot . batchId ) } 0${ i } ` ) ,
81+ prevBatchId : i < 4 ? b64 ( `${ hex ( poolBatchSnapshot . prevBatchId ) } 0${ i } ` ) : '' ,
82+ } ) ) ,
83+ } ;
84+ index += BATCH_QUERY_LIMIT ;
85+ } else if ( desc . methodName === 'LeaseDurations' ) {
86+ res = poolLeaseDurations ;
87+ }
8688 opts . onEnd ( {
8789 status : grpc . Code . OK ,
8890 message : { toObject : ( ) => res } ,
@@ -94,11 +96,21 @@ describe('BatchStore', () => {
9496
9597 await store . fetchBatches ( ) ;
9698 expect ( store . batches . size ) . toBe ( 5 ) ;
99+
100+ await store . fetchBatches ( ) ;
101+ expect ( store . batches . size ) . toBe ( 5 ) ;
97102 } ) ;
98103
99104 it ( 'should handle errors when fetching batches' , async ( ) => {
100- grpcMock . unary . mockImplementationOnce ( ( ) => {
101- throw new Error ( 'test-err' ) ;
105+ grpcMock . unary . mockImplementation ( ( desc , opts ) => {
106+ if ( desc . methodName === 'BatchSnapshots' ) {
107+ throw new Error ( 'test-err' ) ;
108+ }
109+ opts . onEnd ( {
110+ status : grpc . Code . OK ,
111+ message : { toObject : ( ) => poolLeaseDurations } ,
112+ } as any ) ;
113+ return undefined as any ;
102114 } ) ;
103115 expect ( rootStore . appView . alerts . size ) . toBe ( 0 ) ;
104116 await store . fetchBatches ( ) ;
@@ -107,8 +119,15 @@ describe('BatchStore', () => {
107119 } ) ;
108120
109121 it ( 'should not show error when last snapshot is not found' , async ( ) => {
110- grpcMock . unary . mockImplementationOnce ( ( ) => {
111- throw new Error ( 'batch snapshot not found' ) ;
122+ grpcMock . unary . mockImplementation ( ( desc , opts ) => {
123+ if ( desc . methodName === 'BatchSnapshots' ) {
124+ throw new Error ( 'batch snapshot not found' ) ;
125+ }
126+ opts . onEnd ( {
127+ status : grpc . Code . OK ,
128+ message : { toObject : ( ) => poolLeaseDurations } ,
129+ } as any ) ;
130+ return undefined as any ;
112131 } ) ;
113132 expect ( rootStore . appView . alerts . size ) . toBe ( 0 ) ;
114133 await store . fetchBatches ( ) ;
@@ -117,20 +136,19 @@ describe('BatchStore', () => {
117136
118137 it ( 'should fetch the latest batch' , async ( ) => {
119138 await store . fetchBatches ( ) ;
120- expect ( store . orderedIds . length ) . toBe ( BATCH_QUERY_LIMIT ) ;
139+ expect ( store . batches . size ) . toBe ( BATCH_QUERY_LIMIT ) ;
121140
122141 // return the same last batch to ensure no new data is added
123142 const lastBatchId = store . sortedBatches [ 0 ] . batchId ;
124143 index -- ;
125144 await store . fetchLatestBatch ( ) ;
126- expect ( store . orderedIds . length ) . toBe ( BATCH_QUERY_LIMIT ) ;
145+ expect ( store . batches . size ) . toBe ( BATCH_QUERY_LIMIT ) ;
127146 expect ( store . sortedBatches [ 0 ] . batchId ) . toBe ( lastBatchId ) ;
128147
129148 // return a new batch as the latest
130149 index = 100 ;
131150 await store . fetchLatestBatch ( ) ;
132- expect ( store . orderedIds . length ) . toBe ( BATCH_QUERY_LIMIT + 1 ) ;
133- expect ( store . orderedIds [ 0 ] ) . toBe ( hex ( `100-${ poolBatchSnapshot . batchId } ` ) ) ;
151+ expect ( store . batches . size ) . toBe ( BATCH_QUERY_LIMIT + 1 ) ;
134152 } ) ;
135153
136154 it ( 'should handle errors when fetching the latest batch' , async ( ) => {
@@ -205,6 +223,28 @@ describe('BatchStore', () => {
205223 expect ( store . nodeTier ) . toBe ( AUCT . NodeTier . TIER_0 ) ;
206224 } ) ;
207225
226+ it ( 'should handle errors when fetching node tier' , async ( ) => {
227+ grpcMock . unary . mockImplementationOnce ( ( ) => {
228+ throw new Error ( 'test-err' ) ;
229+ } ) ;
230+ expect ( rootStore . appView . alerts . size ) . toBe ( 0 ) ;
231+ await store . fetchNodeTier ( ) ;
232+ expect ( rootStore . appView . alerts . size ) . toBe ( 1 ) ;
233+ expect ( values ( rootStore . appView . alerts ) [ 0 ] . message ) . toBe ( 'test-err' ) ;
234+ } ) ;
235+
236+ it ( 'should set the active market' , async ( ) => {
237+ expect ( store . selectedLeaseDuration ) . toBe ( 0 ) ;
238+ await store . fetchBatches ( ) ;
239+ expect ( store . selectedLeaseDuration ) . toBe ( 2016 ) ;
240+ expect ( keys ( store . leaseDurations ) ) . toEqual ( [ 2016 , 4032 , 6048 , 8064 ] ) ;
241+ store . setActiveMarket ( 4032 ) ;
242+ expect ( store . selectedLeaseDuration ) . toBe ( 4032 ) ;
243+ store . setActiveMarket ( 5000 ) ;
244+ expect ( store . selectedLeaseDuration ) . toBe ( 4032 ) ;
245+ expect ( rootStore . appView . alerts . size ) . toBe ( 1 ) ;
246+ } ) ;
247+
208248 it ( 'should start and stop polling' , async ( ) => {
209249 let callCount = 0 ;
210250 injectIntoGrpcUnary ( desc => {
0 commit comments