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-9168] Automated unit tests against complex criteria #461

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
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
Loading