Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): ensure client.search is bound #6352

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: any = 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: any = 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