Skip to content

Commit

Permalink
chore: test cases added
Browse files Browse the repository at this point in the history
  • Loading branch information
aanshi07 committed Oct 14, 2024
1 parent 69f3af3 commit d03ad20
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/v0/destinations/tune/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const processRouterDest = async (inputs, reqMetadata) => {
};

module.exports = {
responseBuilder,
processEvent,
process,
processRouterDest,
};
148 changes: 148 additions & 0 deletions src/v0/destinations/tune/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { InstrumentationError } from '@rudderstack/integrations-lib';
import { responseBuilder, processEvent } from './transform';
describe('responseBuilder', () => {
// Correctly maps properties to destination keys based on eventsMapping
it('should map properties to destination keys when eventsMapping is provided', () => {
const message = {
event: 'product list viewed',
properties: {
platform: 'meta',
conversions: 1,
ad_unit_id: 221187,
ad_interaction_time: '1652826278',
},
};
const Config = {
tuneEvents: [
{
eventName: 'product list viewed',
eventsMapping: [
{ from: 'platform', to: 'destinationPlatform' },
{ from: 'conversions', to: 'destinationConversions' },
],
url: 'https://example.com/event',
},
],
};
const expectedParams = {
destinationPlatform: 'meta',
destinationConversions: 1,
};
const response = responseBuilder(message, { Config });
expect(response.params).toEqual(expectedParams);
expect(response.endpoint).toBe('https://example.com/event');
});

// Handles empty properties object without errors
it('should handle empty properties object without throwing errors', () => {
const message = {
event: 'product list viewed',
properties: {},
};
const Config = {
tuneEvents: [
{
eventName: 'product list viewed',
eventsMapping: [
{ from: 'platform', to: 'destinationPlatform' },
{ from: 'conversions', to: 'destinationConversions' },
],
url: 'https://example.com/event',
},
],
};
const response = responseBuilder(message, { Config });
expect(response.params).toEqual({});
expect(response.endpoint).toBe('https://example.com/event');
});
});

describe('processEvent', () => {
// Processes 'track' messages correctly using responseBuilder
it('should process "track" messages correctly using responseBuilder', () => {
const message = {
type: 'track',
event: 'product list viewed',
properties: {
platform: 'meta',
conversions: 1,
ad_unit_id: 221187,
ad_interaction_time: '1652826278',
},
};
const destination = {
Config: {
tuneEvents: [
{
eventName: 'product list viewed',
url: 'https://example.com/track',
eventsMapping: [
{ from: 'platform', to: 'platform_key' },
{ from: 'conversions', to: 'conversions_key' },
],
},
],
},
};
const expectedResponse = {
body: {
FORM: {},
JSON: {},
JSON_ARRAY: {},
XML: {},
},
params: {
platform_key: 'meta',
conversions_key: 1,
},
endpoint: 'https://example.com/track',
event: 'product list viewed',
files: {},
headers: {},
method: 'POST',
type: 'REST',
version: '1',
};
const response = processEvent(message, destination);
expect(response).toEqual(expectedResponse);
});

// Throws an error if message type is missing
it('should throw an error if message type is missing', () => {
const message = {
event: 'product list viewed',
properties: {
platform: 'meta',
conversions: 1,
},
};
const destination = {
Config: {
tuneEvents: [],
},
};
expect(() => processEvent(message, destination)).toThrowError(
new InstrumentationError('Message Type is not present. Aborting message.', 400),
);
});

it('should throw an error when message type is not "track"', () => {
const message = {
type: 'identify',
event: 'product list viewed',
properties: {
platform: 'meta',
conversions: 1,
},
};
const destination = {
Config: {
tuneEvents: [],
},
};

expect(() => processEvent(message, destination)).toThrowError(
new InstrumentationError('Message type not supported. Only "track" is allowed.', 400),
);
});
});

0 comments on commit d03ad20

Please sign in to comment.