1
+ import algoliasearchV3 from 'algoliasearch-v3' ;
2
+ import algoliasearchV4 from 'algoliasearch-v4' ;
3
+ import { liteClient as algoliasearchV5 } from 'algoliasearch-v5/lite' ;
4
+
1
5
import { hydrateSearchClient } from '../hydrateSearchClient' ;
2
6
3
7
import type { SearchClient , InitialResults } from '../../../types' ;
@@ -44,6 +48,7 @@ describe('hydrateSearchClient', () => {
44
48
client = {
45
49
transporter : { responsesCache : { set : setCache } } ,
46
50
addAlgoliaAgent : jest . fn ( ) ,
51
+ search : jest . fn ( ) ,
47
52
} as unknown as SearchClient ;
48
53
49
54
hydrateSearchClient ( client , initialResults ) ;
@@ -64,6 +69,7 @@ describe('hydrateSearchClient', () => {
64
69
it ( 'should populate the cache for < v4 if there is no transporter object' , ( ) => {
65
70
client = {
66
71
addAlgoliaAgent : jest . fn ( ) ,
72
+ search : jest . fn ( ) ,
67
73
_useCache : true ,
68
74
} as unknown as SearchClient ;
69
75
@@ -77,6 +83,7 @@ describe('hydrateSearchClient', () => {
77
83
client = {
78
84
transporter : { responsesCache : { set : setCache } } ,
79
85
addAlgoliaAgent : jest . fn ( ) ,
86
+ search : jest . fn ( ) ,
80
87
} as unknown as SearchClient ;
81
88
82
89
hydrateSearchClient ( client , {
@@ -110,6 +117,7 @@ describe('hydrateSearchClient', () => {
110
117
client = {
111
118
transporter : { responsesCache : { set : setCache } } ,
112
119
addAlgoliaAgent : jest . fn ( ) ,
120
+ search : jest . fn ( ) ,
113
121
} as unknown as SearchClient ;
114
122
115
123
hydrateSearchClient ( client , {
@@ -176,6 +184,7 @@ describe('hydrateSearchClient', () => {
176
184
client = {
177
185
transporter : { responsesCache : { set : setCache } } ,
178
186
addAlgoliaAgent : jest . fn ( ) ,
187
+ search : jest . fn ( ) ,
179
188
} as unknown as SearchClient ;
180
189
181
190
hydrateSearchClient ( client , {
@@ -204,6 +213,7 @@ describe('hydrateSearchClient', () => {
204
213
client = {
205
214
transporter : { responsesCache : { set : jest . fn ( ) } } ,
206
215
addAlgoliaAgent : jest . fn ( ) ,
216
+ search : jest . fn ( ) ,
207
217
} as unknown as SearchClient ;
208
218
209
219
hydrateSearchClient ( client , {
@@ -221,6 +231,7 @@ describe('hydrateSearchClient', () => {
221
231
client = {
222
232
transporter : { responsesCache : { set : setCache } } ,
223
233
addAlgoliaAgent : jest . fn ( ) ,
234
+ search : jest . fn ( ) ,
224
235
} as unknown as SearchClient ;
225
236
226
237
hydrateSearchClient ( client , {
@@ -235,4 +246,47 @@ describe('hydrateSearchClient', () => {
235
246
{ results : [ ] }
236
247
) ;
237
248
} ) ;
249
+
250
+ it ( 'should not throw if search requires to be bound (v5)' , async ( ) => {
251
+ const send = jest . fn ( ) . mockResolvedValue ( { status : 200 , content : '{}' } ) ;
252
+ const searchClient : any = algoliasearchV5 ( 'appId' , 'apiKey' , {
253
+ requester : {
254
+ send,
255
+ } ,
256
+ } ) ;
257
+
258
+ hydrateSearchClient ( searchClient , initialResults ) ;
259
+
260
+ await searchClient . search ( [ { indexName : 'another' , params : { } } ] ) ;
261
+
262
+ expect ( send ) . toHaveBeenCalled ( ) ;
263
+ } ) ;
264
+
265
+ it ( 'should not throw if search requires to be bound (v4)' , async ( ) => {
266
+ const send = jest . fn ( ) . mockResolvedValue ( { status : 200 , content : '{}' } ) ;
267
+ const searchClient : any = algoliasearchV4 ( 'appId' , 'apiKey' , {
268
+ requester : {
269
+ send,
270
+ } ,
271
+ } ) ;
272
+
273
+ hydrateSearchClient ( searchClient , initialResults ) ;
274
+
275
+ await searchClient . search ( [ { indexName : 'another' , params : { } } ] ) ;
276
+
277
+ expect ( send ) . toHaveBeenCalled ( ) ;
278
+ } ) ;
279
+
280
+ it ( 'should not throw if search requires to be bound (v3)' , async ( ) => {
281
+ const searchClient : any = algoliasearchV3 ( 'appId' , 'apiKey' ) ;
282
+ searchClient . _request = jest
283
+ . fn ( )
284
+ . mockResolvedValue ( { body : { status : 200 } } ) ;
285
+
286
+ hydrateSearchClient ( searchClient , initialResults ) ;
287
+
288
+ await searchClient . search ( [ { indexName : 'another' , params : { } } ] ) ;
289
+
290
+ expect ( searchClient . _request ) . toHaveBeenCalled ( ) ;
291
+ } ) ;
238
292
} ) ;
0 commit comments