-
Notifications
You must be signed in to change notification settings - Fork 34
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
feat(atomic, headless): add support for sort criteria alphanumericNatural #4493
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import {FacetSortCriterion} from '@coveo/headless'; | ||
import {orderBy} from 'natural-orderby'; | ||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think adding a dependency is necessary for tests. Is there other ways you can test this ? Or at least put it in devDependencies ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep def should be in dev deps. Extracting the sorting logic from natural-orderby would be too much work. I tried with natural-compare-lite and javascript-natural-sort, which are already in our deps, but they don't sort in the same way as the index... |
||
import {test, expect} from './fixture'; | ||
|
||
test.describe('when selecting the facet search "More matches for" button', () => { | ||
|
@@ -151,3 +153,81 @@ test.describe('when the "Show more" button has been selected', () => { | |
}); | ||
}); | ||
}); | ||
|
||
const sortCriteriaTests: { | ||
criteria: FacetSortCriterion; | ||
sortFunction: (values: string[]) => string[]; | ||
}[] = [ | ||
{ | ||
criteria: 'alphanumeric', | ||
sortFunction: (values: string[]) => [...values].sort(), | ||
}, | ||
{ | ||
criteria: 'alphanumericDescending', | ||
sortFunction: (values: string[]) => [...values].sort().reverse(), | ||
}, | ||
{ | ||
criteria: 'alphanumericNatural', | ||
sortFunction: (values: string[]) => | ||
orderBy([...values], [(value) => value], 'asc'), | ||
}, | ||
{ | ||
criteria: 'alphanumericNaturalDescending', | ||
sortFunction: (values: string[]) => | ||
orderBy([...values], [(value) => value], ['desc']), | ||
}, | ||
]; | ||
|
||
test.describe('Sort Criteria', () => { | ||
sortCriteriaTests.forEach(({criteria, sortFunction}) => { | ||
test.describe(`when sort criteria is set to "${criteria}"`, () => { | ||
test.beforeEach(async ({facet}) => { | ||
await facet.load({ | ||
args: { | ||
sortCriteria: criteria, | ||
field: 'cat_available_sizes', | ||
label: 'Size', | ||
numberOfValues: 30, | ||
}, | ||
}); | ||
await facet.hydrated.waitFor(); | ||
await expect.poll(async () => await facet.facetValue.count()).toBe(30); | ||
}); | ||
|
||
test(`should have facet values sorted by ${criteria}`, async ({ | ||
facet, | ||
}) => { | ||
const values = await facet.facetValueLabel.allTextContents(); | ||
const sortedValues = sortFunction(values); | ||
expect(values).toEqual(sortedValues); | ||
}); | ||
}); | ||
}); | ||
|
||
test.describe('when sort criteria is set to "occurrences"', () => { | ||
test.beforeEach(async ({facet}) => { | ||
await facet.load({ | ||
args: { | ||
sortCriteria: 'occurrences', | ||
field: 'cat_available_sizes', | ||
label: 'Size', | ||
numberOfValues: 30, | ||
}, | ||
}); | ||
await facet.hydrated.waitFor(); | ||
await expect.poll(async () => await facet.facetValue.count()).toBe(30); | ||
}); | ||
|
||
test('should have facet values sorted by occurrences', async ({facet}) => { | ||
const values = await facet.facetValueOccurrences.allTextContents(); | ||
const sortedValues = [...values] | ||
.sort((a, b) => { | ||
const numA = parseInt(a.replace(/[^\d]/g, ''), 10); | ||
const numB = parseInt(b.replace(/[^\d]/g, ''), 10); | ||
return numA - numB; | ||
}) | ||
.reverse(); | ||
expect(values).toEqual(sortedValues); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The interface
FacetSortCriterion
does not contain alphanumericNatural' & alphanumericNaturalDescending.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR adds them!