Skip to content
198 changes: 198 additions & 0 deletions spec/src/modules/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13683,6 +13683,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
const optionalParameters = {
section: 'Products',
variationId: '2',
threadId: 'thread-123',
};

it('Should respond with a valid response when term and required parameters are provided', (done) => {
Expand Down Expand Up @@ -13836,6 +13837,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('section').to.equal(optionalParameters.section);
expect(bodyParams).to.have.property('variation_id').to.equal(optionalParameters.variationId);
expect(bodyParams).to.have.property('thread_id').to.equal(optionalParameters.threadId);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
Expand Down Expand Up @@ -14013,6 +14015,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
const optionalParameters = {
section: 'Products',
variationId: '2',
threadId: 'thread-123',
};

it('Should respond with a valid response when term and required parameters are provided', (done) => {
Expand Down Expand Up @@ -14165,6 +14168,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('section').to.equal(optionalParameters.section);
expect(bodyParams).to.have.property('variation_id').to.equal(optionalParameters.variationId);
expect(bodyParams).to.have.property('thread_id').to.equal(optionalParameters.threadId);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
Expand Down Expand Up @@ -14334,6 +14338,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
const optionalParameters = {
section: 'Products',
variationId: '2',
threadId: 'thread-123',
};

it('Should respond with a valid response when term and required parameters are provided', (done) => {
Expand Down Expand Up @@ -14487,6 +14492,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('section').to.equal(optionalParameters.section);
expect(bodyParams).to.have.property('variation_id').to.equal(optionalParameters.variationId);
expect(bodyParams).to.have.property('thread_id').to.equal(optionalParameters.threadId);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
Expand Down Expand Up @@ -14656,6 +14662,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
const optionalParameters = {
section: 'Products',
variationId: '2',
threadId: 'thread-123',
};

it('Should respond with a valid response when term and required parameters are provided', (done) => {
Expand Down Expand Up @@ -14807,6 +14814,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('section').to.equal(optionalParameters.section);
expect(bodyParams).to.have.property('variation_id').to.equal(optionalParameters.variationId);
expect(bodyParams).to.have.property('thread_id').to.equal(optionalParameters.threadId);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
Expand Down Expand Up @@ -14976,6 +14984,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
const optionalParameters = {
section: 'Products',
variationId: '2',
threadId: 'thread-123',
};

it('Should respond with a valid response when term and required parameters are provided', (done) => {
Expand Down Expand Up @@ -15128,6 +15137,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('section').to.equal(optionalParameters.section);
expect(bodyParams).to.have.property('variation_id').to.equal(optionalParameters.variationId);
expect(bodyParams).to.have.property('thread_id').to.equal(optionalParameters.threadId);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
Expand Down Expand Up @@ -15297,6 +15307,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
const optionalParameters = {
section: 'Products',
variationId: '2',
threadId: 'thread-123',
};

it('Should respond with a valid response when term and required parameters are provided', (done) => {
Expand Down Expand Up @@ -15449,6 +15460,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('section').to.equal(optionalParameters.section);
expect(bodyParams).to.have.property('variation_id').to.equal(optionalParameters.variationId);
expect(bodyParams).to.have.property('thread_id').to.equal(optionalParameters.threadId);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
Expand Down Expand Up @@ -15619,6 +15631,9 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
section: 'Products',
variationId: '2',
qnaResultId: '0daf0015-fc29-4727-9140-8d5313a1902c',
threadId: 'thread-123',
items: [{ itemId: 'rec1', itemName: 'Rec Product 1' }, { itemId: 'rec2', itemName: 'Rec Product 2' }],
followUpQuestions: [{ value: 'What about size?' }, { value: 'Is it machine washable?' }],
};

it('Should respond with a valid response when term and required parameters are provided', (done) => {
Expand Down Expand Up @@ -15773,6 +15788,12 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
expect(requestParams).to.have.property('section').to.equal(optionalParameters.section);
expect(bodyParams).to.have.property('variation_id').to.equal(optionalParameters.variationId);
expect(bodyParams).to.have.property('qna_result_id').to.equal(optionalParameters.qnaResultId);
expect(bodyParams).to.have.property('thread_id').to.equal(optionalParameters.threadId);
expect(bodyParams).to.have.property('items').to.be.an('array').with.lengthOf(2);
expect(bodyParams.items[0]).to.have.property('item_id').to.equal('rec1');
expect(bodyParams.items[0]).to.have.property('item_name').to.equal('Rec Product 1');
expect(bodyParams).to.have.property('follow_up_questions').to.be.an('array').with.lengthOf(2);
expect(bodyParams.follow_up_questions[0]).to.have.property('value').to.equal('What about size?');

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
Expand Down Expand Up @@ -15943,6 +15964,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
section: 'Products',
variationId: '2',
qnaResultId: '0daf0015-fc29-4727-9140-8d5313a1902c',
threadId: 'thread-123',
};

it('Should respond with a valid response when term and required parameters are provided', (done) => {
Expand Down Expand Up @@ -16095,6 +16117,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
expect(requestParams).to.have.property('section').to.equal(optionalParameters.section);
expect(bodyParams).to.have.property('qna_result_id').to.equal(optionalParameters.qnaResultId);
expect(bodyParams).to.have.property('variation_id').to.equal(optionalParameters.variationId);
expect(bodyParams).to.have.property('thread_id').to.equal(optionalParameters.threadId);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
Expand Down Expand Up @@ -16259,6 +16282,181 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
});
});

describe('trackProductInsightsAgentResultClick', () => {
const requiredParameters = { itemId: '1', itemName: 'item1', question: 'Why choose this?', seedItemId: 'seed-1' };
const optionalParameters = {
section: 'Products',
variationId: '2',
position: 1,
seedItemName: 'Seed Product',
seedVariationId: 'seed-var-1',
threadId: 'thread-123',
qnaResultId: '0daf0015-fc29-4727-9140-8d5313a1902c',
};

it('Should respond with a valid response when required parameters are provided', (done) => {
const { tracker } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
...requestQueueOptions,
});

tracker.on('success', (responseParams) => {
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);

// Request
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('key');
expect(requestParams).to.have.property('i');
expect(requestParams).to.have.property('s');
expect(requestParams).to.have.property('c').to.equal(clientVersion);
expect(requestParams).to.have.property('_dt');
expect(requestParams).to.have.property('item_id').to.equal(requiredParameters.itemId);
expect(requestParams).to.have.property('item_name').to.equal(requiredParameters.itemName);
expect(requestParams).to.have.property('question').to.equal(requiredParameters.question);
expect(requestParams).to.have.property('seed_item_id').to.equal(requiredParameters.seedItemId);
validateOriginReferrer(requestParams);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
expect(responseParams).to.have.property('message').to.equal('ok');

done();
});

expect(tracker.trackProductInsightsAgentResultClick(requiredParameters)).to.equal(true);
});

it('Should respond with a valid response when required and optional parameters are provided', (done) => {
const { tracker } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
...requestQueueOptions,
});

tracker.on('success', (responseParams) => {
const bodyParams = helpers.extractBodyParamsFromFetch(fetchSpy);
const requestParams = helpers.extractUrlParamsFromFetch(fetchSpy);
// Request
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('section').to.equal(optionalParameters.section);
expect(bodyParams).to.have.property('variation_id').to.equal(optionalParameters.variationId);
expect(bodyParams).to.have.property('position').to.equal(optionalParameters.position);
expect(bodyParams).to.have.property('seed_item_name').to.equal(optionalParameters.seedItemName);
expect(bodyParams).to.have.property('seed_variation_id').to.equal(optionalParameters.seedVariationId);
expect(bodyParams).to.have.property('thread_id').to.equal(optionalParameters.threadId);
expect(bodyParams).to.have.property('qna_result_id').to.equal(optionalParameters.qnaResultId);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
expect(responseParams).to.have.property('message').to.equal('ok');

done();
});

expect(tracker.trackProductInsightsAgentResultClick(
{ ...requiredParameters, ...optionalParameters },
)).to.equal(true);
});

it('Should throw an error when no parameters are provided', () => {
const { tracker } = new ConstructorIO({ apiKey: testApiKey });

expect(tracker.trackProductInsightsAgentResultClick()).to.be.an('error');
});

it('Should throw an error when invalid parameters are provided', () => {
const { tracker } = new ConstructorIO({ apiKey: testApiKey });

expect(tracker.trackProductInsightsAgentResultClick('invalid')).to.be.an('error');
});

it('Should respond with a valid response when required parameters and segments are provided', (done) => {
const segments = ['foo', 'bar'];
const { tracker } = new ConstructorIO({
apiKey: testApiKey,
segments,
fetch: fetchSpy,
...requestQueueOptions,
});

tracker.on('success', (responseParams) => {
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);

// Request
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('us').to.deep.equal(segments);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
expect(responseParams).to.have.property('message').to.equal('ok');

done();
});

expect(tracker.trackProductInsightsAgentResultClick(requiredParameters)).to.equal(true);
});

it('Should respond with a valid response when required parameters and userId are provided', (done) => {
const userId = 'user-id';
const { tracker } = new ConstructorIO({
apiKey: testApiKey,
userId,
fetch: fetchSpy,
...requestQueueOptions,
});

tracker.on('success', (responseParams) => {
const requestParams = helpers.extractBodyParamsFromFetch(fetchSpy);

// Request
expect(fetchSpy).to.have.been.called;
expect(requestParams).to.have.property('ui').to.equal(userId);

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
expect(responseParams).to.have.property('message').to.equal('ok');

done();
});

expect(tracker.trackProductInsightsAgentResultClick(requiredParameters)).to.equal(true);
});

if (!skipNetworkTimeoutTests) {
it('Should be rejected when network request timeout is provided and reached', (done) => {
const { tracker } = new ConstructorIO({
apiKey: testApiKey,
...requestQueueOptions,
});

tracker.on('error', ({ message }) => {
expect(message).to.equal(timeoutRejectionMessage);
done();
});

expect(tracker.trackProductInsightsAgentResultClick(requiredParameters, { timeout: 10 })).to.equal(true);
});

it('Should be rejected when global network request timeout is provided and reached', (done) => {
const { tracker } = new ConstructorIO({
apiKey: testApiKey,
networkParameters: {
timeout: 20,
},
...requestQueueOptions,
});

tracker.on('error', ({ message }) => {
expect(message).to.equal(timeoutRejectionMessage);
done();
});

expect(tracker.trackProductInsightsAgentResultClick(requiredParameters)).to.equal(true);
});
}
});

describe('trackResultsImpressionView', () => {
const requiredParameters = {
items: [
Expand Down
Loading
Loading