Skip to content

Commit 80db006

Browse files
authored
[Bugfix] Search and category filter sorting bug (#3888)
* Fix category filter sorting and filtering * Fix formatting
1 parent a9f0639 commit 80db006

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

packages/peregrine/lib/talons/FilterModal/__tests__/helpers.spec.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,74 @@ describe('#sortFiltersArray', () => {
171171
]
172172
`);
173173
});
174+
test('returns sorted filters array with entry containing category_uid attribute code', () => {
175+
const initialFiltersArray = [
176+
{
177+
attribute_code: 'code_c',
178+
label: 'C - Label',
179+
position: 0
180+
},
181+
{
182+
attribute_code: 'code_b',
183+
label: 'B - Label',
184+
position: 0
185+
},
186+
{
187+
attribute_code: 'code_f',
188+
label: 'Label - F',
189+
position: 20
190+
},
191+
{
192+
attribute_code: 'category_uid',
193+
label: 'label',
194+
position: null
195+
},
196+
{
197+
attribute_code: 'code_e',
198+
label: 'Label - E',
199+
position: 10
200+
},
201+
{
202+
attribute_code: 'code_a',
203+
label: 'A - Label',
204+
position: 0
205+
}
206+
];
207+
const sortedFilters = sortFiltersArray(initialFiltersArray);
208+
209+
expect(sortedFilters).toMatchInlineSnapshot(`
210+
Array [
211+
Object {
212+
"attribute_code": "category_uid",
213+
"label": "label",
214+
"position": null,
215+
},
216+
Object {
217+
"attribute_code": "code_a",
218+
"label": "A - Label",
219+
"position": 0,
220+
},
221+
Object {
222+
"attribute_code": "code_b",
223+
"label": "B - Label",
224+
"position": 0,
225+
},
226+
Object {
227+
"attribute_code": "code_c",
228+
"label": "C - Label",
229+
"position": 0,
230+
},
231+
Object {
232+
"attribute_code": "code_e",
233+
"label": "Label - E",
234+
"position": 10,
235+
},
236+
Object {
237+
"attribute_code": "code_f",
238+
"label": "Label - F",
239+
"position": 20,
240+
},
241+
]
242+
`);
243+
});
174244
});

packages/peregrine/lib/talons/FilterModal/__tests__/useFilterModal.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ jest.mock('@apollo/client', () => {
4646
{
4747
name: 'category_id'
4848
},
49+
{
50+
name: 'category_uid'
51+
},
4952
{
5053
name: 'foo'
5154
},
@@ -94,6 +97,20 @@ const defaultProps = {
9497
}
9598
]
9699
},
100+
{
101+
attribute_code: 'category_uid',
102+
label: 'Category 2',
103+
options: [
104+
{
105+
label: 'Bottoms',
106+
value: '28'
107+
},
108+
{
109+
label: 'Tops',
110+
value: '19'
111+
}
112+
]
113+
},
97114
{
98115
attribute_code: 'foo',
99116
label: 'Foo',
@@ -167,12 +184,15 @@ describe('#useFilterModal', () => {
167184
createTestInstance(<Component />);
168185
const { filterNames } = log.mock.calls[0][0];
169186
expect(filterNames.get('category_id')).toBeTruthy();
187+
expect(filterNames.get('category_uid')).toBeTruthy();
170188
});
171189

172190
it('only renders filters that are valid and enabled', () => {
173191
createTestInstance(<Component />);
174192
const { filterNames } = log.mock.calls[0][0];
175193
expect(filterNames.get('foo')).toBeTruthy();
194+
expect(filterNames.get('category_id')).toBeFalsy();
195+
expect(filterNames.get('category_uid')).toBeFalsy();
176196
});
177197

178198
it('renders boolean filters', () => {

packages/peregrine/lib/talons/FilterModal/helpers.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,16 @@ export const getFiltersFromSearch = initialValue => {
111111
export const sortFiltersArray = initialArray => {
112112
return initialArray.sort((a, b) => {
113113
// Place Category filter first
114-
if (a['attribute_code'] === 'category_id') {
114+
if (
115+
a['attribute_code'] === 'category_id' ||
116+
a['attribute_code'] === 'category_uid'
117+
) {
115118
return -1;
116119
}
117-
if (b['attribute_code'] === 'category_id') {
120+
if (
121+
b['attribute_code'] === 'category_id' ||
122+
b['attribute_code'] === 'category_uid'
123+
) {
118124
return 1;
119125
}
120126

packages/peregrine/lib/talons/FilterModal/useFilterModal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const useFilterModal = props => {
5555
// Disable category filtering when not on a search page.
5656
if (pathname !== '/search.html') {
5757
disabled.add('category_id');
58+
disabled.add('category_uid');
5859
}
5960

6061
return disabled;

packages/peregrine/lib/talons/FilterSidebar/__tests__/useFilterSidebar.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ jest.mock('@apollo/client', () => {
4646
{
4747
name: 'category_id'
4848
},
49+
{
50+
name: 'category_uid'
51+
},
4952
{
5053
name: 'foo'
5154
},
@@ -94,6 +97,20 @@ const defaultProps = {
9497
}
9598
]
9699
},
100+
{
101+
attribute_code: 'category_uid',
102+
label: 'Category 2',
103+
options: [
104+
{
105+
label: 'Bottoms',
106+
value: '28'
107+
},
108+
{
109+
label: 'Tops',
110+
value: '19'
111+
}
112+
]
113+
},
97114
{
98115
attribute_code: 'foo',
99116
label: 'Foo',
@@ -167,12 +184,15 @@ describe('#useFilterSidebar', () => {
167184
createTestInstance(<Component />);
168185
const { filterNames } = log.mock.calls[0][0];
169186
expect(filterNames.get('category_id')).toBeTruthy();
187+
expect(filterNames.get('category_uid')).toBeTruthy();
170188
});
171189

172190
it('only renders filters that are valid and enabled', () => {
173191
createTestInstance(<Component />);
174192
const { filterNames } = log.mock.calls[0][0];
175193
expect(filterNames.get('foo')).toBeTruthy();
194+
expect(filterNames.get('category_id')).toBeFalsy();
195+
expect(filterNames.get('category_uid')).toBeFalsy();
176196
});
177197

178198
it('writes filter state to history when "isApplying"', () => {

packages/peregrine/lib/talons/FilterSidebar/useFilterSidebar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const useFilterSidebar = props => {
4545
// Disable category filtering when not on a search page.
4646
if (pathname !== '/search.html') {
4747
disabled.add('category_id');
48+
disabled.add('category_uid');
4849
}
4950

5051
return disabled;

0 commit comments

Comments
 (0)