Skip to content

Commit

Permalink
Support in facet-set-slice, UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbrooke committed Aug 22, 2023
1 parent 8226d67 commit 4f88e95
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const facetOptionsReducer = createReducer(
.addCase(restoreSearchParameters, (state, action) => {
[
...Object.keys(action.payload.f ?? {}),
...Object.keys(action.payload.fExcluded ?? {}),
...Object.keys(action.payload.cf ?? {}),
...Object.keys(action.payload.nf ?? {}),
...Object.keys(action.payload.df ?? {}),
Expand Down
13 changes: 11 additions & 2 deletions packages/headless/src/features/facets/facet-set/facet-set-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,27 @@ export const facetSetReducer = createReducer(
})
.addCase(restoreSearchParameters, (state, action) => {
const f = action.payload.f || {};
const fExcluded = action.payload.fExcluded || {};
const facetIds = Object.keys(state);

facetIds.forEach((id) => {
const {request} = state[id]!;
const selectedValues = f[id] || [];
const excludedValues = fExcluded[id] || [];
const idleValues = request.currentValues.filter(
(facetValue) => !selectedValues.includes(facetValue.value)
(facetValue) =>
!selectedValues.includes(facetValue.value) &&
!excludedValues.includes(facetValue.value)
);

request.currentValues = [
...selectedValues.map(buildSelectedFacetValueRequest),
...excludedValues.map(buildExcludedFacetValueRequest),
...idleValues.map(restoreFacetValueToIdleState),
];
request.preventAutoSelect = selectedValues.length > 0;
request.numberOfValues = Math.max(
selectedValues.length,
selectedValues.length + excludedValues.length,
request.numberOfValues
);
});
Expand Down Expand Up @@ -299,6 +304,10 @@ function buildSelectedFacetValueRequest(value: string): FacetValueRequest {
return {value, state: 'selected'};
}

function buildExcludedFacetValueRequest(value: string): FacetValueRequest {
return {value, state: 'excluded'};
}

function restoreFacetValueToIdleState(
facetValue: FacetValueRequest
): FacetValueRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
logFacetClearAll,
logFacetDeselect,
logFacetSelect,
logFacetExclude,
} from '../facets/facet-set/facet-set-analytics-actions';
import {
logPageNumber,
Expand Down Expand Up @@ -53,11 +54,13 @@ describe('logParametersChange', () => {
);
});

testFacetLogging('f', expectIdenticalActionType);
testFacetSelectLogging('f', expectIdenticalActionType);

testFacetLogging('af', expectIdenticalActionType);
testFacetSelectLogging('af', expectIdenticalActionType);

testFacetLogging('cf', expectIdenticalActionType);
testFacetSelectLogging('cf', expectIdenticalActionType);

testFacetExcludeLogging(expectIdenticalActionType);

it('should log a generic #logInterfaceChange when an unmanaged parameter', () => {
expectIdenticalActionType(
Expand All @@ -67,20 +70,66 @@ describe('logParametersChange', () => {
});
});

function testFacetLogging(
function testFacetSelectLogging(
parameter: string,
expectIdenticalActionType: (
action1: SearchAction,
action2: SearchAction
) => void
) {
testFacetLogging(parameter, expectIdenticalActionType);

it(`should log #logFacetSelect when an ${parameter} parameter is added`, () => {
expectIdenticalActionType(
logParametersChange({}, {[parameter]: {author: ['Cervantes']}}),
logFacetSelect({facetId: 'author', facetValue: 'Cervantes'})
);
});

it(`should log #logFacetSelect when an ${parameter} parameter is modified & a value added`, () => {
expectIdenticalActionType(
logParametersChange(
{[parameter]: {author: ['Cervantes']}},
{[parameter]: {author: ['Cervantes', 'Orwell']}}
),
logFacetSelect({facetId: 'author', facetValue: 'Orwell'})
);
});
}

function testFacetExcludeLogging(
expectIdenticalActionType: (
action1: SearchAction,
action2: SearchAction
) => void
) {
/*testFacetLogging('fExcluded', expectIdenticalActionType);
it('should log #logFacetSelect when an fExcluded parameter is added', () => {
expectIdenticalActionType(
logParametersChange({}, {fExcluded: {author: ['Cervantes']}}),
logFacetExclude({facetId: 'author', facetValue: 'Cervantes'})
);
});*/

it('should log #logFacetSelect when an fExcluded parameter is modified & a value added', () => {
expectIdenticalActionType(
logParametersChange(
{fExcluded: {author: ['Cervantes']}},
{fExcluded: {author: ['Cervantes', 'Orwell']}}
),
logFacetExclude({facetId: 'author', facetValue: 'Orwell'})
);
});
}

function testFacetLogging(
parameter: string,
expectIdenticalActionType: (
action1: SearchAction,
action2: SearchAction
) => void
) {
it(`should log #logFacetDeselect when an ${parameter} parameter with a single value is removed`, () => {
expectIdenticalActionType(
logParametersChange({[parameter]: {author: ['Cervantes']}}, {}),
Expand All @@ -95,16 +144,6 @@ function testFacetLogging(
);
});

it(`should log #logFacetSelect when an ${parameter} parameter is modified & a value added`, () => {
expectIdenticalActionType(
logParametersChange(
{[parameter]: {author: ['Cervantes']}},
{[parameter]: {author: ['Cervantes', 'Orwell']}}
),
logFacetSelect({facetId: 'author', facetValue: 'Orwell'})
);
});

it(`should log #logFacetDeselect when an ${parameter} parameter is modified & a value removed`, () => {
expectIdenticalActionType(
logParametersChange(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export function logParametersChange(
) {
return logFacetAnalyticsAction(
previousParameters.fExcluded,
newParameters.fExcluded
newParameters.fExcluded,
true
);
}

Expand Down Expand Up @@ -102,7 +103,8 @@ function parseRangeFacetParams(facetsParams: RangeFacetParameters) {

function logFacetAnalyticsAction(
previousFacets: FacetParameters = {},
newFacets: FacetParameters = {}
newFacets: FacetParameters = {},
excluded = false
): SearchAction {
const previousIds = Object.keys(previousFacets);
const newIds = Object.keys(newFacets);
Expand All @@ -118,15 +120,14 @@ function logFacetAnalyticsAction(
const addedIds = newIds.filter((id) => !previousIds.includes(id));
if (addedIds.length) {
const facetId = addedIds[0];
const facetValue = newFacets[facetId][0];
return facetValue === 'selected'
? logFacetSelect({
return excluded
? logFacetExclude({
facetId,
facetValue: facetValue,
facetValue: newFacets[facetId][0],
})
: logFacetExclude({
: logFacetSelect({
facetId,
facetValue: facetValue,
facetValue: newFacets[facetId][0],
});
}

Expand All @@ -147,12 +148,12 @@ function logFacetAnalyticsAction(
);

if (addedValues.length) {
return addedValues[0] === 'selected'
? logFacetSelect({
return excluded
? logFacetExclude({
facetId: facetIdWithDifferentValues,
facetValue: addedValues[0],
})
: logFacetExclude({
: logFacetSelect({
facetId: facetIdWithDifferentValues,
facetValue: addedValues[0],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ describe('buildSearchParameterSerializer', () => {
expect(result).toEqual('f-author=a,b&f-filetype=c,d');
});

it('serializes the #fExcluded parameter correctly', () => {
const fExcluded = {author: ['a', 'b'], filetype: ['c', 'd']};
const result = serialize({fExcluded});
expect(result).toEqual('fExcluded-author=a,b&fExcluded-filetype=c,d');
});

it('serializes special characters in the #f parameter correctly', () => {
someSpecialCharactersThatNeedsEncoding.forEach((specialChar) => {
const f = {author: ['a', specialChar]};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ function serializePair(pair: [string, unknown]) {
return '';
}

if (key === 'f' || key === 'cf' || key === 'sf' || key === 'af') {
if (
key === 'f' ||
key === 'fExcluded' ||
key === 'cf' ||
key === 'sf' ||
key === 'af'
) {
return isFacetObject(val) ? serializeFacets(key, val) : '';
}

Expand Down Expand Up @@ -303,8 +309,8 @@ function castUnknownObject(value: string) {

function keyHasObjectValue(
key: SearchParameterKey
): key is 'f' | 'cf' | 'nf' | 'df' | 'sf' | 'af' {
const keys = ['f', 'cf', 'nf', 'df', 'sf', 'af'];
): key is 'f' | 'fExcluded' | 'cf' | 'nf' | 'df' | 'sf' | 'af' {
const keys = ['f', 'fExcluded', 'cf', 'nf', 'df', 'sf', 'af'];
return keys.includes(key);
}

Expand Down

0 comments on commit 4f88e95

Please sign in to comment.