Skip to content

Commit

Permalink
fix: adding unit test cases for validate payload
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Apr 30, 2024
1 parent 0bee394 commit 158cd84
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/v0/destinations/algolia/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
58 changes: 58 additions & 0 deletions src/v0/destinations/algolia/util.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 158cd84

Please sign in to comment.