1
- import type { EchoResponse } from '@algolia/client-common' ;
2
1
import {
3
- DEFAULT_CONNECT_TIMEOUT_NODE ,
4
- DEFAULT_READ_TIMEOUT_NODE ,
5
- DEFAULT_WRITE_TIMEOUT_NODE ,
2
+ DEFAULT_CONNECT_TIMEOUT_BROWSER ,
3
+ DEFAULT_READ_TIMEOUT_BROWSER ,
4
+ DEFAULT_WRITE_TIMEOUT_BROWSER ,
6
5
} from '@algolia/client-common' ;
7
- import { echoRequester } from '@algolia/requester-node-http/src/echoRequester' ;
6
+ import type { EchoResponse } from '@algolia/requester-testing' ;
7
+ import { describe , test , expect } from 'vitest' ;
8
8
9
- import { algoliasearch , apiClientVersion } from '../builds/node' ;
10
- import { liteClient } from '../lite/builds/node' ;
9
+ import { browserEchoRequester } from '../../requester-testing/src/browserEchoRequester' ;
10
+ import { algoliasearch , apiClientVersion } from '../builds/browser' ;
11
+ import { liteClient } from '../lite/builds/browser' ;
11
12
12
13
const client = algoliasearch ( 'APP_ID' , 'API_KEY' , {
13
- requester : echoRequester ( ) ,
14
+ requester : browserEchoRequester ( ) ,
14
15
} ) ;
15
16
16
17
describe ( 'api' , ( ) => {
17
- it ( 'exposes the `appId` currently in use at the root of the API' , ( ) => {
18
+ test ( 'exposes the `appId` currently in use at the root of the API' , ( ) => {
18
19
expect ( client . appId ) . toEqual ( 'APP_ID' ) ;
19
20
} ) ;
20
21
21
- it ( 'provides a `clearCache` method' , ( ) => {
22
+ test ( 'provides a `clearCache` method' , ( ) => {
22
23
expect ( client . clearCache ) . not . toBeUndefined ( ) ;
23
24
expect ( ( ) => client . clearCache ( ) ) . not . toThrow ( ) ;
24
25
} ) ;
25
26
26
- it ( 'provides a `setClientApiKey` method' , ( ) => {
27
+ test ( 'provides a `setClientApiKey` method' , ( ) => {
27
28
const _client = algoliasearch ( 'foo' , 'bar' ) ;
28
- expect ( _client . transporter . baseHeaders [ 'x-algolia-api-key' ] ) . toEqual ( 'bar' ) ;
29
+ expect ( _client . transporter . baseQueryParameters [ 'x-algolia-api-key' ] ) . toEqual ( 'bar' ) ;
29
30
expect ( _client . setClientApiKey ) . not . toBeUndefined ( ) ;
30
31
_client . setClientApiKey ( { apiKey : 'tabac' } ) ;
31
- expect ( _client . transporter . baseHeaders [ 'x-algolia-api-key' ] ) . toEqual ( 'tabac' ) ;
32
+ expect ( _client . transporter . baseQueryParameters [ 'x-algolia-api-key' ] ) . toEqual ( 'tabac' ) ;
32
33
} ) ;
33
34
34
- it ( 'throws with undefined API key' , ( ) => {
35
+ test ( 'throws with undefined API key' , ( ) => {
35
36
expect ( ( ) => algoliasearch ( 'APP_ID' , '' ) ) . toThrow ( '`apiKey` is missing' ) ;
36
37
} ) ;
37
38
38
- it ( 'throws with undefined app ID' , ( ) => {
39
+ test ( 'throws with undefined app ID' , ( ) => {
39
40
expect ( ( ) => algoliasearch ( '' , 'API_KEY' ) ) . toThrow ( '`appId` is missing' ) ;
40
41
} ) ;
41
42
42
- it ( 'provides the search client at the root of the API' , ( ) => {
43
+ test ( 'provides the search client at the root of the API' , ( ) => {
43
44
expect ( client . search ) . not . toBeUndefined ( ) ;
44
45
} ) ;
45
46
46
47
describe ( '_ua' , ( ) => {
47
- it ( 'provides a backward compatible `_ua` variable at the root of the client' , ( ) => {
48
+ test ( 'provides a backward compatible `_ua` variable at the root of the client' , ( ) => {
48
49
expect ( client . _ua ) . toEqual (
49
50
expect . stringContaining ( `Algolia for JavaScript (${ apiClientVersion } ); Search (${ apiClientVersion } );` ) ,
50
51
) ;
51
52
} ) ;
52
53
53
- it ( 'keeps `_ua` updated with the transporter algolia agent' , ( ) => {
54
- expect ( client . _ua ) . toEqual ( expect . stringMatching ( / .* ; N o d e \. j s \( . * \) $ / g) ) ;
54
+ test ( 'keeps `_ua` updated with the transporter algolia agent' , ( ) => {
55
+ expect ( client . _ua ) . toEqual ( expect . stringMatching ( / .* ; B r o w s e r $ / g) ) ;
55
56
56
- client . addAlgoliaAgent ( 'Jest ' , '0.0.1' ) ;
57
+ client . addAlgoliaAgent ( 'Vitest ' , '0.0.1' ) ;
57
58
58
- expect ( client . _ua ) . toEqual ( expect . stringMatching ( / .* ; J e s t \( 0 \. 0 \. 1 \) $ / g) ) ;
59
+ expect ( client . _ua ) . toEqual ( expect . stringMatching ( / .* ; V i t e s t \( 0 \. 0 \. 1 \) $ / g) ) ;
59
60
} ) ;
60
61
} ) ;
61
62
62
- it ( 'exposes the search client transporter for the algoliasearch client' , ( ) => {
63
+ test ( 'exposes the search client transporter for the algoliasearch client' , ( ) => {
63
64
expect ( client . transporter ) . not . toBeUndefined ( ) ;
64
65
expect ( client . transporter ) . toEqual ( {
65
66
algoliaAgent : {
66
67
add : expect . any ( Function ) ,
67
68
value : expect . stringContaining (
68
- `Algolia for JavaScript (${ apiClientVersion } ); Search (${ apiClientVersion } ); Node.js ` ,
69
+ `Algolia for JavaScript (${ apiClientVersion } ); Search (${ apiClientVersion } ); Browser ` ,
69
70
) ,
70
71
} ,
71
- baseHeaders : {
72
- 'content-type' : 'text/plain' ,
72
+ baseQueryParameters : {
73
73
'x-algolia-api-key' : 'API_KEY' ,
74
74
'x-algolia-application-id' : 'APP_ID' ,
75
75
} ,
76
- baseQueryParameters : { } ,
76
+ baseHeaders : {
77
+ 'content-type' : 'text/plain' ,
78
+ } ,
77
79
hosts : expect . arrayContaining ( [
78
80
{
79
81
accept : 'read' ,
@@ -124,37 +126,37 @@ describe('api', () => {
124
126
set : expect . any ( Function ) ,
125
127
} ,
126
128
timeouts : {
127
- connect : DEFAULT_CONNECT_TIMEOUT_NODE ,
128
- read : DEFAULT_READ_TIMEOUT_NODE ,
129
- write : DEFAULT_WRITE_TIMEOUT_NODE ,
129
+ connect : DEFAULT_CONNECT_TIMEOUT_BROWSER ,
130
+ read : DEFAULT_READ_TIMEOUT_BROWSER ,
131
+ write : DEFAULT_WRITE_TIMEOUT_BROWSER ,
130
132
} ,
131
133
} ) ;
132
134
} ) ;
133
135
134
136
describe ( 'init clients' , ( ) => {
135
- it ( 'provides an init method for the analytics client' , ( ) => {
137
+ test ( 'provides an init method for the analytics client' , ( ) => {
136
138
expect ( client . initAnalytics ) . not . toBeUndefined ( ) ;
137
139
} ) ;
138
140
139
- it ( 'provides an init method for the abtesting client' , ( ) => {
141
+ test ( 'provides an init method for the abtesting client' , ( ) => {
140
142
expect ( client . initAbtesting ) . not . toBeUndefined ( ) ;
141
143
} ) ;
142
144
143
- it ( 'provides an init method for the personalization client' , ( ) => {
145
+ test ( 'provides an init method for the personalization client' , ( ) => {
144
146
expect ( client . initPersonalization ) . not . toBeUndefined ( ) ;
145
147
} ) ;
146
148
147
- it ( 'provides an init method for the recommend client' , ( ) => {
149
+ test ( 'provides an init method for the recommend client' , ( ) => {
148
150
expect ( client . initRecommend ) . not . toBeUndefined ( ) ;
149
151
} ) ;
150
152
151
- it ( 'default `init` clients to the root `algoliasearch` credentials' , async ( ) => {
152
- const abtestingClient = client . initAbtesting ( { options : { requester : echoRequester ( ) } } ) ;
153
- const analyticsClient = client . initAnalytics ( { options : { requester : echoRequester ( ) } } ) ;
154
- const recommendClient = client . initRecommend ( { options : { requester : echoRequester ( ) } } ) ;
153
+ test ( 'default `init` clients to the root `algoliasearch` credentials' , async ( ) => {
154
+ const abtestingClient = client . initAbtesting ( { options : { requester : browserEchoRequester ( ) } } ) ;
155
+ const analyticsClient = client . initAnalytics ( { options : { requester : browserEchoRequester ( ) } } ) ;
156
+ const recommendClient = client . initRecommend ( { options : { requester : browserEchoRequester ( ) } } ) ;
155
157
const personalizationClient = client . initPersonalization ( {
156
158
region : 'eu' ,
157
- options : { requester : echoRequester ( ) } ,
159
+ options : { requester : browserEchoRequester ( ) } ,
158
160
} ) ;
159
161
160
162
const res1 = ( await abtestingClient . customGet ( {
@@ -196,27 +198,27 @@ describe('api', () => {
196
198
) ;
197
199
} ) ;
198
200
199
- it ( '`init` clients accept different credentials' , async ( ) => {
201
+ test ( '`init` clients accept different credentials' , async ( ) => {
200
202
const abtestingClient = client . initAbtesting ( {
201
203
appId : 'appId1' ,
202
204
apiKey : 'apiKey1' ,
203
- options : { requester : echoRequester ( ) } ,
205
+ options : { requester : browserEchoRequester ( ) } ,
204
206
} ) ;
205
207
const analyticsClient = client . initAnalytics ( {
206
208
appId : 'appId2' ,
207
209
apiKey : 'apiKey2' ,
208
- options : { requester : echoRequester ( ) } ,
210
+ options : { requester : browserEchoRequester ( ) } ,
209
211
} ) ;
210
212
const personalizationClient = client . initPersonalization ( {
211
213
appId : 'appId3' ,
212
214
apiKey : 'apiKey3' ,
213
215
region : 'eu' ,
214
- options : { requester : echoRequester ( ) } ,
216
+ options : { requester : browserEchoRequester ( ) } ,
215
217
} ) ;
216
218
const recommendClient = client . initRecommend ( {
217
219
appId : 'appId4' ,
218
220
apiKey : 'apiKey4' ,
219
- options : { requester : echoRequester ( ) } ,
221
+ options : { requester : browserEchoRequester ( ) } ,
220
222
} ) ;
221
223
222
224
const res1 = ( await abtestingClient . customGet ( {
@@ -261,7 +263,7 @@ describe('api', () => {
261
263
} ) ;
262
264
263
265
describe ( 'bundle' , ( ) => {
264
- it ( 'expose both a full bundled package and a lite one' , ( ) => {
266
+ test ( 'expose both a full bundled package and a lite one' , ( ) => {
265
267
expect ( liteClient ) . not . toBeUndefined ( ) ;
266
268
expect ( algoliasearch ) . not . toBeUndefined ( ) ;
267
269
} ) ;
@@ -272,7 +274,7 @@ describe('bundle', () => {
272
274
* The new signatures are already tested in the CTS.
273
275
*/
274
276
describe ( 'search with legacy signature' , ( ) => {
275
- it ( 'allows searching for query' , async ( ) => {
277
+ test ( 'allows searching for query' , async ( ) => {
276
278
const req = ( await client . search ( [
277
279
{
278
280
indexName : 'theIndexName' ,
@@ -282,10 +284,13 @@ describe('search with legacy signature', () => {
282
284
expect ( req . path ) . toEqual ( '/1/indexes/*/queries' ) ;
283
285
expect ( req . method ) . toEqual ( 'POST' ) ;
284
286
expect ( req . data ) . toEqual ( { requests : [ { indexName : 'theIndexName' } ] } ) ;
285
- expect ( req . searchParams ) . toStrictEqual ( undefined ) ;
287
+ expect ( req . searchParams ) . toStrictEqual ( {
288
+ 'x-algolia-api-key' : 'API_KEY' ,
289
+ 'x-algolia-application-id' : 'APP_ID' ,
290
+ } ) ;
286
291
} ) ;
287
292
288
- it ( 'allows searching for facet' , async ( ) => {
293
+ test ( 'allows searching for facet' , async ( ) => {
289
294
const req = ( await client . search ( [
290
295
{
291
296
indexName : 'theIndexName' ,
@@ -299,10 +304,13 @@ describe('search with legacy signature', () => {
299
304
expect ( req . data ) . toEqual ( {
300
305
requests : [ { indexName : 'theIndexName' , type : 'facet' , facet : 'theFacet' } ] ,
301
306
} ) ;
302
- expect ( req . searchParams ) . toStrictEqual ( undefined ) ;
307
+ expect ( req . searchParams ) . toStrictEqual ( {
308
+ 'x-algolia-api-key' : 'API_KEY' ,
309
+ 'x-algolia-application-id' : 'APP_ID' ,
310
+ } ) ;
303
311
} ) ;
304
312
305
- it ( 'accepts a `params` parameter for `searchParams`' , async ( ) => {
313
+ test ( 'accepts a `params` parameter for `searchParams`' , async ( ) => {
306
314
const req = ( await client . search ( [
307
315
{
308
316
indexName : 'theIndexName' ,
@@ -317,19 +325,22 @@ describe('search with legacy signature', () => {
317
325
expect ( req . data ) . toEqual ( {
318
326
requests : [ { indexName : 'theIndexName' , hitsPerPage : 42 } ] ,
319
327
} ) ;
320
- expect ( req . searchParams ) . toStrictEqual ( undefined ) ;
328
+ expect ( req . searchParams ) . toStrictEqual ( {
329
+ 'x-algolia-api-key' : 'API_KEY' ,
330
+ 'x-algolia-application-id' : 'APP_ID' ,
331
+ } ) ;
321
332
} ) ;
322
333
} ) ;
323
334
324
335
describe ( 'init' , ( ) => {
325
336
test ( 'sets authMode' , async ( ) => {
326
337
const qpClient = algoliasearch ( 'foo' , 'bar' , {
327
338
authMode : 'WithinQueryParameters' ,
328
- requester : echoRequester ( ) ,
339
+ requester : browserEchoRequester ( ) ,
329
340
} ) ;
330
341
const headerClient = algoliasearch ( 'foo' , 'bar' , {
331
342
authMode : 'WithinHeaders' ,
332
- requester : echoRequester ( ) ,
343
+ requester : browserEchoRequester ( ) ,
333
344
} ) ;
334
345
335
346
const qpResult = ( await qpClient . customGet ( {
@@ -351,13 +362,11 @@ describe('init', () => {
351
362
} ) ;
352
363
} ) ;
353
364
354
- test ( 'defaults to headers ' , async ( ) => {
365
+ test ( 'defaults to qp ' , async ( ) => {
355
366
const res = ( await client . customGet ( {
356
367
path : '1/foo' ,
357
368
} ) ) as unknown as EchoResponse ;
358
- expect ( res . headers ) . toEqual ( {
359
- accept : 'application/json' ,
360
- 'content-type' : 'text/plain' ,
369
+ expect ( res . searchParams ) . toEqual ( {
361
370
'x-algolia-api-key' : 'API_KEY' ,
362
371
'x-algolia-application-id' : 'APP_ID' ,
363
372
} ) ;
0 commit comments