From d03ad20ef7a6da899d8276d3adffe6bc2ed2d64f Mon Sep 17 00:00:00 2001 From: Aanshi Lahoti Date: Mon, 14 Oct 2024 16:39:40 +0530 Subject: [PATCH] chore: test cases added --- src/v0/destinations/tune/transform.js | 2 + src/v0/destinations/tune/util.test.js | 148 ++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 src/v0/destinations/tune/util.test.js diff --git a/src/v0/destinations/tune/transform.js b/src/v0/destinations/tune/transform.js index 11996c99dc..79ef9c0f34 100644 --- a/src/v0/destinations/tune/transform.js +++ b/src/v0/destinations/tune/transform.js @@ -66,6 +66,8 @@ const processRouterDest = async (inputs, reqMetadata) => { }; module.exports = { + responseBuilder, + processEvent, process, processRouterDest, }; diff --git a/src/v0/destinations/tune/util.test.js b/src/v0/destinations/tune/util.test.js new file mode 100644 index 0000000000..58c0811986 --- /dev/null +++ b/src/v0/destinations/tune/util.test.js @@ -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), + ); + }); +});