Skip to content

fix(recommend): prevent undefined threshold #1300

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

Merged
merged 2 commits into from
Aug 25, 2021
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
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