@@ -21,6 +21,7 @@ import {
2121 StoragePreSignUrlRequest ,
2222 StorageListFilesResponse ,
2323 File ,
24+ StorageExistsResponse ,
2425} from '@nitric/api/proto/storage/v1/storage_pb' ;
2526import { UnimplementedError } from '../../errors' ;
2627import {
@@ -162,6 +163,70 @@ describe('Storage Client Tests', () => {
162163 expect ( readMock ) . toBeCalledTimes ( 1 ) ;
163164 } ) ;
164165 } ) ;
166+
167+ describe ( 'Given nitric.api.storage.StorageClient.Exists throws an error' , ( ) => {
168+ const MOCK_ERROR = {
169+ code : 2 ,
170+ message : 'UNIMPLEMENTED' ,
171+ } ;
172+ let existsMock ;
173+
174+ beforeAll ( ( ) => {
175+ existsMock = jest
176+ . spyOn ( GrpcStorageClient . prototype , 'exists' )
177+ . mockImplementation ( ( _ , callback : any ) => {
178+ callback ( MOCK_ERROR , null ) ;
179+
180+ return null as any ;
181+ } ) ;
182+ } ) ;
183+
184+ afterAll ( ( ) => {
185+ jest . resetAllMocks ( ) ;
186+ } ) ;
187+
188+ test ( 'Then StorageClient.exists should reject' , async ( ) => {
189+ const storage = new Storage ( ) ;
190+ await expect (
191+ storage . bucket ( 'test_bucket' ) . file ( 'test/item' ) . exists ( )
192+ ) . rejects . toEqual ( new UnimplementedError ( 'UNIMPLEMENTED' ) ) ;
193+ } ) ;
194+
195+ test ( 'The Grpc client for Storage.write should have been called exactly once' , ( ) => {
196+ expect ( existsMock ) . toBeCalledTimes ( 1 ) ;
197+ } ) ;
198+ } ) ;
199+
200+ describe ( 'Given nitric.api.storage.StorageClient.Exists succeeds' , ( ) => {
201+ let writeMock ;
202+ const MOCK_REPLY = new StorageExistsResponse ( ) ;
203+
204+ beforeAll ( ( ) => {
205+ writeMock = jest
206+ . spyOn ( GrpcStorageClient . prototype , 'exists' )
207+ . mockImplementation ( ( _ , callback : any ) => {
208+ callback ( null , MOCK_REPLY ) ;
209+
210+ return null as any ;
211+ } ) ;
212+ } ) ;
213+
214+ afterAll ( ( ) => {
215+ jest . resetAllMocks ( ) ;
216+ } ) ;
217+
218+ test ( 'Then StorageClient.exists should resolve with success status' , async ( ) => {
219+ const storage = new Storage ( ) ;
220+ await expect (
221+ storage . bucket ( 'test_bucket' ) . file ( 'test/item' ) . exists ( )
222+ ) . resolves . toBe ( false ) ;
223+ } ) ;
224+
225+ test ( 'The Grpc client for Storage.write should have been called exactly once' , ( ) => {
226+ expect ( writeMock ) . toBeCalledTimes ( 1 ) ;
227+ } ) ;
228+ } ) ;
229+
165230 describe ( 'Given nitric.api.storage.StorageClient.Delete throws an error' , ( ) => {
166231 const MOCK_ERROR = {
167232 code : 2 ,
0 commit comments