-
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(productlistings): create plpv2 interactive controller
[CAPI-85]
- Loading branch information
1 parent
68c67b0
commit a24804a
Showing
3 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...src/features/product-listing/v2/result-list/product-listing-v2-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 { | ||
buildMockCommerceEngine, | ||
MockCommerceEngine, | ||
} from '../../../../test/mock-engine'; | ||
import {buildMockProductRecommendation} from '../../../../test/mock-product-recommendation'; | ||
import {logProductRecommendationOpen} from '../../product-listing-analytics'; | ||
import {pushRecentResult} from '../../product-listing-recent-results'; | ||
import { | ||
buildInteractiveResult, | ||
InteractiveResult, | ||
} from './product-listing-v2-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
...less/src/features/product-listing/v2/result-list/product-listing-v2-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 { | ||
InteractiveResultCore, | ||
InteractiveResultCoreOptions, | ||
InteractiveResultCoreProps, | ||
} from '../../../../controllers'; | ||
import {buildInteractiveResultCore} from '../../../../controllers/core/interactive-result/headless-core-interactive-result'; | ||
import {logProductRecommendationOpen} from '../../product-listing-analytics'; | ||
import {pushRecentResult} from '../../product-listing-recent-results'; | ||
|
||
export interface InteractiveResultOptions extends InteractiveResultCoreOptions { | ||
/** | ||
* The query result. | ||
*/ | ||
result: ProductRecommendation; | ||
} | ||
|
||
export interface InteractiveResultProps extends InteractiveResultCoreProps { | ||
/** | ||
* The options for the `InteractiveResult` controller. | ||
* */ | ||
options: InteractiveResultOptions; | ||
} | ||
|
||
/** | ||
* The `InteractiveResult` controller provides an interface for triggering desirable side effects, such as logging UA events to the Coveo Platform, when a user selects a query result. | ||
*/ | ||
export interface InteractiveResult extends InteractiveResultCore {} | ||
|
||
/** | ||
* Creates an `InteractiveResult` controller instance. | ||
* | ||
* @param engine - The headless 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); | ||
} |
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