Skip to content

Commit 2a171a9

Browse files
authored
fix(client): ensure client.search is bound (#6352)
* fix(client): ensure client.search is bound fixes #6350 fixes algolia/algoliasearch-client-javascript#1549 possibly also solves #6348, to investigate afterwards * fix lint
1 parent 735e8d1 commit 2a171a9

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

packages/instantsearch.js/src/lib/utils/__tests__/hydrateSearchClient-test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import algoliasearchV3 from 'algoliasearch-v3';
2+
import algoliasearchV4 from 'algoliasearch-v4';
3+
import { liteClient as algoliasearchV5 } from 'algoliasearch-v5/lite';
4+
15
import { hydrateSearchClient } from '../hydrateSearchClient';
26

37
import type { SearchClient, InitialResults } from '../../../types';
@@ -44,6 +48,7 @@ describe('hydrateSearchClient', () => {
4448
client = {
4549
transporter: { responsesCache: { set: setCache } },
4650
addAlgoliaAgent: jest.fn(),
51+
search: jest.fn(),
4752
} as unknown as SearchClient;
4853

4954
hydrateSearchClient(client, initialResults);
@@ -64,6 +69,7 @@ describe('hydrateSearchClient', () => {
6469
it('should populate the cache for < v4 if there is no transporter object', () => {
6570
client = {
6671
addAlgoliaAgent: jest.fn(),
72+
search: jest.fn(),
6773
_useCache: true,
6874
} as unknown as SearchClient;
6975

@@ -77,6 +83,7 @@ describe('hydrateSearchClient', () => {
7783
client = {
7884
transporter: { responsesCache: { set: setCache } },
7985
addAlgoliaAgent: jest.fn(),
86+
search: jest.fn(),
8087
} as unknown as SearchClient;
8188

8289
hydrateSearchClient(client, {
@@ -110,6 +117,7 @@ describe('hydrateSearchClient', () => {
110117
client = {
111118
transporter: { responsesCache: { set: setCache } },
112119
addAlgoliaAgent: jest.fn(),
120+
search: jest.fn(),
113121
} as unknown as SearchClient;
114122

115123
hydrateSearchClient(client, {
@@ -176,6 +184,7 @@ describe('hydrateSearchClient', () => {
176184
client = {
177185
transporter: { responsesCache: { set: setCache } },
178186
addAlgoliaAgent: jest.fn(),
187+
search: jest.fn(),
179188
} as unknown as SearchClient;
180189

181190
hydrateSearchClient(client, {
@@ -204,6 +213,7 @@ describe('hydrateSearchClient', () => {
204213
client = {
205214
transporter: { responsesCache: { set: jest.fn() } },
206215
addAlgoliaAgent: jest.fn(),
216+
search: jest.fn(),
207217
} as unknown as SearchClient;
208218

209219
hydrateSearchClient(client, {
@@ -221,6 +231,7 @@ describe('hydrateSearchClient', () => {
221231
client = {
222232
transporter: { responsesCache: { set: setCache } },
223233
addAlgoliaAgent: jest.fn(),
234+
search: jest.fn(),
224235
} as unknown as SearchClient;
225236

226237
hydrateSearchClient(client, {
@@ -235,4 +246,47 @@ describe('hydrateSearchClient', () => {
235246
{ results: [] }
236247
);
237248
});
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+
});
238292
});

packages/instantsearch.js/src/lib/utils/hydrateSearchClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function hydrateSearchClient(
8585
if ('transporter' in client && !client._cacheHydrated) {
8686
client._cacheHydrated = true;
8787

88-
const baseMethod = client.search as unknown as (
88+
const baseMethod = client.search.bind(client) as unknown as (
8989
query: any,
9090
...args: any[]
9191
) => any;

0 commit comments

Comments
 (0)