-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(headless): consider only selected value ancestry for analytics, h…
…istory and breadcrumbs (#3137) * expunge parents * clean+ut * refactor catfacetval interfaces to reflect common props https://coveord.atlassian.net/browse/KIT-2696 * clean up getActiveValueFromValueTree * Apply suggestions from code review Co-authored-by: dmbrooke <38883189+dmbrooke@users.noreply.github.com> --------- Co-authored-by: dmbrooke <38883189+dmbrooke@users.noreply.github.com>
- Loading branch information
1 parent
fec8cbf
commit fdc644b
Showing
17 changed files
with
182 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 51 additions & 50 deletions
101
packages/headless/src/features/facets/category-facet-set/category-facet-utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,58 @@ | ||
import {buildMockCategoryFacetValue} from '../../../test/mock-category-facet-value'; | ||
import {getActiveValueFromValueTree} from './category-facet-utils'; | ||
import {findActiveValueAncestry} from './category-facet-utils'; | ||
import {CategoryFacetValue} from './interfaces/response'; | ||
|
||
describe('#getActiveValueFromValueTree', () => { | ||
it("should return undefined if there's no selected values", () => { | ||
expect(getActiveValueFromValueTree([])).toBeUndefined(); | ||
describe('#findActiveValueAncestry', () => { | ||
it("should return an empty array if there's no selected values", () => { | ||
expect(findActiveValueAncestry([])).toEqual([]); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
caseName: 'doubleRootValues', | ||
getValues: (expectedValue: CategoryFacetValue) => [ | ||
expectedValue, | ||
buildMockCategoryFacetValue({state: 'selected'}), | ||
], | ||
}, | ||
{ | ||
caseName: 'rootAndNestedValues', | ||
getValues: (expectedValue: CategoryFacetValue) => [ | ||
buildMockCategoryFacetValue({children: [expectedValue]}), | ||
buildMockCategoryFacetValue({state: 'selected'}), | ||
], | ||
}, | ||
{ | ||
caseName: 'doubleNestedValues', | ||
getValues: (expectedValue: CategoryFacetValue) => [ | ||
buildMockCategoryFacetValue({children: [expectedValue]}), | ||
buildMockCategoryFacetValue({ | ||
children: [buildMockCategoryFacetValue({state: 'selected'})], | ||
}), | ||
], | ||
}, | ||
{ | ||
caseName: 'singleNestedValue', | ||
getValues: (expectedValue: CategoryFacetValue) => [ | ||
buildMockCategoryFacetValue({children: [expectedValue]}), | ||
describe('when there is a value selected', () => { | ||
const selectedValue = buildMockCategoryFacetValue({ | ||
state: 'selected', | ||
value: 'A', | ||
}); | ||
const notAncestorValue = buildMockCategoryFacetValue(); | ||
|
||
const buildAncestor = (children: CategoryFacetValue[]) => { | ||
return buildMockCategoryFacetValue({children}); | ||
}; | ||
|
||
const hierarchicalTestCases = [ | ||
() => [notAncestorValue, buildAncestor([selectedValue])], | ||
() => [buildAncestor([notAncestorValue, selectedValue])], | ||
() => [ | ||
buildAncestor([ | ||
notAncestorValue, | ||
buildAncestor([selectedValue, notAncestorValue]), | ||
]), | ||
], | ||
}, | ||
{ | ||
caseName: 'singleRootValue', | ||
getValues: (expectedValue: CategoryFacetValue) => [expectedValue], | ||
}, | ||
])( | ||
'should return the first selected values found while doing a depth first search - $caseName', | ||
({getValues}) => { | ||
const expectedValues = buildMockCategoryFacetValue({ | ||
state: 'selected', | ||
value: 'A', | ||
}); | ||
|
||
const values: CategoryFacetValue[] = getValues(expectedValues); | ||
|
||
expect(getActiveValueFromValueTree(values)).toBe(expectedValues); | ||
} | ||
); | ||
]; | ||
|
||
it('should return an array containing only the selected Value if the selected value is at the root', () => { | ||
expect(findActiveValueAncestry([selectedValue])).toEqual([selectedValue]); | ||
}); | ||
|
||
it.each(hierarchicalTestCases)( | ||
'should return an array containing the selected value whole ancestry', | ||
(generateValues) => { | ||
expect(findActiveValueAncestry(generateValues())).not.toContain( | ||
notAncestorValue | ||
); | ||
} | ||
); | ||
|
||
it.each(hierarchicalTestCases)( | ||
'should return an array containing the ancestors in order, from the root to the selected value', | ||
(generateValue) => { | ||
const ancestry = findActiveValueAncestry(generateValue()); | ||
|
||
expect(ancestry.pop()).toBe(selectedValue); | ||
expect(ancestry[ancestry.length - 1].children).toContain(selectedValue); | ||
for (let i = 0; i < ancestry.length - 2; i++) { | ||
expect(ancestry[i].children).toContain(ancestry[i + 1]); | ||
} | ||
} | ||
); | ||
}); | ||
}); |
Oops, something went wrong.