-
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.
feat(commerce): create PLP v2 interactive result controller (#3129)
- Loading branch information
1 parent
1d63e43
commit 4d3c39d
Showing
4 changed files
with
149 additions
and
2 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
82 changes: 82 additions & 0 deletions
82
.../commerce/product-listing/result-list/headless-product-listing-interactive-result.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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import {ProductRecommendation} from '../../../../api/search/search/product-recommendation'; | ||
import {configuration} from '../../../../app/common-reducers'; | ||
import {logProductRecommendationOpen} from '../../../../features/product-listing/product-listing-analytics'; | ||
import {pushRecentResult} from '../../../../features/recent-results/recent-results-actions'; | ||
import { | ||
buildMockCommerceEngine, | ||
MockCommerceEngine, | ||
} from '../../../../test/mock-engine'; | ||
import {buildMockProductRecommendation} from '../../../../test/mock-product-recommendation'; | ||
import { | ||
buildInteractiveResult, | ||
InteractiveResult, | ||
} from './headless-product-listing-interactive-result'; | ||
|
||
describe('InteractiveResult', () => { | ||
let engine: MockCommerceEngine; | ||
let mockProductRec: ProductRecommendation; | ||
let interactiveResult: InteractiveResult; | ||
let logDocumentOpenPendingActionType: string; | ||
|
||
const productRecStringParams = { | ||
permanentid: 'permanentid', | ||
documentUri: 'documentUri', | ||
clickUri: 'clickUri', | ||
}; | ||
|
||
function initializeInteractiveResult(delay?: number) { | ||
const productRec = (mockProductRec = buildMockProductRecommendation( | ||
productRecStringParams | ||
)); | ||
logDocumentOpenPendingActionType = | ||
logProductRecommendationOpen(mockProductRec).pending.type; | ||
interactiveResult = buildInteractiveResult(engine, { | ||
options: {result: productRec, selectionDelay: delay}, | ||
}); | ||
} | ||
|
||
function findLogDocumentAction() { | ||
return ( | ||
engine.actions.find( | ||
(action) => action.type === logDocumentOpenPendingActionType | ||
) ?? null | ||
); | ||
} | ||
|
||
function expectLogDocumentActionPending() { | ||
const action = findLogDocumentAction(); | ||
expect(action).toEqual( | ||
logProductRecommendationOpen(mockProductRec).pending( | ||
action!.meta.requestId | ||
) | ||
); | ||
} | ||
|
||
beforeEach(() => { | ||
engine = buildMockCommerceEngine(); | ||
initializeInteractiveResult(); | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('adds the correct reducers to engine', () => { | ||
expect(engine.addReducers).toHaveBeenCalledWith({configuration}); | ||
}); | ||
|
||
it('when calling select() should add the result to recent results list', () => { | ||
interactiveResult.select(); | ||
jest.runAllTimers(); | ||
|
||
expect( | ||
engine.actions.find((a) => a.type === pushRecentResult.type) | ||
).toBeDefined(); | ||
}); | ||
|
||
it('when calling select(), logs documentOpen', () => { | ||
interactiveResult.select(); | ||
expectLogDocumentActionPending(); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
...llers/commerce/product-listing/result-list/headless-product-listing-interactive-result.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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {ProductRecommendation} from '../../../../api/search/search/product-recommendation'; | ||
import {CommerceEngine} from '../../../../app/commerce-engine/commerce-engine'; | ||
import {logProductRecommendationOpen} from '../../../../features/product-listing/product-listing-analytics'; | ||
import {pushRecentResult} from '../../../../features/product-listing/product-listing-recent-results'; | ||
import { | ||
buildInteractiveResultCore, | ||
InteractiveResultCore, | ||
InteractiveResultCoreOptions, | ||
InteractiveResultCoreProps, | ||
} from '../../../core/interactive-result/headless-core-interactive-result'; | ||
|
||
export interface InteractiveResultOptions extends InteractiveResultCoreOptions { | ||
/** | ||
* The product. | ||
*/ | ||
result: ProductRecommendation; | ||
} | ||
|
||
export interface InteractiveResultProps extends InteractiveResultCoreProps { | ||
/** | ||
* The options for the `InteractiveResult` controller. | ||
* */ | ||
options: InteractiveResultOptions; | ||
} | ||
|
||
/** | ||
* The `InteractiveProduct` controller provides an interface for handling long presses, multiple clicks, etc. to ensure that Coveo usage analytics events are logged properly when a user selects a product. | ||
*/ | ||
export interface InteractiveResult extends InteractiveResultCore {} | ||
|
||
/** | ||
* Creates an `InteractiveResult` controller instance. | ||
* | ||
* @param engine - The headless commerce engine. | ||
* @param props - The configurable `InteractiveResult` properties. | ||
* @returns An `InteractiveResult` controller instance. | ||
*/ | ||
export function buildInteractiveResult( | ||
engine: CommerceEngine, | ||
props: InteractiveResultProps | ||
): InteractiveResult { | ||
let wasOpened = false; | ||
|
||
const logAnalyticsIfNeverOpened = () => { | ||
if (wasOpened) { | ||
return; | ||
} | ||
wasOpened = true; | ||
engine.dispatch(logProductRecommendationOpen(props.options.result)); | ||
}; | ||
|
||
const action = () => { | ||
logAnalyticsIfNeverOpened(); | ||
engine.dispatch(pushRecentResult(props.options.result)); | ||
}; | ||
|
||
return buildInteractiveResultCore(engine, props, action); | ||
} |