Skip to content

Commit

Permalink
fix(searchresponse.ts): bugfix in transformSearchResponse transformin…
Browse files Browse the repository at this point in the history
…g badge fields
  • Loading branch information
chrisFrazier77 committed Dec 18, 2024
1 parent 2c9561e commit 4231094
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
46 changes: 46 additions & 0 deletions packages/snap-client/src/Client/transforms/searchResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/snap-client/src/Client/transforms/searchResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
});
};
Expand Down

0 comments on commit 4231094

Please sign in to comment.