Skip to content

Commit

Permalink
Merge pull request #1185 from searchspring/fix-missing-instantiator-c…
Browse files Browse the repository at this point in the history
…ontext

Instantiator Fix
  • Loading branch information
korgon authored Oct 25, 2024
2 parents cc0a0e8 + 0076a0c commit 5404312
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,32 @@ describe('RecommendationInstantiator', () => {
expect(clientSpy).toHaveBeenCalledTimes(1);
});

it('makes the context found on the target available', async () => {
it('supports legacy script type with config context', async () => {
const profile = 'legacy';
document.body.innerHTML = `<script type="searchspring/personalized-recommendations" profile="${profile}"></script>`;

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

const newConfig = {
...baseConfig,
context: {
testing: 'things',
},
};

const recommendationInstantiator = new RecommendationInstantiator(newConfig, { client });
await wait();
expect(Object.keys(recommendationInstantiator.controller).length).toBe(1);
expect(recommendationInstantiator.controller['recommend_legacy_0']).toBeDefined();
expect(recommendationInstantiator.controller['recommend_legacy_0'].context).toStrictEqual({
profile,
...newConfig.context,
});
expect(clientSpy).toHaveBeenCalledTimes(1);
});

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' };
product = 'sku1';
Expand Down Expand Up @@ -319,7 +344,14 @@ describe('RecommendationInstantiator', () => {
const client = new MockClient(baseConfig.client!.globals, {});
const clientSpy = jest.spyOn(client, 'recommend');

const recommendationInstantiator = new RecommendationInstantiator(baseConfig, { client });
const newConfig = {
...baseConfig,
context: {
testing: 'things',
},
};

const recommendationInstantiator = new RecommendationInstantiator(newConfig, { client });
await wait();
expect(Object.keys(recommendationInstantiator.controller).length).toBe(1);
Object.keys(recommendationInstantiator.controller).forEach((controllerId) => {
Expand Down Expand Up @@ -350,6 +382,7 @@ describe('RecommendationInstantiator', () => {
limit: 5,
siteId: 'abc123',
},
...newConfig.context,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export class RecommendationInstantiator {
}),
};

readyTheController(this, elem, elemContext, profileCount, originalElem, profileRequestGlobals);
readyTheController(this, elem, deepmerge(this.context, elemContext), profileCount, originalElem, profileRequestGlobals);
}
}
);
Expand Down

0 comments on commit 5404312

Please sign in to comment.