Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 185 additions & 3 deletions spec/src/modules/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13290,6 +13290,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 @@ -13442,6 +13443,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 All @@ -13451,7 +13453,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
});

expect(tracker.trackProductInsightsAgentView(
Object.assign(requiredParameters, optionalParameters),
{ ...requiredParameters, ...optionalParameters },
)).to.equal(true);
});

Expand Down Expand Up @@ -14896,6 +14898,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: [{ question: 'What about size?' }, { question: 'Is it machine washable?' }],
};

it('Should respond with a valid response when term and required parameters are provided', (done) => {
Expand Down Expand Up @@ -15050,6 +15055,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('question').to.equal('What about size?');

// Response
expect(responseParams).to.have.property('method').to.equal('POST');
Expand All @@ -15059,7 +15070,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
});

expect(tracker.trackProductInsightsAgentAnswerView(
Object.assign(requiredParameters, optionalParameters),
{ ...requiredParameters, ...optionalParameters },
)).to.equal(true);
});

Expand Down Expand Up @@ -15220,6 +15231,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 @@ -15372,6 +15384,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 All @@ -15381,7 +15394,7 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
});

expect(tracker.trackProductInsightsAgentAnswerFeedback(
Object.assign(requiredParameters, optionalParameters),
{ ...requiredParameters, ...optionalParameters },
)).to.equal(true);
});

Expand Down Expand Up @@ -15536,6 +15549,175 @@ describe(`ConstructorIO - Tracker${bundledDescriptionSuffix}`, () => {
});
});

describe('trackProductInsightsAgentResultClick', () => {
const requiredParameters = { itemId: '1', itemName: 'item1' };
const optionalParameters = {
section: 'Products',
variationId: '2',
position: 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);
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('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()).to.be.an('error');
});
Comment on lines +16352 to +16356

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('trackMediaImpressionView', () => {
let requiredParameters;

Expand Down
Loading