Skip to content

Commit

Permalink
[MOB-9652] customEvent test case for nested JSON array
Browse files Browse the repository at this point in the history
  • Loading branch information
darshan-iterable committed Oct 2, 2024
1 parent e3c77f4 commit 8bb085e
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/anonymousUserTracking/criteriaCompletionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,12 @@ class CriteriaCompletionChecker {
);

if (field.includes('.')) {
const splitedField = field.split('.') as string[];
const splitField = field.split('.') as string[];
const fields =
eventData?.eventType === TRACK_EVENT &&
eventData?.eventName === splitedField[0]
? splitedField.slice(1)
: splitedField;
eventData?.eventName === splitField[0]
? splitField.slice(1)
: splitField;

let fieldValue = eventData;
let isSubFieldArray = false;
Expand Down
44 changes: 44 additions & 0 deletions src/anonymousUserTracking/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,50 @@ export const NESTED_CRITERIA_MULTI_LEVEL_ARRAY = {
]
};

export const NESTED_CRITERIA_MULTI_LEVEL_ARRAY_TRACK_EVENT = {
count: 1,
criteriaSets: [
{
criteriaId: '459',
name: 'event a.h.b=d && a.h.c=g',
createdAt: 1721251169153,
updatedAt: 1723488175352,
searchQuery: {
combinator: 'And',
searchQueries: [
{
combinator: 'And',
searchQueries: [
{
dataType: 'customEvent',
searchCombo: {
combinator: 'And',
searchQueries: [
{
dataType: 'customEvent',
field: 'TopLevelArrayObject.a.h.b',
comparatorType: 'Equals',
value: 'd',
fieldType: 'string'
},
{
dataType: 'customEvent',
field: 'TopLevelArrayObject.a.h.c',
comparatorType: 'Equals',
value: 'g',
fieldType: 'string'
}
]
}
}
]
}
]
}
}
]
};

export const IS_ONE_OF_CRITERIA = {
count: 1,
criteriaSets: [
Expand Down
126 changes: 125 additions & 1 deletion src/anonymousUserTracking/tests/nestedTesting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import CriteriaCompletionChecker from '../criteriaCompletionChecker';
import {
NESTED_CRITERIA,
NESTED_CRITERIA_MULTI_LEVEL,
NESTED_CRITERIA_MULTI_LEVEL_ARRAY
NESTED_CRITERIA_MULTI_LEVEL_ARRAY,
NESTED_CRITERIA_MULTI_LEVEL_ARRAY_TRACK_EVENT
} from './constants';

const localStorageMock = {
Expand Down Expand Up @@ -289,4 +290,127 @@ describe('nestedTesting', () => {
);
expect(result).toEqual('436');
});

it('should return criteriaId null (Multi level Nested field criteria - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataFields: {
furniture: {
material: [
{
type: 'table',
color: 'Gray',
lengthInches: 40,
widthInches: 60
},
{
type: 'Sofa',
color: 'black',
lengthInches: 20,
widthInches: 30
}
]
}
},
eventType: 'user'
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify(NESTED_CRITERIA_MULTI_LEVEL_ARRAY)
);
expect(result).toEqual(null);
});

it('should return criteriaId 459 (Multi level Nested field criteria)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventName: 'TopLevelArrayObject',
dataFields: {
a: {
h: [
{
b: 'e',
c: 'h'
},
{
b: 'd',
c: 'g'
}
]
}
},
eventType: 'customEvent'
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify(NESTED_CRITERIA_MULTI_LEVEL_ARRAY_TRACK_EVENT)
);
expect(result).toEqual('459');
});

it('should return criteriaId null (Multi level Nested field criteria - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventName: 'TopLevelArrayObject',
dataFields: {
a: {
h: [
{
b: 'd',
c: 'h'
},
{
b: 'e',
c: 'g'
}
]
}
},
eventType: 'customEvent'
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify(NESTED_CRITERIA_MULTI_LEVEL_ARRAY_TRACK_EVENT)
);
expect(result).toEqual(null);
});
});

0 comments on commit 8bb085e

Please sign in to comment.