Skip to content

Commit

Permalink
Fixed bools needs to be string (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
darshan-iterable authored Jul 31, 2024
1 parent 06c55d9 commit 7c04dba
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion react-example/src/views/AUTTesting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const AUTTesting: FC<Props> = () => {
const [isUpdatingCart, setUpdatingCart] = useState<boolean>(false);
const [isTrackingPurchase, setTrackingPurchase] = useState<boolean>(false);
const [userDataField, setUserDataField] = useState<string>(
' { "dataFields": {"phone_number": "57688559" }}'
' { "dataFields": {"phone_number": "57688559", "subscribed": true }}'
);
const [isUpdatingUser, setUpdatingUser] = useState<boolean>(false);
const [updateUserResponse, setUpdateUserResponse] = useState<string>(
Expand Down
7 changes: 4 additions & 3 deletions src/anonymousUserTracking/criteriaCompletionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,14 @@ class CriteriaCompletionChecker {
private compareValueEquality(sourceTo: any, stringValue: string): boolean {
if (
(typeof sourceTo === 'number' || typeof sourceTo === 'boolean') &&
stringValue !== '' &&
!isNaN(parseFloat(stringValue))
stringValue !== ''
) {
if (typeof sourceTo === 'number') {
if (typeof sourceTo === 'number' && !isNaN(parseFloat(stringValue))) {
return sourceTo === parseFloat(stringValue);
} else if (typeof sourceTo === 'boolean') {
return sourceTo === (stringValue === 'true');
} else {
return false;
}
} else if (typeof sourceTo === 'string') {
return sourceTo === stringValue;
Expand Down
73 changes: 73 additions & 0 deletions src/anonymousUserTracking/tests/criteriaCompletionChecker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1445,4 +1445,77 @@ describe('CriteriaCompletionChecker', () => {
);
expect(result).toEqual(null);
});

it('should return criteriaId 100 (boolean test)', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataFields: {
subscribed: true,
phoneNumber: '99999999'
},
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({
count: 1,
criterias: [
{
criteriaId: '100',
name: 'User',
createdAt: 1716560453973,
updatedAt: 1716560453973,
searchQuery: {
combinator: 'And',
searchQueries: [
{
combinator: 'And',
searchQueries: [
{
dataType: 'user',
searchCombo: {
combinator: 'And',
searchQueries: [
{
field: 'subscribed',
fieldType: 'boolean',
comparatorType: 'Equals',
dataType: 'user',
id: 25,
value: 'true'
},
{
field: 'phoneNumber',
fieldType: 'String',
comparatorType: 'IsSet',
dataType: 'user',
id: 28,
value: ''
}
]
}
}
]
}
]
}
}
]
})
);
expect(result).toEqual('100');
});
});

0 comments on commit 7c04dba

Please sign in to comment.