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..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]))) + .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) ? rawResult.badges : [], + badges: Array.isArray(rawResult.badges) && typeof rawResult.badges[0] == 'object' ? rawResult.badges : [], children, }); };