Skip to content

Commit

Permalink
Merge pull request #1186 from searchspring/fix-profile-specific-integ…
Browse files Browse the repository at this point in the history
…ration-script

Profile Specific Integration Bug
  • Loading branch information
korgon authored Oct 28, 2024
2 parents 2466108 + 0f479df commit 82e2e8c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { RecommendationInstantiator, RecommendationInstantiatorConfig } from './RecommendationInstantiator';
import type { PluginGrouping } from '@searchspring/snap-controller';
import { cookies } from '@searchspring/snap-toolbox';

import { Logger } from '@searchspring/snap-logger';
import { MockClient } from '@searchspring/snap-shared';
import { Next } from '@searchspring/snap-event-manager';

const DEFAULT_PROFILE = 'trending';
const CART_COOKIE = 'ssCartProducts';

const Component = (props: any) => {
const controller = props.controller;
Expand Down Expand Up @@ -35,6 +37,7 @@ const baseConfig: RecommendationInstantiatorConfig = {
describe('RecommendationInstantiator', () => {
beforeEach(() => {
delete window.searchspring;
cookies.unset(CART_COOKIE);
});

it('throws if configuration is not provided', () => {
Expand Down Expand Up @@ -663,6 +666,51 @@ describe('RecommendationInstantiator', () => {
});
});

it('can use new style script (without globals)', async () => {
const profileContextArray = [
{
tag: '404',
selector: '.ss__recs__404',
},
];

//good testing to build off of
document.body.innerHTML = `
<div class="ss__recs__404"></div>
<script type="searchspring/recommendations">
profiles = [
{
tag: '404',
selector: '.ss__recs__404',
},
];
</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);
Object.keys(recommendationInstantiator.controller).forEach((controllerId, index) => {
const controller = recommendationInstantiator.controller[controllerId];
expect(controller.context).toStrictEqual({
profile: profileContextArray[index],
});
});
const batchId = recommendationInstantiator.controller[Object.keys(recommendationInstantiator.controller)[0]].store.config.batchId;

expect(clientSpy).toHaveBeenCalledTimes(1);
expect(clientSpy).toHaveBeenNthCalledWith(1, {
tag: '404',
batchId,
siteId: baseConfig.client?.globals.siteId,
branch: 'production',
batched: true,
});
});

it('will utilize attachments (plugins / middleware) added via methods upon creation of controller', async () => {
document.body.innerHTML = `<script type="searchspring/recommend" profile="${DEFAULT_PROFILE}"></script>`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,16 @@ export class RecommendationInstantiator {

// type the new profile specific integration context variables
const scriptContextProfiles = elemContext.profiles as ProfileSpecificProfile[];
const scriptContextGlobals = elemContext.globals as ProfileSpecificGlobals;
const scriptContextGlobals = elemContext.globals as ProfileSpecificGlobals | undefined;

// grab from globals
const requestGlobals: Partial<RecommendRequestModel> = {
...defined({
blockedItems: scriptContextGlobals.blockedItems,
filters: scriptContextGlobals.filters,
cart: scriptContextGlobals.cart && getArrayFunc(scriptContextGlobals.cart),
products: scriptContextGlobals.products,
shopper: scriptContextGlobals.shopper?.id,
blockedItems: scriptContextGlobals?.blockedItems,
filters: scriptContextGlobals?.filters,
cart: scriptContextGlobals?.cart && getArrayFunc(scriptContextGlobals.cart),
products: scriptContextGlobals?.products,
shopper: scriptContextGlobals?.shopper?.id,
batchId: Math.random(),
}),
};
Expand Down Expand Up @@ -209,7 +209,7 @@ export class RecommendationInstantiator {
profile: target.profile?.options,
tag: target.profile.tag! || target.profile.profile!, // have to support both tag and profile due to having profile at release, but will favor tag
};
const profileContext: ContextVariables = deepmerge(this.context, { globals: scriptContextGlobals, profile: target.profile });
const profileContext: ContextVariables = deepmerge(this.context, defined({ globals: scriptContextGlobals, profile: target.profile }));
if (elemContext.custom) {
profileContext.custom = elemContext.custom;
}
Expand Down

0 comments on commit 82e2e8c

Please sign in to comment.