Skip to content

Commit

Permalink
fix: adding test cases for identify response builder
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Feb 12, 2024
1 parent 0c885d0 commit ed01e14
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 81 deletions.
2 changes: 0 additions & 2 deletions src/v0/destinations/bluecore/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO : experiment if a custom event without distinct_id as an identifier reaches bluecore or not

const { isDefinedAndNotNull, ConfigurationError, TransformationError,
InstrumentationError, } = require('@rudderstack/integrations-lib');
const { EventType } = require('../../../constants');
Expand Down
221 changes: 142 additions & 79 deletions src/v0/destinations/bluecore/transform.test.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,146 @@
const { trackResponseBuilder } = require('./transform')
const { trackResponseBuilder, identifyResponseBuilder } = require('./transform');
const {
InstrumentationError
} = require('@rudderstack/integrations-lib');

const commonIdentifyMappingCategory = {
name: 'bluecoreIdentifyConfig',
type: 'identify',
}

const commonTrackMappingCategory = {
name: 'bluecoreTrackConfig',
type: 'track',
};
describe('trackResponseBuilder', () => {

// Given a valid message, category, destination config, and event name, the function should construct a payload with the correct event name and token
it('should construct a payload with the correct event name and token', () => {
const message = { type: 'track', event: 'purchase', properties: { total: 20, order_id: 123, products: [{ product_id: 12, name: "new prod" }] } };
const category = {
name: 'bluecoreTrackConfig',
type: 'track',
};
const destination = { Config: { bluecoreNamespace: 'namespace' } };
const eventName = 'purchase';

const expectedPayload = {
event: 'purchase',
token: 'namespace',
properties: {
"order_id": 123,
"products": [
{
"id": 12,
"name": "new prod",
},
],
"total": 20,
},
};

const result = trackResponseBuilder(message, category, destination, eventName);

expect(result).toEqual(expectedPayload);
});

// When the event name is 'optin', 'unsubscribe', or 'search', the function should not add a product array to the payload
it('need not add a product array to the payload when the event name is "optin"', () => {
const message = { type: 'track', event: 'optin', properties: { total: 20, order_id: 123, } };
const category = {
name: 'bluecoreTrackConfig',
type: 'track',
};
const destination = { Config: { bluecoreNamespace: 'namespace' } };
const eventName = 'optin';

const expectedPayload = {
event: 'optin',
token: 'namespace',
properties: {
order_id: 123,
total: 20,
},
};

const result = trackResponseBuilder(message, category, destination, eventName);

expect(result).toEqual(expectedPayload);
});

// When the event name is not 'optin', 'unsubscribe', or 'search', the function should add a product array to the payload
it('should add a product array to the payload when the event name is not "optin", "unsubscribe", or "search"', () => {
const message = { type: 'track', event: 'purchase', properties: {order_id:123, total: 20 , products: [{ id: '1', name: 'product' }]} };
const category = {
name: 'bluecoreTrackConfig',
type: 'track',
};
const destination = { Config: { bluecoreNamespace: 'namespace' } };
const eventName = 'purchase';

const expectedPayload = {
event: 'purchase',
token: 'namespace',
properties: {
order_id: 123,
total: 20,
products: [{ id: '1', name: 'product' }]
}
};

const result = trackResponseBuilder(message, category, destination, eventName);

expect(result).toEqual(expectedPayload);
});
// Given a valid message, category, destination config, and event name, the function should construct a payload with the correct event name and token
it('should construct a payload with the correct event name and token', () => {
const message = { type: 'track', event: 'purchase', properties: { total: 20, order_id: 123, products: [{ product_id: 12, name: "new prod" }] } };
const category = commonTrackMappingCategory
const destination = { Config: { bluecoreNamespace: 'namespace' } };
const eventName = 'purchase';

const expectedPayload = {
event: 'purchase',
token: 'namespace',
properties: {
"order_id": 123,
"products": [
{
"id": 12,
"name": "new prod",
},
],
"total": 20,
},
};

const result = trackResponseBuilder(message, category, destination, eventName);

expect(result).toEqual(expectedPayload);
});

// When the event name is 'optin', 'unsubscribe', or 'search', the function should not add a product array to the payload
it('need not add a product array to the payload when the event name is "optin"', () => {
const message = { type: 'track', event: 'optin', properties: { total: 20, order_id: 123, } };
const category = commonTrackMappingCategory
const destination = { Config: { bluecoreNamespace: 'namespace' } };
const eventName = 'optin';

const expectedPayload = {
event: 'optin',
token: 'namespace',
properties: {
order_id: 123,
total: 20,
},
};

const result = trackResponseBuilder(message, category, destination, eventName);

expect(result).toEqual(expectedPayload);
});

// When the event name is not 'optin', 'unsubscribe', or 'search', the function should add a product array to the payload
it('should add a product array to the payload when the event name is not "optin", "unsubscribe", or "search"', () => {
const message = { type: 'track', event: 'purchase', properties: { order_id: 123, total: 20, products: [{ id: '1', name: 'product' }] } };
const category = commonTrackMappingCategory
const destination = { Config: { bluecoreNamespace: 'namespace' } };
const eventName = 'purchase';

const expectedPayload = {
event: 'purchase',
token: 'namespace',
properties: {
order_id: 123,
total: 20,
products: [{ id: '1', name: 'product' }]
}
};

const result = trackResponseBuilder(message, category, destination, eventName);

expect(result).toEqual(expectedPayload);
});
});

describe('identifyResponseBuilder', () => {

// Given a valid message, category, destination config, and event name, the function should construct a payload with the correct event name and token
it('should construct a payload with the correct event name and token', () => {
const message = { type: 'identify', userId: '123', traits: { email: '[email protected]', name: 'test' } };
const category = commonIdentifyMappingCategory;
const destination = { Config: { bluecoreNamespace: 'namespace' } };

const expectedPayload = {
event: 'customer_patch',
token: 'namespace',
properties: {
"customer": {
"email": "[email protected]",
"name": "test",
},
"distinct_id": "[email protected]",
},
};

const result = identifyResponseBuilder(message, category, destination);

expect(result).toEqual(expectedPayload);
});

it('should construct a payload with the identify event name and token', () => {
const message = { type: 'identify', userId: '123', traits: { email: '[email protected]', name: 'test', action: 'identify' } };
const category = commonIdentifyMappingCategory;
const destination = { Config: { bluecoreNamespace: 'namespace' } };

const expectedPayload = {
event: 'identify',
token: 'namespace',
properties: {
"customer": {
"email": "[email protected]",
"name": "test",
},
"distinct_id": "[email protected]",
},
};

const result = identifyResponseBuilder(message, category, destination);

expect(result).toEqual(expectedPayload);
});

it('should construct a payload with the identify event name and token', () => {
const message = { type: 'identify', userId: '123', traits: { email: '[email protected]', name: 'test', action: 'random' } };
const category = commonIdentifyMappingCategory;
const destination = { Config: { bluecoreNamespace: 'namespace' } };

expect(() => {
identifyResponseBuilder(message, category, destination);
}).toThrow(InstrumentationError);
});
});


0 comments on commit ed01e14

Please sign in to comment.