Skip to content

Commit 381ba88

Browse files
feat(javascript): add requester-testing package (generated)
algolia/api-clients-automation#3684 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 8051cca commit 381ba88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1516
-4176
lines changed

package.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,12 @@
1616
"test:bundle": "lerna run test:bundle --verbose --skip-nx-cache --include-dependencies"
1717
},
1818
"devDependencies": {
19-
"@babel/core": "7.25.2",
20-
"@babel/plugin-transform-class-properties": "7.25.4",
21-
"@babel/plugin-transform-runtime": "7.25.4",
22-
"@babel/preset-env": "7.25.4",
23-
"@babel/runtime": "7.25.6",
24-
"@rollup/plugin-babel": "6.0.4",
25-
"@rollup/plugin-node-resolve": "15.2.3",
26-
"@rollup/plugin-terser": "0.4.4",
27-
"@types/jest": "29.5.12",
2819
"@types/node": "22.5.1",
29-
"@types/rollup-plugin-node-globals": "1.4.4",
3020
"@types/semver": "7.5.8",
3121
"bundlesize": "0.18.2",
3222
"execa": "9.3.1",
3323
"lerna": "8.1.8",
3424
"rollup": "4.21.2",
35-
"rollup-plugin-node-globals": "1.4.0",
36-
"rollup-plugin-typescript2": "0.36.0",
3725
"semver": "7.6.3",
3826
"typescript": "5.5.4"
3927
},
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import { test, expect } from 'vitest';
2+
13
import { algoliasearch, apiClientVersion } from '../builds/browser';
24

3-
describe('bundle', () => {
4-
const client = algoliasearch('APP_ID', 'API_KEY');
5+
const client = algoliasearch('APP_ID', 'API_KEY');
56

6-
it('sets the ua', () => {
7-
expect(client.transporter.algoliaAgent).toEqual({
8-
add: expect.any(Function),
9-
value: expect.stringContaining(
10-
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Browser`,
11-
),
12-
});
7+
test('sets the ua', () => {
8+
expect(client.transporter.algoliaAgent).toEqual({
9+
add: expect.any(Function),
10+
value: expect.stringContaining(
11+
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Browser`,
12+
),
1313
});
1414
});

packages/algoliasearch/__tests__/algoliasearch.common.test.ts

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,81 @@
1-
import type { EchoResponse } from '@algolia/client-common';
21
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,
65
} 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';
88

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';
1112

1213
const client = algoliasearch('APP_ID', 'API_KEY', {
13-
requester: echoRequester(),
14+
requester: browserEchoRequester(),
1415
});
1516

1617
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', () => {
1819
expect(client.appId).toEqual('APP_ID');
1920
});
2021

