From 93788d422510adc15840fac987631efe67f76d2e Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 31 Oct 2024 16:37:53 -0600 Subject: [PATCH] fix(preact/recommendationinstantiator): adding back support for legacy usage of seed and product supporting using these with arrays --- .../RecommendationInstantiator.test.tsx | 69 +++++++++++++++++++ .../RecommendationInstantiator.tsx | 3 +- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/packages/snap-preact/src/Instantiators/RecommendationInstantiator.test.tsx b/packages/snap-preact/src/Instantiators/RecommendationInstantiator.test.tsx index 696bf1441..13b3dc9af 100644 --- a/packages/snap-preact/src/Instantiators/RecommendationInstantiator.test.tsx +++ b/packages/snap-preact/src/Instantiators/RecommendationInstantiator.test.tsx @@ -292,6 +292,75 @@ describe('RecommendationInstantiator', () => { expect(clientSpy).toHaveBeenCalledTimes(1); }); + it('supports legacy script type with array values in seed', async () => { + document.body.innerHTML = ``; + + 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 = ``; + + 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 = ``; + + 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 = ``; diff --git a/packages/snap-preact/src/Instantiators/RecommendationInstantiator.tsx b/packages/snap-preact/src/Instantiators/RecommendationInstantiator.tsx index a9eb6c9a8..fbd165e40 100644 --- a/packages/snap-preact/src/Instantiators/RecommendationInstantiator.tsx +++ b/packages/snap-preact/src/Instantiators/RecommendationInstantiator.tsx @@ -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 = { 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,