From 158cd84af5a8185ca4e9a0bf99402da92e2280e9 Mon Sep 17 00:00:00 2001 From: shrouti1507 Date: Tue, 30 Apr 2024 16:24:14 +0530 Subject: [PATCH] fix: adding unit test cases for validate payload --- src/v0/destinations/algolia/util.js | 1 + src/v0/destinations/algolia/util.test.js | 58 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/v0/destinations/algolia/util.test.js diff --git a/src/v0/destinations/algolia/util.js b/src/v0/destinations/algolia/util.js index 11ce0a9ad5..b10097bbee 100644 --- a/src/v0/destinations/algolia/util.js +++ b/src/v0/destinations/algolia/util.js @@ -143,6 +143,7 @@ const clickPayloadValidator = (payload) => { return updatedPayload; }; +// ref : https://www.algolia.com/doc/rest-api/insights/#method-param-objectdata-2:~:text=currency-,%23,currency%20as%20ISO%2D4217%20currency%20code%2C%20such%20as%20USD%20or%20EUR.,-ObjectData function validatePayload(payload) { if (payload.objectData && Array.isArray(payload.objectData)) { const hasRelevantFields = payload.objectData.some( diff --git a/src/v0/destinations/algolia/util.test.js b/src/v0/destinations/algolia/util.test.js new file mode 100644 index 0000000000..850d93f1c6 --- /dev/null +++ b/src/v0/destinations/algolia/util.test.js @@ -0,0 +1,58 @@ +const { InstrumentationError } = require('@rudderstack/integrations-lib'); +const { validatePayload } = require('./util'); + +describe('validatePayload', () => { + // When payload is valid and contains relevant fields and currency + it('should validate payload when it is valid and contains relevant fields and currency', () => { + const payload = { + objectData: [ + { price: 10, quantity: 2, discount: 0.1 }, + { price: 20, quantity: 1, discount: 0 }, + ], + currency: 'USD', + }; + + expect(() => validatePayload(payload)).not.toThrow(); + }); + + // When payload contains objects with missing relevant fields + it('should throw an error when payload contains objects with missing relevant fields', () => { + const payload = { + objectData: [ + { price: 10, quantity: 2 }, + { price: 20, discount: 0 }, + ], + }; + + expect(() => validatePayload(payload)).toThrow(InstrumentationError); + }); + + // When payload is valid and contains relevant fields but no currency + it('should throw an InstrumentationError when currency is missing', () => { + const payload = { + objectData: [ + { price: 10, quantity: 2, discount: 0.1 }, + { price: 20, quantity: 1, discount: 0 }, + ], + }; + + expect(() => validatePayload(payload)).toThrow(InstrumentationError); + }); + + // When payload is valid but does not contain relevant fields + it('should not throw an error when payload does not contain relevant fields', () => { + const payload = { + objectData: [{ position: 'Product A' }, { position: 'Product B' }], + currency: 'USD', + }; + + expect(() => validatePayload(payload)).not.toThrow(); + }); + + // When payload is empty + it('should not throw an error when payload is empty', () => { + const payload = {}; + + expect(() => validatePayload(payload)).not.toThrow(); + }); +});