21-
it('provides a `clearCache` method', () => {
22+
test('provides a `clearCache` method', () => {
2223
expect(client.clearCache).not.toBeUndefined();
2324
expect(() => client.clearCache()).not.toThrow();
2425
});
2526

26-
it('provides a `setClientApiKey` method', () => {
27+
test('provides a `setClientApiKey` method', () => {
2728
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');
2930
expect(_client.setClientApiKey).not.toBeUndefined();
3031
_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');
3233
});
3334

34-
it('throws with undefined API key', () => {
35+
test('throws with undefined API key', () => {
3536
expect(() => algoliasearch('APP_ID', '')).toThrow('`apiKey` is missing');
3637
});
3738

38-
it('throws with undefined app ID', () => {
39+
test('throws with undefined app ID', () => {
3940
expect(() => algoliasearch('', 'API_KEY')).toThrow('`appId` is missing');
4041
});
4142

42-
it('provides the search client at the root of the API', () => {
43+
test('provides the search client at the root of the API', () => {
4344
expect(client.search).not.toBeUndefined();
4445
});
4546

4647
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', () => {
4849
expect(client._ua).toEqual(
4950
expect.stringContaining(`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion});`),
5051
);
5152
});
5253

53-
it('keeps `_ua` updated with the transporter algolia agent', () => {
54-
expect(client._ua).toEqual(expect.stringMatching(/.*; Node\.js \(.*\)$/g));
54+
test('keeps `_ua` updated with the transporter algolia agent', () => {
55+
expect(client._ua).toEqual(expect.stringMatching(/.*; Browser$/g));
5556

56-
client.addAlgoliaAgent('Jest', '0.0.1');
57+
client.addAlgoliaAgent('Vitest', '0.0.1');
5758

58-
expect(client._ua).toEqual(expect.stringMatching(/.*; Jest \(0\.0\.1\)$/g));
59+
expect(client._ua).toEqual(expect.stringMatching(/.*; Vitest \(0\.0\.1\)$/g));
5960
});
6061
});
6162

62-
it('exposes the search client transporter for the algoliasearch client', () => {
63+
test('exposes the search client transporter for the algoliasearch client', () => {
6364
expect(client.transporter).not.toBeUndefined();
6465
expect(client.transporter).toEqual({
6566
algoliaAgent: {
6667
add: expect.any(Function),
6768
value: expect.stringContaining(
68-
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Node.js`,
69+
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Browser`,
6970
),
7071
},
71-
baseHeaders: {
72-
'content-type': 'text/plain',
72+
baseQueryParameters: {
7373
'x-algolia-api-key': 'API_KEY',
7474
'x-algolia-application-id': 'APP_ID',
7575
},
76-
baseQueryParameters: {},
76+
baseHeaders: {
77+
'content-type': 'text/plain',
78+
},
7779
hosts: expect.arrayContaining([
7880
{
7981
accept: 'read',
@@ -124,37 +126,37 @@ describe('api', () => {
124126
set: expect.any(Function),
125127
},
126128
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,
130132
},
131133
});
132134
});
133135

134136
describe('init clients', () => {
135-
it('provides an init method for the analytics client', () => {
137+
test('provides an init method for the analytics client', () => {
136138
expect(client.initAnalytics).not.toBeUndefined();
137139
});
138140

139-
it('provides an init method for the abtesting client', () => {
141+
test('provides an init method for the abtesting client', () => {
140142
expect(client.initAbtesting).not.toBeUndefined();
141143
});
142144

143-
it('provides an init method for the personalization client', () => {
145+
test('provides an init method for the personalization client', () => {
144146
expect(client.initPersonalization).not.toBeUndefined();
145147
});
146148

147-
it('provides an init method for the recommend client', () => {
149+
test('provides an init method for the recommend client', () => {
148150
expect(client.initRecommend).not.toBeUndefined();
149151
});
150152

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() } });
155157
const personalizationClient = client.initPersonalization({
156158
region: 'eu',
157-
options: { requester: echoRequester() },
159+
options: { requester: browserEchoRequester() },
158160
});
159161

160162
const res1 = (await abtestingClient.customGet({
@@ -196,27 +198,27 @@ describe('api', () => {
196198
);
197199
});
198200

199-
it('`init` clients accept different credentials', async () => {
201+
test('`init` clients accept different credentials', async () => {
200202
const abtestingClient = client.initAbtesting({
201203
appId: 'appId1',
202204
apiKey: 'apiKey1',
203-
options: { requester: echoRequester() },
205+
options: { requester: browserEchoRequester() },
204206
});
205207
const analyticsClient = client.initAnalytics({
206208
appId: 'appId2',
207209
apiKey: 'apiKey2',
208-
options: { requester: echoRequester() },
210+
options: { requester: browserEchoRequester() },
209211
});
210212
const personalizationClient = client.initPersonalization({
211213
appId: 'appId3',
212214
apiKey: 'apiKey3',
213215
region: 'eu',
214-
options: { requester: echoRequester() },
216+
options: { requester: browserEchoRequester() },
215217
});
216218
const recommendClient = client.initRecommend({
217219
appId: 'appId4',
218220
apiKey: 'apiKey4',
219-
options: { requester: echoRequester() },
221+
options: { requester: browserEchoRequester() },
220222
});
221223

222224
const res1 = (await abtestingClient.customGet({
@@ -261,7 +263,7 @@ describe('api', () => {
261263
});
262264

263265
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', () => {
265267
expect(liteClient).not.toBeUndefined();
266268
expect(algoliasearch).not.toBeUndefined();
267269
});
@@ -272,7 +274,7 @@ describe('bundle', () => {
272274
* The new signatures are already tested in the CTS.
273275
*/
274276
describe('search with legacy signature', () => {
275-
it('allows searching for query', async () => {
277+
test('allows searching for query', async () => {
276278
const req = (await client.search([
277279
{
278280
indexName: 'theIndexName',
@@ -282,10 +284,13 @@ describe('search with legacy signature', () => {
282284
expect(req.path).toEqual('/1/indexes/*/queries');
283285
expect(req.method).toEqual('POST');
284286
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+
});
286291
});
287292

288-
it('allows searching for facet', async () => {
293+
test('allows searching for facet', async () => {
289294
const req = (await client.search([
290295
{
291296
indexName: 'theIndexName',
@@ -299,10 +304,13 @@ describe('search with legacy signature', () => {
299304
expect(req.data).toEqual({
300305
requests: [{ indexName: 'theIndexName', type: 'facet', facet: 'theFacet' }],
301306
});
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+
});
303311
});
304312

305-
it('accepts a `params` parameter for `searchParams`', async () => {
313+
test('accepts a `params` parameter for `searchParams`', async () => {
306314
const req = (await client.search([
307315
{
308316
indexName: 'theIndexName',
@@ -317,19 +325,22 @@ describe('search with legacy signature', () => {
317325
expect(req.data).toEqual({
318326
requests: [{ indexName: 'theIndexName', hitsPerPage: 42 }],
319327
});
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+
});
321332
});
322333
});
323334

324335
describe('init', () => {
325336
test('sets authMode', async () => {
326337
const qpClient = algoliasearch('foo', 'bar', {
327338
authMode: 'WithinQueryParameters',
328-
requester: echoRequester(),
339+
requester: browserEchoRequester(),
329340
});
330341
const headerClient = algoliasearch('foo', 'bar', {
331342
authMode: 'WithinHeaders',
332-
requester: echoRequester(),
343+
requester: browserEchoRequester(),
333344
});
334345

335346
const qpResult = (await qpClient.customGet({
@@ -351,13 +362,11 @@ describe('init', () => {
351362
});
352363
});
353364

354-
test('defaults to headers', async () => {
365+
test('defaults to qp', async () => {
355366
const res = (await client.customGet({
356367
path: '1/foo',
357368
})) as unknown as EchoResponse;
358-
expect(res.headers).toEqual({
359-
accept: 'application/json',
360-
'content-type': 'text/plain',
369+
expect(res.searchParams).toEqual({
361370
'x-algolia-api-key': 'API_KEY',
362371
'x-algolia-application-id': 'APP_ID',
363372
});
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1+
import { test, expect } from 'vitest';
2+
13
import { algoliasearch, apiClientVersion } from '../builds/fetch';
24

3-
describe('bundle', () => {
4-
const client = algoliasearch('APP_ID', 'API_KEY');
5+
const client = algoliasearch('APP_ID', 'API_KEY');
56

6-
it('sets the ua', () => {
7-
expect(client.transporter.algoliaAgent).toEqual({
8-
add: expect.any(Function),
9-
value: expect.stringContaining(
10-
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Fetch`,
11-
),
12-
});
7+
test('sets the ua', () => {
8+
expect(client.transporter.algoliaAgent).toEqual({
9+
add: expect.any(Function),
10+
value: expect.stringContaining(`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Fetch`),
1311
});
1412
});
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import { test, expect } from 'vitest';
2+
13
import { algoliasearch, apiClientVersion } from '../builds/node';
24

3-
describe('bundle', () => {
4-
const client = algoliasearch('APP_ID', 'API_KEY');
5+
const client = algoliasearch('APP_ID', 'API_KEY');
56

6-
it('sets the ua', () => {
7-
expect(client.transporter.algoliaAgent).toEqual({
8-
add: expect.any(Function),
9-
value: expect.stringContaining(
10-
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Node.js`,
11-
),
12-
});
7+
test('sets the ua', () => {
8+
expect(client.transporter.algoliaAgent).toEqual({
9+
add: expect.any(Function),
10+
value: expect.stringContaining(
11+
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Node.js`,
12+
),
1313
});
1414
});

packages/algoliasearch/babel.config.cjs

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)