From c061661dc7159603c3f4099aeaefb0a4190e93e2 Mon Sep 17 00:00:00 2001 From: ItsSudip Date: Mon, 10 Jun 2024 13:35:37 +0530 Subject: [PATCH] chore: address comments --- .../integrations/AdobeAnalytics/util.test.js | 63 +++++++++++++ .../__tests__/utils/commonUtils.test.js | 91 +++++++++++++++++++ .../src/integrations/AdobeAnalytics/util.js | 2 +- 3 files changed, 155 insertions(+), 1 deletion(-) diff --git a/packages/analytics-js-integrations/__tests__/integrations/AdobeAnalytics/util.test.js b/packages/analytics-js-integrations/__tests__/integrations/AdobeAnalytics/util.test.js index fd35e35709..655132fbe3 100644 --- a/packages/analytics-js-integrations/__tests__/integrations/AdobeAnalytics/util.test.js +++ b/packages/analytics-js-integrations/__tests__/integrations/AdobeAnalytics/util.test.js @@ -3,6 +3,7 @@ import { handleLists, setConfig, mapMerchProductEvents, + get, } from '../../../src/integrations/AdobeAnalytics/util'; let windowSpy; @@ -176,4 +177,66 @@ describe('AdobeAnalytics Utility functions tests', () => { expect(result).toEqual([]); }); }); + describe('get tests', () => { + it('should retrieve a value from a nested object using a dot-separated string', () => { + const context = { + user: { + id: '123', + name: 'John Doe', + email: 'john.doe@example.com', + }, + }; + const value = 'user.name'; + const result = get(context, value); + expect(result).toBe('John Doe'); + }); + it('should return undefined when the path is an empty string', () => { + const context = { + user: { + id: '123', + name: 'John Doe', + }, + }; + const value = ''; + const result = get(context, value); + expect(result).toBeUndefined(); + }); + it('should return undefined when the key contains dot', () => { + const context = { + user: { + id: '123', + name: 'John Doe', + 'keyWith.dot': 'value', + }, + }; + const value = 'user.keyWith.dot'; + const result = get(context, value); + expect(result).toBeUndefined(); + }); + it('should return undefined when the key contains dot', () => { + const context = { + user: { + id: '123', + name: 'John Doe', + 'keyWith-dash': 'value', + }, + }; + const value = 'user.keyWith-dash'; + const result = get(context, value); + expect(result).toEqual('value'); + }); + it('should return correct value when the value is an array and provided index', () => { + const context = { + user: { + id: '123', + name: 'John Doe', + 'keyWith-dash': 'value', + keyWithArrayVal: ['value1', 'value2'], + }, + }; + const value = 'user.keyWithArrayVal.1'; + const result = get(context, value); + expect(result).toEqual('value2'); + }); + }); }); diff --git a/packages/analytics-js-integrations/__tests__/utils/commonUtils.test.js b/packages/analytics-js-integrations/__tests__/utils/commonUtils.test.js index 50f9e6b70e..eacc3e7881 100644 --- a/packages/analytics-js-integrations/__tests__/utils/commonUtils.test.js +++ b/packages/analytics-js-integrations/__tests__/utils/commonUtils.test.js @@ -355,4 +355,95 @@ describe('isBlank', () => { const result = utils.isBlank(null); expect(result).toBe(false); }); + it('should return true when input is a string of whitespaces', () => { + const result = utils.isBlank(' '); + expect(result).toBe(true); + }); + it('should return false when input is a valid string', () => { + const result = utils.isBlank('validString'); + expect(result).toBe(false); + }); + it('should return false when input is a valid number', () => { + const result = utils.isBlank(123456); + expect(result).toBe(false); + }); + + it('should return false when input is a valid object', () => { + const result = utils.isBlank({ key1: 'value1', key2: 'value2' }); + expect(result).toBe(false); + }); + it('should return false when input is a valid boolean', () => { + const result = utils.isBlank(false); + expect(result).toBe(false); + }); +}); + +describe('isNotEmpty', () => { + // returns false for empty string + it('should return false when input is an empty string', () => { + expect(utils.isNotEmpty('')).toBe(false); + }); + // returns false for null + it('should return false when input is null', () => { + expect(utils.isNotEmpty(null)).toBe(true); + }); + // returns true for non-empty string + it('should return true when input is a non-empty string', () => { + expect(utils.isNotEmpty('hello')).toBe(true); + }); + // returns false for empty object + it('should return false when input is an empty object', () => { + expect(utils.isNotEmpty({})).toBe(false); + }); + // returns true for non-empty object + it('should return true when input is a non-empty object', () => { + const obj = { key: 'value' }; + expect(utils.isNotEmpty(obj)).toBe(true); + }); + // returns true for booleans + it('should return true when input is a boolean', () => { + expect(utils.isNotEmpty(true)).toBe(false); + expect(utils.isNotEmpty(false)).toBe(false); + }); + // returns true for numbers + it('should return true when input is a number', () => { + expect(utils.isNotEmpty(5)).toBe(false); + expect(utils.isNotEmpty(0)).toBe(false); + expect(utils.isNotEmpty(-10)).toBe(false); + expect(utils.isNotEmpty(0.444548)).toBe(false); + }); + // returns false for undefined + it('should return false when input is undefined', () => { + expect(utils.isNotEmpty(undefined)).toBe(false); + }); + // returns true for non-empty array + it('should return true when input is a non-empty array', () => { + const input = [1, 2, 3]; + expect(utils.isNotEmpty(input)).toBe(true); + }); + // handles strings with only whitespace correctly + it('should return false when input is a string with only whitespace', () => { + expect(utils.isNotEmpty(' ')).toBe(true); + }); + // returns false for empty array + it('should return false when input is an empty array', () => { + expect(utils.isNotEmpty([])).toBe(false); + }); + // handles mixed data types within arrays + it('should return true when input is an array with mixed data types', () => { + const input = [1, 'hello', { key: 'value' }, true]; + expect(utils.isNotEmpty(input)).toBe(true); + }); + // handles functions and symbols correctly + it('should return true for functions and symbols', () => { + const func = () => {}; + const sym = Symbol('test'); + expect(utils.isNotEmpty(func)).toBe(false); + expect(utils.isNotEmpty(sym)).toBe(false); + }); + // handles Date objects correctly + it('should return true when input is a Date object', () => { + const date = new Date(); + expect(utils.isNotEmpty(date)).toBe(true); + }); }); diff --git a/packages/analytics-js-integrations/src/integrations/AdobeAnalytics/util.js b/packages/analytics-js-integrations/src/integrations/AdobeAnalytics/util.js index c71d93d48e..dc1efa8c09 100644 --- a/packages/analytics-js-integrations/src/integrations/AdobeAnalytics/util.js +++ b/packages/analytics-js-integrations/src/integrations/AdobeAnalytics/util.js @@ -7,7 +7,6 @@ import { path } from 'ramda'; import Logger from '../../utils/logger'; import { toIso, getHashFromArray, isDefinedAndNotNullAndNotEmpty } from '../../utils/commonUtils'; - const get = (context, value) => path(value.split('.'), context); const logger = new Logger(DISPLAY_NAME); @@ -676,4 +675,5 @@ export { setConfig, getConfig, handleVideoContextData, + get, };