Skip to content

Commit

Permalink
[MOB-9652] support for nested JSON array (#459)
Browse files Browse the repository at this point in the history
* MOB-9650 Added support for nested criteria match a.b.c

* fix: removed updatecart from nested criteria

* [MOB-9652] support for nested JSON array

* [MOB-9652] customEvent test case for nested JSON array

* [MOB-9168] Automated unit tests against complex criteria (#461)

* [MOB-9168] Automated unit tests against complex criteria

* [MOB-9168] Automated unit tests against complex criteria
  • Loading branch information
darshan-iterable authored Oct 7, 2024
1 parent 604ea66 commit c720d4f
Show file tree
Hide file tree
Showing 4 changed files with 946 additions and 11 deletions.
45 changes: 35 additions & 10 deletions src/anonymousUserTracking/criteriaCompletionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,41 @@ class CriteriaCompletionChecker {
);

if (field.includes('.')) {
const fields = field.split('.');
const firstElement = eventData?.[fields[0]];
if (Array.isArray(firstElement)) {
return firstElement?.some((item: any) => {
const data = {
...eventData,
[fields[0]]: item
};
return this.evaluateFieldLogic(searchQueries, data);
});
const splitField = field.split('.') as string[];
const fields =
eventData?.eventType === TRACK_EVENT &&
eventData?.eventName === splitField[0]
? splitField.slice(1)
: splitField;

let fieldValue = eventData;
let isSubFieldArray = false;
let isSubMatch = false;

fields.forEach((subField) => {
const subFieldValue = fieldValue[subField];
if (Array.isArray(subFieldValue)) {
isSubFieldArray = true;
isSubMatch = subFieldValue.some((item: any) => {
const data = fields.reduceRight((acc: any, key) => {
if (key === subField) {
return { [key]: item };
}
return { [key]: acc };
}, {});

return this.evaluateFieldLogic(searchQueries, {
...eventData,
...data
});
});
} else {
fieldValue = subFieldValue;
}
});

if (isSubFieldArray) {
return isSubMatch;
}

const valueFromObj = this.getFieldValue(eventData, field);
Expand Down
253 changes: 253 additions & 0 deletions src/anonymousUserTracking/tests/complexCriteria.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { SHARED_PREFS_EVENT_LIST_KEY } from '../../constants';
import CriteriaCompletionChecker from '../criteriaCompletionChecker';
import {
COMPLEX_CRITERIA_1,
COMPLEX_CRITERIA_2,
COMPLEX_CRITERIA_3
} from './constants';

const localStorageMock = {
getItem: jest.fn(),
Expand Down Expand Up @@ -1598,4 +1603,252 @@ describe('complexCriteria', () => {
);
expect(result).toEqual(null);
});

// MARK: Complex criteria #1
it('should return criteriaId 290 if (1 OR 2 OR 3) AND (4 AND 5) AND (6 NOT 7) matched', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataFields: {
saved_cars: { color: 'black' },
'animal-found': { vaccinated: true },
eventName: 'birthday'
},
eventType: 'customEvent'
},
{
dataFields: { reason: 'testing', total: 30 },
eventType: 'purchase'
},
{
dataFields: { firstName: 'Adam' },
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(COMPLEX_CRITERIA_1)
);
expect(result).toEqual('290');
});

it('should return criteriaId null if (1 OR 2 OR 3) AND (4 AND 5) AND (6 NOT 7) - No match', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventType: 'user',
dataFields: {
firstName: 'Alex'
}
},
{
eventType: 'customEvent',
eventName: 'saved_cars',
dataFields: {
color: ''
}
},
{
eventType: 'customEvent',
eventName: 'animal-found',
dataFields: {
vaccinated: true
}
},
{
eventType: 'purchase',
dataFields: {
total: 30,
reason: 'testing'
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

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

// MARK: Complex criteria #2
it('should return criteriaId 291 if (6 OR 7) OR (4 AND 5) OR (1 NOT 2 NOT 3) matched', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventType: 'user',
dataFields: {
firstName: 'xcode'
}
},
{
eventType: 'customEvent',
eventName: 'saved_cars',
dataFields: {
color: 'black'
}
},
{
eventType: 'customEvent',
eventName: 'animal-found',
dataFields: {
vaccinated: true
}
},
{
eventType: 'purchase',
dataFields: {
total: 110,
reason: 'testing'
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

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

it('should return criteriaId null if (6 OR 7) OR (4 AND 5) OR (1 NOT 2 NOT 3) - No match', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventType: 'user',
dataFields: {
firstName: 'Alex'
}
},
{
eventType: 'purchase',
dataFields: {
total: 10,
reason: 'null'
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

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

// MARK: Complex criteria #3
it('should return criteriaId 292 if (1 AND 2) NOR (3 OR 4 OR 5) NOR (6 NOR 7) matched', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataType: 'user',
dataFields: {
firstName: 'xcode',
lastName: 'ssr'
}
},
{
dataType: 'customEvent',
eventName: 'animal-found',
dataFields: {
vaccinated: true,
count: 10
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

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

it('should return criteriaId null if (1 AND 2) NOR (3 OR 4 OR 5) NOR (6 NOR 7) - No match', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventType: 'user',
dataFields: {
firstName: 'Alex',
lastName: 'Aris'
}
},
{
eventType: 'customEvent',
eventName: 'animal-found',
dataFields: {
vaccinated: false,
count: 4
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

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

0 comments on commit c720d4f

Please sign in to comment.