Skip to content

Commit 42f69a7

Browse files
committed
Accept seedItemIds as a number, add tests
1 parent b3d9811 commit 42f69a7

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

spec/src/modules/tracker.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4690,6 +4690,35 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
46904690
expect(tracker.trackRecommendationView(requiredParamsWithSeedItemIds)).to.equal(true);
46914691
});
46924692

4693+
it('Should respond with a valid response and convert seedItemIds to an array if it\'s is a number', (done) => {
4694+
const seedItemIds = 123;
4695+
const { tracker } = new ConstructorIO({
4696+
apiKey: testApiKey,
4697+
fetch: fetchSpy,
4698+
...requestQueueOptions,
4699+
});
4700+
const requiredParamsWithSeedItemIds = {
4701+
seedItemIds,
4702+
...requiredParameters,
4703+
};
4704+
4705+
tracker.on('success', (responseParams) => {
4706+
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);
4707+
4708+
// Request
4709+
expect(fetchSpy).to.have.been.called;
4710+
expect(requestParams).to.have.property('seed_item_ids').to.deep.equal(['123']);
4711+
4712+
// Response
4713+
expect(responseParams).to.have.property('method').to.equal('POST');
4714+
expect(responseParams).to.have.property('message');
4715+
4716+
done();
4717+
});
4718+
4719+
expect(tracker.trackRecommendationView(requiredParamsWithSeedItemIds)).to.equal(true);
4720+
});
4721+
46934722
it('Should respond with a valid response and convert seedItemIds to an array if it\'s a string', (done) => {
46944723
const seedItemIds = '123';
46954724
const { tracker } = new ConstructorIO({
@@ -4719,8 +4748,8 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
47194748
expect(tracker.trackRecommendationView(requiredParamsWithSeedItemIds)).to.equal(true);
47204749
});
47214750

4722-
it('Should respond with a valid response and omit seed_item_ids if seedItemIds is a number', (done) => {
4723-
const seedItemIds = 123;
4751+
it('Should respond with a valid response and omit seed_item_ids if seedItemIds is null', (done) => {
4752+
const seedItemIds = null;
47244753
const { tracker } = new ConstructorIO({
47254754
apiKey: testApiKey,
47264755
fetch: fetchSpy,

src/modules/tracker.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ class Tracker {
12221222
* @param {string} [parameters.resultId] - Recommendation result identifier (returned in response from Constructor)
12231223
* @param {string} [parameters.section="Products"] - Results section
12241224
* @param {object} [parameters.analyticsTags] - Pass additional analytics data
1225-
* @param {string[]|string} [parameters.seedItemIds] - Item ID(s) to be used as seed
1225+
* @param {string[]|string|number} [parameters.seedItemIds] - Item ID(s) to be used as seed
12261226
* @param {object} [networkParameters] - Parameters relevant to the network request
12271227
* @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
12281228
* @returns {(true|Error)}
@@ -1301,7 +1301,9 @@ class Tracker {
13011301
bodyParams.analytics_tags = analyticsTags;
13021302
}
13031303

1304-
if (seedItemIds?.length && typeof seedItemIds === 'string') {
1304+
if (typeof seedItemIds === 'number') {
1305+
bodyParams.seed_item_ids = [String(seedItemIds)];
1306+
} else if (seedItemIds?.length && typeof seedItemIds === 'string') {
13051307
bodyParams.seed_item_ids = [seedItemIds];
13061308
} else if (seedItemIds?.length && Array.isArray(seedItemIds)) {
13071309
bodyParams.seed_item_ids = seedItemIds;

0 commit comments

Comments
 (0)