Skip to content

Commit

Permalink
Merge pull request #1187 from searchspring/instantiator-legacy-support
Browse files Browse the repository at this point in the history
Hotfix Recommendation Support
  • Loading branch information
chrisFrazier77 authored Oct 31, 2024
2 parents 4fec7f7 + 93788d4 commit cf3d20d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,75 @@ describe('RecommendationInstantiator', () => {
expect(clientSpy).toHaveBeenCalledTimes(1);
});

it('supports legacy script type with array values in seed', async () => {
document.body.innerHTML = `<script type="searchspring/personalized-recommendations" profile="legacy">
seed = ['prod1234'];
</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(1);
expect(recommendationInstantiator.controller['recommend_legacy_0']).toBeDefined();
expect(clientSpy).toHaveBeenCalledTimes(1);
expect(clientSpy).toHaveBeenCalledWith({
batchId: undefined,
batched: true,
branch: 'production',
products: ['prod1234'],
siteId: '8uyt2m',
tag: 'legacy',
});
});

it('supports legacy script type with array values in product', async () => {
document.body.innerHTML = `<script type="searchspring/personalized-recommendations" profile="legacy">
product = ['prod1234'];
</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(1);
expect(recommendationInstantiator.controller['recommend_legacy_0']).toBeDefined();
expect(clientSpy).toHaveBeenCalledTimes(1);
expect(clientSpy).toHaveBeenCalledWith({
batchId: undefined,
batched: true,
branch: 'production',
products: ['prod1234'],
siteId: '8uyt2m',
tag: 'legacy',
});
});

it('supports legacy script type with array values in products', async () => {
document.body.innerHTML = `<script type="searchspring/personalized-recommendations" profile="legacy">
products = ['prod1234', 'prod4567'];
</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(1);
expect(recommendationInstantiator.controller['recommend_legacy_0']).toBeDefined();
expect(clientSpy).toHaveBeenCalledTimes(1);
expect(clientSpy).toHaveBeenCalledWith({
batchId: undefined,
batched: true,
branch: 'production',
products: ['prod1234', 'prod4567'],
siteId: '8uyt2m',
tag: 'legacy',
});
});

it('supports legacy script type with config context', async () => {
const profile = 'legacy';
document.body.innerHTML = `<script type="searchspring/personalized-recommendations" profile="${profile}"></script>`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ export class RecommendationInstantiator {
} else {
// using the "legacy" integration structure
const { profile, products, product, seed, filters, blockedItems, options, shopper, shopperId } = elemContext;
const combinedProducts = [].concat(products || product || seed || []);

const profileRequestGlobals: Partial<RecommendRequestModel> = {
tag: profile,
...defined({
products: products || (product && [product]) || (seed && [seed]),
products: combinedProducts.length ? combinedProducts : undefined,
cart: elemContext.cart && getArrayFunc(elemContext.cart),
shopper: shopper?.id || shopperId,
filters,
Expand Down

0 comments on commit cf3d20d

Please sign in to comment.