Skip to content

Commit

Permalink
fix(client): ensure client.search is bound
Browse files Browse the repository at this point in the history
fixes #6350
fixes algolia/algoliasearch-client-javascript#1549
possibly also solves #6348, to investigate afterwards
  • Loading branch information
Haroenv committed Sep 9, 2024
1 parent 735e8d1 commit 57dfd44
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import algoliasearchV3 from 'algoliasearch-v3';
import algoliasearchV4 from 'algoliasearch-v4';
import { liteClient as algoliasearchV5 } from 'algoliasearch-v5/lite';

import { hydrateSearchClient } from '../hydrateSearchClient';

import type { SearchClient, InitialResults } from '../../../types';
Expand Down Expand Up @@ -44,6 +48,7 @@ describe('hydrateSearchClient', () => {
client = {
transporter: { responsesCache: { set: setCache } },
addAlgoliaAgent: jest.fn(),
search: jest.fn(),
} as unknown as SearchClient;

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

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

hydrateSearchClient(client, {
Expand Down Expand Up @@ -110,6 +117,7 @@ describe('hydrateSearchClient', () => {
client = {
transporter: { responsesCache: { set: setCache } },
addAlgoliaAgent: jest.fn(),
search: jest.fn(),
} as unknown as SearchClient;

hydrateSearchClient(client, {
Expand Down Expand Up @@ -176,6 +184,7 @@ describe('hydrateSearchClient', () => {
client = {
transporter: { responsesCache: { set: setCache } },
addAlgoliaAgent: jest.fn(),
search: jest.fn(),
} as unknown as SearchClient;

hydrateSearchClient(client, {
Expand Down Expand Up @@ -204,6 +213,7 @@ describe('hydrateSearchClient', () => {
client = {
transporter: { responsesCache: { set: jest.fn() } },
addAlgoliaAgent: jest.fn(),
search: jest.fn(),
} as unknown as SearchClient;

hydrateSearchClient(client, {
Expand All @@ -221,6 +231,7 @@ describe('hydrateSearchClient', () => {
client = {
transporter: { responsesCache: { set: setCache } },
addAlgoliaAgent: jest.fn(),
search: jest.fn(),
} as unknown as SearchClient;

hydrateSearchClient(client, {
Expand All @@ -235,4 +246,47 @@ describe('hydrateSearchClient', () => {
{ results: [] }
);
});

it('should not throw if search requires to be bound (v5)', async () => {
const send = jest.fn().mockResolvedValue({ status: 200, content: '{}' });
const searchClient = algoliasearchV5('appId', 'apiKey', {
requester: {
send,
},
});

hydrateSearchClient(searchClient, initialResults);

await searchClient.search([{ indexName: 'another', params: {} }]);

expect(send).toHaveBeenCalled();
});

it('should not throw if search requires to be bound (v4)', async () => {
const send = jest.fn().mockResolvedValue({ status: 200, content: '{}' });
const searchClient = algoliasearchV4('appId', 'apiKey', {
requester: {
send,
},
});

hydrateSearchClient(searchClient, initialResults);

await searchClient.search([{ indexName: 'another', params: {} }]);

expect(send).toHaveBeenCalled();
});

it('should not throw if search requires to be bound (v3)', async () => {
const searchClient: any = algoliasearchV3('appId', 'apiKey');
searchClient._request = jest
.fn()
.mockResolvedValue({ body: { status: 200 } });

hydrateSearchClient(searchClient, initialResults);

await searchClient.search([{ indexName: 'another', params: {} }]);

expect(searchClient._request).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function hydrateSearchClient(
if ('transporter' in client && !client._cacheHydrated) {
client._cacheHydrated = true;

const baseMethod = client.search as unknown as (
const baseMethod = client.search.bind(client) as unknown as (
query: any,
...args: any[]
) => any;
Expand Down

0 comments on commit 57dfd44

Please sign in to comment.