Skip to content
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

Transform Badges Bugfix #1230

Merged
merged 2 commits into from
Dec 19, 2024
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
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' && 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: Array.isArray(rawResult.badges) && typeof rawResult.badges[0] == 'object' ? rawResult.badges : [],
children,
});
};
Expand Down
Loading