Skip to content

Commit

Permalink
fix(client): use client.search in sffv for v5 (#6354)
Browse files Browse the repository at this point in the history
* fix(client): use client.search in sffv for v5

v5 has a `searchForFacetValues` function, but it's single-index only. We thus need to detect it and use `client.search` directly. In a future major we can change this and only use `client.search`

* hup lint fixed
  • Loading branch information
Haroenv authored Sep 10, 2024
1 parent 2a171a9 commit 11399e6
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 30 deletions.
5 changes: 4 additions & 1 deletion packages/algoliasearch-helper/src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ AlgoliaSearchHelper.prototype.searchForFacetValues = function (
maxFacetHits,
userState
) {
var clientHasSFFV = typeof this.client.searchForFacetValues === 'function';
var clientHasSFFV =
typeof this.client.searchForFacetValues === 'function' &&
// v5 has a wrong sffv signature
typeof this.client.searchForFacets !== 'function';
var clientHasInitIndex = typeof this.client.initIndex === 'function';
if (
!clientHasSFFV &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var algoliasearch = require('algoliasearch');
var isV5 = (algoliasearch.apiClientVersion || '')[0] === '5';
algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliasearchHelper = require('../../../index');
Expand Down Expand Up @@ -71,43 +72,85 @@ test('When searchOnce with promises, hasPendingRequests is true', function (done
triggerCb();
});

test('When searchForFacetValues, hasPendingRequests is true', function (done) {
var client = algoliasearch('dsf', 'dsfdf');
if (!isV5) {
test('When searchForFacetValues, hasPendingRequests is true (v3, v4)', function (done) {
var client = algoliasearch('dsf', 'dsfdf');

let triggerCb;
client.searchForFacetValues = function () {
return new Promise(function (resolve) {
triggerCb = function () {
resolve([
{
exhaustiveFacetsCount: true,
facetHits: [],
processingTimeMS: 3,
},
]);
};
});
};

var triggerCb;
client.searchForFacetValues = function () {
return new Promise(function (resolve) {
triggerCb = function () {
resolve([
{
exhaustiveFacetsCount: true,
facetHits: [],
processingTimeMS: 3,
},
]);
};
var helper = algoliasearchHelper(client, 'test_hotels-node');
var countNoMoreSearch = 0;
helper.on('searchQueueEmpty', function () {
countNoMoreSearch += 1;
});
};

var helper = algoliasearchHelper(client, 'test_hotels-node');
var countNoMoreSearch = 0;
helper.on('searchQueueEmpty', function () {
countNoMoreSearch += 1;
expect(helper.hasPendingRequests()).toBe(false);

helper.searchForFacetValues('').then(function () {
expect(helper.hasPendingRequests()).toBe(false);
expect(countNoMoreSearch).toBe(1);
done();
});

expect(helper.hasPendingRequests()).toBe(true);
expect(countNoMoreSearch).toBe(0);

triggerCb();
});
} else {
test('When searchForFacetValues, hasPendingRequests is true (v5)', function (done) {
var client = algoliasearch('dsf', 'dsfdf');

let triggerCb;
client.search = function () {
return new Promise(function (resolve) {
triggerCb = function () {
resolve({
results: [
{
exhaustiveFacetsCount: true,
facetHits: [],
processingTimeMS: 3,
},
],
});
};
});
};

expect(helper.hasPendingRequests()).toBe(false);
var helper = algoliasearchHelper(client, 'test_hotels-node');
var countNoMoreSearch = 0;
helper.on('searchQueueEmpty', function () {
countNoMoreSearch += 1;
});

helper.searchForFacetValues('').then(function () {
expect(helper.hasPendingRequests()).toBe(false);
expect(countNoMoreSearch).toBe(1);
done();
});

expect(helper.hasPendingRequests()).toBe(true);
expect(countNoMoreSearch).toBe(0);
helper.searchForFacetValues('').then(function () {
expect(helper.hasPendingRequests()).toBe(false);
expect(countNoMoreSearch).toBe(1);
done();
});

triggerCb();
});
expect(helper.hasPendingRequests()).toBe(true);
expect(countNoMoreSearch).toBe(0);

triggerCb();
});
}

test('When helper.search(), hasPendingRequests is true', function (done) {
var testData = require('../../datasets/SearchParameters/search.dataset')();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ test('searchForFacetValues calls the index method if no client method', function
});
});

test('searchForFacetValues calls client.search if client.searchForFacets exists', function () {
var clientSearch = jest.fn(function () {
return Promise.resolve({
results: [makeFakeSearchForFacetValuesResponse()],
});
});

var fakeClient = {
searchForFacets: jest.fn(),
searchForFacetValues: jest.fn(),
search: clientSearch,
};

var helper = algoliasearchHelper(fakeClient, 'index');

return helper.searchForFacetValues('facet', 'query', 1).then(function () {
expect(clientSearch).toHaveBeenCalledTimes(1);
});
});

test('searchForFacetValues resolve with the correct response from client', function () {
var fakeClient = {
addAlgoliaAgent: function () {},
Expand Down
13 changes: 12 additions & 1 deletion packages/algoliasearch-helper/types/algoliasearch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,17 @@ export type SupportedLanguage = PickForClient<{
v5: AlgoliaSearch.SupportedLanguage;
}>;

// v5 only has the `searchForFacetValues` method in the `search` client, not in `lite`.
// We need to check both clients to get the correct type.
// (this is not actually used in the codebase, but it's here for completeness)
type SearchForFacetValuesV5 = ClientSearchV5 | ClientFullV5 extends {
searchForFacetValues: unknown;
}
?
| ClientSearchV5['searchForFacetValues']
| ClientFullV5['searchForFacetValues']
: never;

export interface SearchClient {
search: <T>(
requests: Array<{ indexName: string; params: SearchOptions }>
Expand All @@ -317,7 +328,7 @@ export interface SearchClient {
searchForFacetValues: unknown;
}
? DefaultSearchClient['searchForFacetValues']
: never;
: SearchForFacetValuesV5;
initIndex?: DefaultSearchClient extends { initIndex: unknown }
? DefaultSearchClient['initIndex']
: never;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,7 @@ function createMockedSearchClient() {
return Promise.resolve([
createSFFVResponse({
facetHits:
// @ts-ignore for v5, which has a different definition of `searchForFacetValues`
requests[0].params.facetQuery === 'query with no results'
? []
: [
Expand Down

0 comments on commit 11399e6

Please sign in to comment.