From 4231094a19932fd920f107286f40a61281d27944 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 18 Dec 2024 09:55:35 -0800 Subject: [PATCH 1/2] fix(searchresponse.ts): bugfix in transformSearchResponse transforming badge fields --- .../Client/transforms/searchResponse.test.ts | 46 +++++++++++++++++++ .../src/Client/transforms/searchResponse.ts | 4 +- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/snap-client/src/Client/transforms/searchResponse.test.ts b/packages/snap-client/src/Client/transforms/searchResponse.test.ts index 0f532e2f7..3746ed258 100644 --- a/packages/snap-client/src/Client/transforms/searchResponse.test.ts +++ b/packages/snap-client/src/Client/transforms/searchResponse.test.ts @@ -420,6 +420,52 @@ describe('search response transformer result', () => { // @ts-ignore expect(transformSearchResponse.results({}).results instanceof Array).toEqual(true); }); + + it('search response search transforms badges', () => { + const resultWithBadgeFeature = { + ...mockSingleResult, + badges: [ + { + tag: 'qa-badge-2910', + value: 'QA Badge 2910', + }, + { + tag: 'qa-off-29', + value: 'QA Off 29', + }, + { + tag: 'gift-guide', + value: 'Gift Guide', + }, + ], + }; + const resultWithRandomBadgeField = { + ...mockSingleResult, + badges: ['1', '2', '3'], + }; + const resultWithRandomBadgeField2 = { + ...mockSingleResult, + badges: { + name: '1', + name2: '2', + name3: '3', + }, + }; + + const result = transformSearchResponse.result(resultWithBadgeFeature); + expect(result.attributes?.badges).toBeUndefined(); + expect(result.badges).toEqual(resultWithBadgeFeature.badges); + + // @ts-ignore - typings are wrong intentionally here + const result2 = transformSearchResponse.result(resultWithRandomBadgeField); + expect(result2.attributes?.badges).toEqual(resultWithRandomBadgeField.badges); + expect(result2.badges).toEqual([]); + + // @ts-ignore - typings are wrong intentionally here + const result3 = transformSearchResponse.result(resultWithRandomBadgeField2); + expect(result3.badges).toEqual([]); + expect(result3.attributes?.badges).toEqual('[object Object]'); + }); }); describe('search response facet transformer', () => { diff --git a/packages/snap-client/src/Client/transforms/searchResponse.ts b/packages/snap-client/src/Client/transforms/searchResponse.ts index 89ba894a0..8bf53dbde 100644 --- a/packages/snap-client/src/Client/transforms/searchResponse.ts +++ b/packages/snap-client/src/Client/transforms/searchResponse.ts @@ -214,7 +214,7 @@ transformSearchResponse.result = (rawResult: rawResult): SearchResponseModelResu const attributes = Object.keys(rawResult) .filter((k) => CORE_FIELDS.indexOf(k) == -1) // remove 'badges' from attributes - but only if it is an object - .filter((k) => !(k == 'badges' && typeof rawResult[k] == 'object' && !Array.isArray(rawResult[k]))) + .filter((k) => !(k == 'badges' && typeof rawResult[k] == 'object' && Array.isArray(rawResult[k]) && typeof rawResult[k]?.[0] == 'object')) .reduce((attributes, key) => { return { ...attributes, @@ -242,7 +242,7 @@ transformSearchResponse.result = (rawResult: rawResult): SearchResponseModelResu core: coreFieldValues, }, attributes, - badges: typeof rawResult.badges == 'object' && !Array.isArray(rawResult.badges) ? rawResult.badges : [], + badges: typeof rawResult.badges == 'object' && Array.isArray(rawResult.badges) && typeof rawResult.badges[0] == 'object' ? rawResult.badges : [], children, }); }; From d0fdf356a2d3c6309e7e6346167430bd1f3ae5ab Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 19 Dec 2024 08:39:00 -0800 Subject: [PATCH 2/2] refactor(searchresponse.ts): remove unneeded check --- packages/snap-client/src/Client/transforms/searchResponse.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/snap-client/src/Client/transforms/searchResponse.ts b/packages/snap-client/src/Client/transforms/searchResponse.ts index 8bf53dbde..5def26e03 100644 --- a/packages/snap-client/src/Client/transforms/searchResponse.ts +++ b/packages/snap-client/src/Client/transforms/searchResponse.ts @@ -214,7 +214,7 @@ transformSearchResponse.result = (rawResult: rawResult): SearchResponseModelResu const attributes = Object.keys(rawResult) .filter((k) => CORE_FIELDS.indexOf(k) == -1) // remove 'badges' from attributes - but only if it is an object - .filter((k) => !(k == 'badges' && typeof rawResult[k] == 'object' && Array.isArray(rawResult[k]) && typeof rawResult[k]?.[0] == 'object')) + .filter((k) => !(k == 'badges' && Array.isArray(rawResult[k]) && typeof rawResult[k]?.[0] == 'object')) .reduce((attributes, key) => { return { ...attributes, @@ -242,7 +242,7 @@ transformSearchResponse.result = (rawResult: rawResult): SearchResponseModelResu core: coreFieldValues, }, attributes, - badges: typeof rawResult.badges == 'object' && Array.isArray(rawResult.badges) && typeof rawResult.badges[0] == 'object' ? rawResult.badges : [], + badges: Array.isArray(rawResult.badges) && typeof rawResult.badges[0] == 'object' ? rawResult.badges : [], children, }); };