Skip to content

Commit

Permalink
fix(recommend): prevent undefined threshold (#1300)
Browse files Browse the repository at this point in the history
* fix(recommend): prevent `undefined` threshold

* fix: test `threshold` value
  • Loading branch information
shortcuts authored Aug 25, 2021
1 parent 8923c4e commit 7d21d3c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
72 changes: 72 additions & 0 deletions packages/recommend/src/__tests__/getRecommendations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
{}
);
});
});
4 changes: 2 additions & 2 deletions packages/recommend/src/methods/getRecommendations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7d21d3c

Please sign in to comment.