Skip to content

Commit

Permalink
chore: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSudip committed Jun 10, 2024
1 parent dae6b42 commit c061661
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
handleLists,
setConfig,
mapMerchProductEvents,
get,
} from '../../../src/integrations/AdobeAnalytics/util';

let windowSpy;
Expand Down Expand Up @@ -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: '[email protected]',
},
};
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');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -676,4 +675,5 @@ export {
setConfig,
getConfig,
handleVideoContextData,
get,
};

0 comments on commit c061661

Please sign in to comment.