1313// limitations under the License.
1414import { FileMode , Storage } from './storage' ;
1515import { StorageServiceClient as GrpcStorageClient } from '@nitric/api/proto/storage/v1/storage_grpc_pb' ;
16- import { StorageWriteResponse , StorageReadResponse , StorageDeleteResponse , StoragePreSignUrlResponse , StoragePreSignUrlRequest } from '@nitric/api/proto/storage/v1/storage_pb' ;
16+ import { StorageWriteResponse , StorageReadResponse , StorageDeleteResponse , StoragePreSignUrlResponse , StoragePreSignUrlRequest , StorageListFilesResponse , File } from '@nitric/api/proto/storage/v1/storage_pb' ;
1717import { UnimplementedError } from '../../errors' ;
1818
1919describe ( 'Storage Client Tests' , ( ) => {
@@ -321,7 +321,7 @@ describe('Storage Client Tests', () => {
321321 preSignUrlMock . mockClear ( ) ;
322322 const client = new Storage ( ) . bucket ( 'test_bucket' ) . file ( 'test/item' ) ;
323323 signUrl = await client . getDownloadUrl ( )
324- } )
324+ } ) ;
325325
326326 test ( 'Then file.signUrl should delete the bytes from the bucket' , ( ) => {
327327 expect ( signUrl ) . toEqual ( "testingUrl" ) ;
@@ -341,4 +341,75 @@ describe('Storage Client Tests', () => {
341341 } ) ;
342342 } ) ;
343343 } ) ;
344+
345+ describe ( 'Given nitric.api.storage.StorageClient.ListFiles throws an error' , ( ) => {
346+ const MOCK_ERROR = {
347+ code : 2 ,
348+ message : 'UNIMPLEMENTED' ,
349+ } ;
350+
351+ let listFilesMock ;
352+
353+ beforeAll ( ( ) => {
354+ listFilesMock = jest
355+ . spyOn ( GrpcStorageClient . prototype , 'listFiles' )
356+ . mockImplementation ( ( _ , callback : any ) => {
357+ callback ( MOCK_ERROR , null ) ;
358+
359+ return null as any ;
360+ } ) ;
361+ } ) ;
362+
363+ afterAll ( ( ) => {
364+ jest . resetAllMocks ( ) ;
365+ } ) ;
366+
367+ test ( 'Then StorageClient.listFiles should reject' , async ( ) => {
368+ const client = new Storage ( ) ;
369+ await expect ( client . bucket ( 'test' ) . files ( ) ) . rejects . toEqual (
370+ new UnimplementedError ( "UNIMPLEMENTED" )
371+ ) ;
372+ } ) ;
373+
374+ test ( 'The Grpc client for Storage.listFiles should have been called exactly once' , ( ) => {
375+ expect ( listFilesMock ) . toBeCalledTimes ( 1 ) ;
376+ } ) ;
377+ } ) ;
378+
379+ describe ( 'Given nitric.api.storage.StorageClient.ListFiles succeeds' , ( ) => {
380+ const MOCK_REPLY = new StorageListFilesResponse ( ) ;
381+ MOCK_REPLY . setFilesList ( [ "test/test.txt" ] . map ( ( k ) => {
382+ const f = new File ( ) ;
383+ f . setKey ( k ) ;
384+ return f ;
385+ } ) ) ;
386+
387+ let listFilesMock ;
388+
389+ beforeAll ( ( ) => {
390+ listFilesMock = jest
391+ . spyOn ( GrpcStorageClient . prototype , 'listFiles' )
392+ . mockImplementation ( ( _ , callback : any ) => {
393+ callback ( null , MOCK_REPLY ) ;
394+
395+ return null as any ;
396+ } ) ;
397+ } ) ;
398+
399+ afterAll ( ( ) => {
400+ jest . resetAllMocks ( ) ;
401+ } ) ;
402+
403+ test ( 'Then StorageClient.listFiles should return files' , async ( ) => {
404+ const client = new Storage ( ) ;
405+ const files = await client . bucket ( 'test' ) . files ( ) ;
406+
407+ expect ( files ) . toHaveLength ( 1 ) ;
408+ expect ( files [ 0 ] . name ) . toBe ( 'test/test.txt' )
409+ } ) ;
410+
411+ test ( 'The Grpc client for Storage.listFiles should have been called exactly once' , ( ) => {
412+ expect ( listFilesMock ) . toBeCalledTimes ( 1 ) ;
413+ } ) ;
414+ } ) ;
344415} ) ;
0 commit comments