1- import { spy , SinonSpy } from 'sinon' ;
21import { Chance } from 'chance' ;
32import {
43 ApiClientConfiguration ,
54 ApiVersion ,
65 AutosuggestClient ,
76 AutosuggestInputType ,
87 HEADERS ,
9- Transport ,
108} from '../../src' ;
119import { generateAutosuggestSuggestion , generateCoordinate } from '../fixtures' ;
1210
@@ -17,25 +15,22 @@ describe('Autosuggest Client', () => {
1715 let apiVersion : ApiVersion ;
1816 let host : string ;
1917 let config : ApiClientConfiguration ;
20- let transportSpy : SinonSpy ;
21- let transport : Transport ;
18+ let transportSpy : jest . Mock < Promise < { status : number ; body : never } > , never > ;
2219 let client : AutosuggestClient ;
2320
2421 beforeEach ( ( ) => {
2522 apiKey = CHANCE . string ( { length : 8 } ) ;
2623 apiVersion = ApiVersion . Version1 ;
2724 host = CHANCE . url ( { path : '' } ) ;
2825 config = { host, apiVersion, headers : { } } ;
29- transportSpy = spy ( ) ;
30- transport = async ( ...args ) => {
31- transportSpy ( ...args ) ;
32- return {
26+ transportSpy = jest . fn (
27+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
28+ async ( ..._args : never ) : Promise < { status : number ; body : never } > => ( {
3329 status : 200 ,
34- // eslint-disable-next-line @typescript-eslint/no-explicit-any
35- body : { } as any ,
36- } ;
37- } ;
38- client = AutosuggestClient . init ( apiKey , config , transport ) ;
30+ body : { } as never ,
31+ } )
32+ ) ;
33+ client = AutosuggestClient . init ( apiKey , config , transportSpy ) ;
3934 } ) ;
4035
4136 it ( 'should instantiate an Autosuggest Client instance' , ( ) => {
@@ -146,7 +141,7 @@ describe('Autosuggest Client', () => {
146141 language,
147142 preferLand,
148143 } ) ;
149- expect ( transportSpy . calledOnceWith ( transportArguments ) ) . toEqual ( true ) ;
144+ expect ( transportSpy ) . toHaveBeenNthCalledWith ( 1 , transportArguments ) ;
150145 } ) ;
151146 it ( 'should call /autosuggest with voice input type' , async ( ) => {
152147 const input = `${ CHANCE . word ( ) } .${ CHANCE . word ( ) } .${ CHANCE . letter ( ) } ` ;
@@ -175,7 +170,7 @@ describe('Autosuggest Client', () => {
175170 inputType,
176171 language,
177172 } ) ;
178- expect ( transportSpy . calledOnceWith ( transportArguments ) ) . toEqual ( true ) ;
173+ expect ( transportSpy ) . toHaveBeenNthCalledWith ( 1 , transportArguments ) ;
179174 } ) ;
180175 it ( 'should throw error when no language provided with voice input type' , async ( ) => {
181176 const input = `${ CHANCE . word ( ) } .${ CHANCE . word ( ) } .${ CHANCE . letter ( ) } ` ;
@@ -195,7 +190,7 @@ describe('Autosuggest Client', () => {
195190 'You must provide language when using a speech input type'
196191 ) ;
197192 } finally {
198- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
193+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
199194 }
200195 } ) ;
201196 it ( 'should throw error if no options provided' , async ( ) => {
@@ -205,7 +200,7 @@ describe('Autosuggest Client', () => {
205200 } catch ( err ) {
206201 expect ( err . message ) . toEqual ( 'You must provide at least options.input' ) ;
207202 } finally {
208- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
203+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
209204 }
210205 } ) ;
211206 it ( 'should throw error if input is empty' , async ( ) => {
@@ -216,7 +211,7 @@ describe('Autosuggest Client', () => {
216211 } catch ( err ) {
217212 expect ( err . message ) . toEqual ( 'You must specify an input value' ) ;
218213 } finally {
219- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
214+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
220215 }
221216 } ) ;
222217 it ( 'should throw error if clipToBoundingBox has southwest lat > northeast lat' , async ( ) => {
@@ -233,7 +228,7 @@ describe('Autosuggest Client', () => {
233228 'Southwest lat must be less than or equal to northeast lat and southwest lng must be less than or equal to northeast lng'
234229 ) ;
235230 } finally {
236- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
231+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
237232 }
238233 } ) ;
239234 it ( 'should throw error if clipToBoundingBox has southwest lng > northeast lng' , async ( ) => {
@@ -250,7 +245,7 @@ describe('Autosuggest Client', () => {
250245 'Southwest lat must be less than or equal to northeast lat and southwest lng must be less than or equal to northeast lng'
251246 ) ;
252247 } finally {
253- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
248+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
254249 }
255250 } ) ;
256251 it ( 'should throw error if clipToCountry has incorrect value' , async ( ) => {
@@ -268,7 +263,7 @@ describe('Autosuggest Client', () => {
268263 'Invalid clip to country. All values must be an ISO 3166-1 alpha-2 country code'
269264 ) ;
270265 } finally {
271- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
266+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
272267 }
273268 } ) ;
274269 it ( 'should throw error if clipToPolygon has less than 4 entries' , async ( ) => {
@@ -287,7 +282,7 @@ describe('Autosuggest Client', () => {
287282 'Invalid clip to polygon value. Array must contain at least 4 coordinates and no more than 25'
288283 ) ;
289284 } finally {
290- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
285+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
291286 }
292287 } ) ;
293288 it ( 'should throw error if clipToPolygon is not closed' , async ( ) => {
@@ -306,7 +301,7 @@ describe('Autosuggest Client', () => {
306301 'Invalid clip to polygon value. The polygon bounds must be closed.'
307302 ) ;
308303 } finally {
309- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
304+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
310305 }
311306 } ) ;
312307 it ( 'should throw error if inputType is not valid' , async ( ) => {
@@ -320,7 +315,7 @@ describe('Autosuggest Client', () => {
320315 'Invalid input type provided. Must provide a valid input type.'
321316 ) ;
322317 } finally {
323- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
318+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
324319 }
325320 } ) ;
326321 const voiceInputTypes = [
@@ -339,7 +334,7 @@ describe('Autosuggest Client', () => {
339334 'You must provide language when using a speech input type'
340335 ) ;
341336 } finally {
342- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
337+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
343338 }
344339 } ) ;
345340 } ) ;
@@ -354,7 +349,7 @@ describe('Autosuggest Client', () => {
354349 'Invalid language code. It must be an ISO-639-1 2 letter code.'
355350 ) ;
356351 } finally {
357- expect ( transportSpy . notCalled ) . toEqual ( true ) ;
352+ expect ( transportSpy ) . not . toHaveBeenCalled ( ) ;
358353 }
359354 } ) ;
360355 it ( 'should call /autosuggest-selection with selected suggestion' , async ( ) => {
@@ -375,8 +370,7 @@ describe('Autosuggest Client', () => {
375370
376371 const actualOnSelected = await client . onSelected ( selected ) ;
377372 expect ( actualOnSelected ) . toEqual ( undefined ) ;
378- const actual = transportSpy . calledOnceWith ( transportArguments ) ;
379- expect ( actual ) . toEqual ( true ) ;
373+ expect ( transportSpy ) . toHaveBeenNthCalledWith ( 1 , transportArguments ) ;
380374 } ) ;
381375 describe ( 'should call /autosuggest-selection with initial request options override' , ( ) => {
382376 const input = `${ CHANCE . word ( ) } .${ CHANCE . word ( ) } .${ CHANCE . letter ( ) } ` ;
@@ -451,9 +445,9 @@ describe('Autosuggest Client', () => {
451445 preferLand,
452446 } ) ;
453447 expect ( actualOnSelected ) . toEqual ( undefined ) ;
454- const actual = transportSpy . calledOnceWith ( transportArguments ) ;
455- expect ( actual ) . toEqual ( true ) ;
448+ expect ( transportSpy ) . toHaveBeenNthCalledWith ( 1 , transportArguments ) ;
456449 } ) ;
450+
457451 it ( 'voice input type' , async ( ) => {
458452 const inputType = AutosuggestInputType . GenericVoice ;
459453 transportArguments . body [ 'input-type' ] = inputType ;
@@ -473,8 +467,7 @@ describe('Autosuggest Client', () => {
473467 preferLand,
474468 } ) ;
475469 expect ( actualOnSelected ) . toEqual ( undefined ) ;
476- const actual = transportSpy . calledOnceWith ( transportArguments ) ;
477- expect ( actual ) . toEqual ( true ) ;
470+ expect ( transportSpy ) . toHaveBeenNthCalledWith ( 1 , transportArguments ) ;
478471 } ) ;
479472 } ) ;
480473} ) ;
0 commit comments