Skip to content

Commit

Permalink
fix(preact/recommendationinstantiator): fixing regression of batching…
Browse files Browse the repository at this point in the history
… in legacy script blocks
  • Loading branch information
korgon committed Nov 13, 2024
1 parent 13e39b7 commit 85e339f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,75 @@ describe('RecommendationInstantiator', () => {
expect(clientSpy).toHaveBeenCalledTimes(1);
});

it('supports legacy scripts with batching', async () => {
document.body.innerHTML = `
<script type="searchspring/personalized-recommendations" profile="legacy">
options = {
batched: true
};
products = ['prod1234', 'prod4567'];
</script>
<script type="searchspring/personalized-recommendations" profile="legacy">
options = {
batched: false
};
products = ['prod0'];
</script>
<script type="searchspring/personalized-recommendations" profile="legacy">
options = {
batched: true
};
products = ['prod789'];
</script>`;

const client = new MockClient(baseConfig.client!.globals, {});
const clientSpy = jest.spyOn(client, 'recommend');

const recommendationInstantiator = new RecommendationInstantiator(baseConfig, { client });
await wait();
expect(Object.keys(recommendationInstantiator.controller).length).toBe(3);
expect(recommendationInstantiator.controller['recommend_legacy_0']).toBeDefined();
expect(clientSpy).toHaveBeenCalledTimes(3);
expect(clientSpy).toHaveBeenNthCalledWith(1, {
batchId: undefined,
batched: true,
branch: 'production',
products: ['prod1234', 'prod4567'],
profile: {
batched: true,
},
siteId: '8uyt2m',
tag: 'legacy',
});

expect(clientSpy).toHaveBeenNthCalledWith(2, {
batchId: undefined,
batched: false,
branch: 'production',
products: ['prod0'],
profile: {
batched: false,
},
siteId: '8uyt2m',
tag: 'legacy',
});

expect(clientSpy).toHaveBeenNthCalledWith(3, {
batchId: undefined,
batched: true,
branch: 'production',
products: ['prod789'],
profile: {
batched: true,
},
siteId: '8uyt2m',
tag: 'legacy',
});
});

it('makes the context found on the target and in the config available', async () => {
document.body.innerHTML = `<script type="searchspring/recommend" profile="${DEFAULT_PROFILE}">
shopper = { id: 'snapdev' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ async function readyTheController(
controllerGlobals: Partial<RecommendRequestModel>
) {
const { profile, batchId, cart, tag } = controllerGlobals;
const batched = (profile?.batched || controllerGlobals.batched) ?? true;
const batched = profile?.batched ?? controllerGlobals.batched ?? true;

if (!tag) {
// FEEDBACK: change message depending on script integration type (profile vs. legacy)
Expand Down

0 comments on commit 85e339f

Please sign in to comment.