From 7d21d3c7c02b200068afa81696eb83361599915b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= <20689156+shortcuts@users.noreply.github.com> Date: Wed, 25 Aug 2021 17:25:53 +0200 Subject: [PATCH] fix(recommend): prevent `undefined` threshold (#1300) * fix(recommend): prevent `undefined` threshold * fix: test `threshold` value --- .../src/__tests__/getRecommendations.test.ts | 72 +++++++++++++++++++ .../src/methods/getRecommendations.ts | 4 +- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/packages/recommend/src/__tests__/getRecommendations.test.ts b/packages/recommend/src/__tests__/getRecommendations.test.ts index 4b998be0b..b9c0f4d16 100644 --- a/packages/recommend/src/__tests__/getRecommendations.test.ts +++ b/packages/recommend/src/__tests__/getRecommendations.test.ts @@ -125,4 +125,76 @@ describe('getRecommendations', () => { {} ); }); + + test('overrides `undefined` threshold with default value', async () => { + const client = createMockedClient(); + + await client.getRecommendations( + [ + { + model: 'bought-together', + indexName: 'products', + objectID: 'B018APC4LE', + threshold: undefined, + }, + ], + {} + ); + + expect(client.transporter.read).toHaveBeenCalledTimes(1); + expect(client.transporter.read).toHaveBeenCalledWith( + { + cacheable: true, + data: { + requests: [ + { + indexName: 'products', + model: 'bought-together', + objectID: 'B018APC4LE', + threshold: 0, + }, + ], + }, + method: 'POST', + path: '1/indexes/*/recommendations', + }, + {} + ); + }); + + test('overrides default threshold value', async () => { + const client = createMockedClient(); + + await client.getRecommendations( + [ + { + model: 'bought-together', + indexName: 'products', + objectID: 'B018APC4LE', + threshold: 42, + }, + ], + {} + ); + + expect(client.transporter.read).toHaveBeenCalledTimes(1); + expect(client.transporter.read).toHaveBeenCalledWith( + { + cacheable: true, + data: { + requests: [ + { + indexName: 'products', + model: 'bought-together', + objectID: 'B018APC4LE', + threshold: 42, + }, + ], + }, + method: 'POST', + path: '1/indexes/*/recommendations', + }, + {} + ); + }); }); diff --git a/packages/recommend/src/methods/getRecommendations.ts b/packages/recommend/src/methods/getRecommendations.ts index 2452a35ee..5690ddaa5 100644 --- a/packages/recommend/src/methods/getRecommendations.ts +++ b/packages/recommend/src/methods/getRecommendations.ts @@ -9,11 +9,11 @@ type GetRecommendations = ( export const getRecommendations: GetRecommendations = base => { return (queries: readonly RecommendationsQuery[], requestOptions) => { const requests: readonly RecommendationsQuery[] = queries.map(query => ({ + ...query, // The `threshold` param is required by the endpoint to make it easier // to provide a default value later, so we default it in the client // so that users don't have to provide a value. - threshold: 0, - ...query, + threshold: query.threshold || 0, })); return base.transporter.read(