From f60a634be3f452f59d0b92f2c84962e7c5c96907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Thu, 24 Jun 2021 15:19:41 +0200 Subject: [PATCH] test(recommend): add test for sorting --- .../__tests__/mapToRecommendations.test.ts | 129 ++++++++++++++++++ .../src/utils/mapToRecommendations.ts | 2 +- 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 packages/recommend-core/src/utils/__tests__/mapToRecommendations.test.ts diff --git a/packages/recommend-core/src/utils/__tests__/mapToRecommendations.test.ts b/packages/recommend-core/src/utils/__tests__/mapToRecommendations.test.ts new file mode 100644 index 00000000..5fb8ae2f --- /dev/null +++ b/packages/recommend-core/src/utils/__tests__/mapToRecommendations.test.ts @@ -0,0 +1,129 @@ +import { mapToRecommendations } from '../mapToRecommendations'; + +const response = { + results: [ + { + exhaustiveNbHits: true, + hits: [ + { + objectID: '1-1', + name: 'Product 1-1', + _score: 100, + }, + { + objectID: '1-2', + name: 'Product 1-2', + _score: 90, + }, + { + objectID: '1-3', + name: 'Product 1-3', + _score: 70, + }, + { + objectID: '1-4', + name: 'Product 1-4', + }, + { + objectID: '1-5', + name: 'Product 1-5', + }, + ], + hitsPerPage: 10, + nbHits: 10, + nbPages: 1, + page: 0, + processingTimeMS: 1, + }, + { + exhaustiveNbHits: true, + hits: [ + { + objectID: '2-1', + name: 'Product 2-1', + _score: 100, + }, + { + objectID: '2-2', + name: 'Product 2-2', + _score: 90, + }, + { + objectID: '2-3', + name: 'Product 2-3', + _score: 70, + }, + { + objectID: '2-4', + name: 'Product 2-4', + }, + { + objectID: '2-5', + name: 'Product 2-5', + }, + ], + hitsPerPage: 10, + nbHits: 10, + nbPages: 1, + page: 0, + processingTimeMS: 1, + }, + ], +}; + +describe('mapToRecommendations', () => { + test('sorts the items based on their scores', () => { + const result = mapToRecommendations({ response: response as any }); + + expect(result).toMatchInlineSnapshot(` + Array [ + Object { + "_score": 100, + "name": "Product 1-1", + "objectID": "1-1", + }, + Object { + "_score": 100, + "name": "Product 2-1", + "objectID": "2-1", + }, + Object { + "_score": 90, + "name": "Product 1-2", + "objectID": "1-2", + }, + Object { + "_score": 90, + "name": "Product 2-2", + "objectID": "2-2", + }, + Object { + "_score": 70, + "name": "Product 1-3", + "objectID": "1-3", + }, + Object { + "_score": 70, + "name": "Product 2-3", + "objectID": "2-3", + }, + Object { + "name": "Product 1-4", + "objectID": "1-4", + }, + Object { + "name": "Product 1-5", + "objectID": "1-5", + }, + Object { + "name": "Product 2-4", + "objectID": "2-4", + }, + Object { + "name": "Product 2-5", + "objectID": "2-5", + }, + ] + `); + }); +}); diff --git a/packages/recommend-core/src/utils/mapToRecommendations.ts b/packages/recommend-core/src/utils/mapToRecommendations.ts index 37906e6a..8baf630f 100644 --- a/packages/recommend-core/src/utils/mapToRecommendations.ts +++ b/packages/recommend-core/src/utils/mapToRecommendations.ts @@ -21,7 +21,7 @@ export function mapToRecommendations({ const scoreA = a._score || 0; const scoreB = b._score || 0; - return scoreA < scoreB ? 1 : -1; + return scoreA > scoreB ? -1 : 1; }, // Multiple identical recommended `objectID`s can be returned b // the engine, so we need to remove duplicates.