Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOB-9145: support isOneOf and isNotOneOf comparator #446

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/anonymousUserTracking/criteriaCompletionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}

class CriteriaCompletionChecker {
private localStoredEventList: any[];

Check warning on line 39 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

constructor(localStoredEventList: string) {
this.localStoredEventList = JSON.parse(localStoredEventList);
Expand Down Expand Up @@ -72,9 +72,9 @@
return matchingCriteria ? matchingCriteria.criteriaId : null;
}

private prepareEventsToProcess(): any[] {

Check warning on line 75 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const eventsToProcess: any[] = this.getEventsWithCartItems();

Check warning on line 76 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const nonPurchaseEvents: any[] = this.getNonCartEvents();

Check warning on line 77 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

nonPurchaseEvents.forEach((event) => {
eventsToProcess.push(event);
Expand All @@ -83,7 +83,7 @@
return eventsToProcess;
}

private getEventsWithCartItems(): any[] {

Check warning on line 86 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const processedEvents: any[] = [];

this.localStoredEventList.forEach((localEventData) => {
Expand Down Expand Up @@ -367,14 +367,14 @@
return this.evaluateComparison(
query.comparatorType,
valueFromObj,
query.value ? query.value : ''
query.value ?? query.values ?? ''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if an empty string is evaluated? should we even run the evaluation at all if value or values isn't present?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So empty string is compared with the criteria value, empty string is not matched with value and gives result as false.
Yes, we run the evaluation.

);
}
} else if (eventKeyItems.length) {
return this.evaluateComparison(
query.comparatorType,
eventData[field],
query.value ? query.value : ''
query.value ?? query.values ?? ''
);
}
return false;
Expand Down Expand Up @@ -420,7 +420,7 @@
return this.evaluateComparison(
query.comparatorType,
item[field],
query.value ? query.value : ''
query.value ?? query.values ?? ''
);
}
return false;
Expand All @@ -430,7 +430,7 @@
private evaluateComparison(
comparatorType: string,
matchObj: any,
valueToCompare: string
valueToCompare: string | string[]
): boolean {
if (!valueToCompare && comparatorType !== 'IsSet') {
return false;
Expand All @@ -448,27 +448,36 @@
case 'LessThanOrEqualTo':
return this.compareNumericValues(
matchObj,
valueToCompare,
valueToCompare as string,
comparatorType
);
case 'Contains':
return this.compareStringContains(matchObj, valueToCompare);
return this.compareStringContains(matchObj, valueToCompare as string);
case 'StartsWith':
return this.compareStringStartsWith(matchObj, valueToCompare);
return this.compareStringStartsWith(matchObj, valueToCompare as string);
case 'MatchesRegex':
return this.compareWithRegex(matchObj, valueToCompare);
return this.compareWithRegex(matchObj, valueToCompare as string);
default:
return false;
}
}

private compareValueEquality(sourceTo: any, stringValue: string): boolean {
private compareValueEquality(
sourceTo: any,
stringValue: string | string[]
): boolean {
if (Array.isArray(sourceTo)) {
return sourceTo.some((source) =>
this.compareValueEquality(source, stringValue)
);
}

if (Array.isArray(stringValue)) {
return stringValue.some((value) =>
this.compareValueEquality(sourceTo, value)
);
}

if (
(typeof sourceTo === 'number' || typeof sourceTo === 'boolean') &&
stringValue !== ''
Expand Down
145 changes: 135 additions & 10 deletions src/anonymousUserTracking/tests/compareArrayDataTypes.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SHARED_PREFS_EVENT_LIST_KEY } from '../../constants';
import CriteriaCompletionChecker from '../criteriaCompletionChecker';
import {
IS_NOT_ONE_OF_CRITERIA,
ARRAY_CONTAINS_CRITERIA,
ARRAY_DOES_NOT_EQUAL_CRITERIA,
ARRAY_EQUAL_CRITERIA,
Expand All @@ -9,7 +10,8 @@ import {
ARRAY_LESS_THAN_CRITERIA,
ARRAY_LESS_THAN_EQUAL_TO_CRITERIA,
ARRAY_MATCHREGEX_CRITERIA,
ARRAY_STARTSWITH_CRITERIA
ARRAY_STARTSWITH_CRITERIA,
IS_ONE_OF_CRITERIA
} from './constants';

const localStorageMock = {
Expand Down Expand Up @@ -64,7 +66,8 @@ describe('compareArrayDataTypes', () => {
expect(result).toEqual('285');
});

it('should return criteriaId 285 (compare array Equal - No match)', () => {

it('should return criteriaId null (compare array Equal - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
Expand Down Expand Up @@ -143,7 +146,7 @@ describe('compareArrayDataTypes', () => {
expect(result).toEqual('285');
});

it('should return criteriaId 285 (compare array DoesNotEqual - No match)', () => {
it('should return criteriaId null (compare array DoesNotEqual - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
Expand Down Expand Up @@ -212,7 +215,7 @@ describe('compareArrayDataTypes', () => {
expect(result).toEqual('285');
});

it('should return criteriaId 285 (compare array GreaterThan - No match)', () => {
it('should return criteriaId null (compare array GreaterThan - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
Expand Down Expand Up @@ -269,7 +272,7 @@ describe('compareArrayDataTypes', () => {
expect(result).toEqual('285');
});

it('should return criteriaId 285 (compare array LessThan - No match)', () => {
it('should return criteriaId null (compare array LessThan - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
Expand Down Expand Up @@ -326,7 +329,7 @@ describe('compareArrayDataTypes', () => {
expect(result).toEqual('285');
});

it('should return criteriaId 285 (compare array GreaterThanOrEqualTo - No match)', () => {
it('should return criteriaId null (compare array GreaterThanOrEqualTo - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
Expand Down Expand Up @@ -383,7 +386,7 @@ describe('compareArrayDataTypes', () => {
expect(result).toEqual('285');
});

it('should return criteriaId 285 (compare array LessThanOrEqualTo - No match)', () => {
it('should return criteriaId null (compare array LessThanOrEqualTo - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
Expand Down Expand Up @@ -448,7 +451,7 @@ describe('compareArrayDataTypes', () => {
expect(result).toEqual('285');
});

it('should return criteriaId 285 (compare array Contains - No match)', () => {
it('should return criteriaId null (compare array Contains - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
Expand Down Expand Up @@ -513,7 +516,7 @@ describe('compareArrayDataTypes', () => {
expect(result).toEqual('285');
});

it('should return criteriaId 285 (compare array StartsWith - No match)', () => {
it('should return criteriaId null (compare array StartsWith - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
Expand Down Expand Up @@ -579,7 +582,7 @@ describe('compareArrayDataTypes', () => {
expect(result).toEqual('285');
});

it('should return criteriaId 285 (compare array MatchesRegex - No match)', () => {
it('should return criteriaId null (compare array MatchesRegex - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
Expand Down Expand Up @@ -612,4 +615,126 @@ describe('compareArrayDataTypes', () => {
);
expect(result).toEqual(null);
});

// MARK: IsOneOf
it('should return criteriaId 299 (compare array IsOneOf)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataFields: {
country: 'China',
addresses: ['US', 'UK', 'JP', 'DE', 'GB']
},
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(IS_ONE_OF_CRITERIA)
);
expect(result).toEqual('299');
});

it('should return criteriaId null (compare array IsOneOf - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataFields: {
country: 'Korea',
addresses: ['US', 'UK']
},
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(IS_ONE_OF_CRITERIA)
);
expect(result).toEqual(null);
});

// MARK: IsNotOneOf
it('should return criteriaId 299 (compare array IsNotOneOf)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataFields: {
country: 'Korea',
addresses: ['US', 'UK']
},
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(IS_NOT_ONE_OF_CRITERIA)
);
expect(result).toEqual('299');
});

it('should return criteriaId null (compare array IsNotOneOf - No match)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataFields: {
country: 'China',
addresses: ['US', 'UK', 'JP', 'DE', 'GB']
},
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(IS_NOT_ONE_OF_CRITERIA)
);
expect(result).toEqual(null);
});
});
84 changes: 84 additions & 0 deletions src/anonymousUserTracking/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,87 @@ export const NESTED_CRITERIA = {
}
]
};

export const IS_ONE_OF_CRITERIA = {
count: 1,
criterias: [
{
criteriaId: '299',
name: 'Criteria_Is_One_of',
createdAt: 1722851586508,
updatedAt: 1724404229481,
searchQuery: {
combinator: 'And',
searchQueries: [
{
combinator: 'And',
searchQueries: [
{
dataType: 'user',
searchCombo: {
combinator: 'And',
searchQueries: [
{
dataType: 'user',
field: 'country',
comparatorType: 'Equals',
values: ['China', 'Japan', 'Kenya']
},
{
dataType: 'user',
field: 'addresses',
comparatorType: 'Equals',
values: ['JP', 'DE', 'GB']
}
]
}
}
]
}
]
}
}
]
};

export const IS_NOT_ONE_OF_CRITERIA = {
count: 1,
criterias: [
{
criteriaId: '299',
name: 'Criteria_Is_Not_One_of',
createdAt: 1722851586508,
updatedAt: 1724404229481,
searchQuery: {
combinator: 'And',
searchQueries: [
{
combinator: 'And',
searchQueries: [
{
dataType: 'user',
searchCombo: {
combinator: 'And',
searchQueries: [
{
dataType: 'user',
field: 'country',
comparatorType: 'DoesNotEqual',
values: ['China', 'Japan', 'Kenya']
},
{
dataType: 'user',
field: 'addresses',
comparatorType: 'DoesNotEqual',
values: ['JP', 'DE', 'GB']
}
]
}
}
]
}
]
}
}
]
};
Loading