Skip to content

Commit

Permalink
fix: items check error when attribute value is undefined or null (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei authored Dec 14, 2023
1 parent 761d9ec commit 1009e2b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/provider/EventChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class EventChecker {
let errorMsg;
let error: EventError;
for (const [key, value] of Object.entries(item)) {
const valueStr = value.toString();
const valueStr = String(value);
if (!EventChecker.itemKeySet.has(key)) {
customKeyNumber += 1;
if (customKeyNumber > MAX_NUM_OF_CUSTOM_ITEM_ATTRIBUTE) {
Expand Down
30 changes: 30 additions & 0 deletions test/provider/AnalyticsEventBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ describe('AnalyticsEventBuilder test', () => {
);
});

test('test check event attribute with invalid value', () => {
let clickstreamAttribute: ClickstreamAttribute = {};
clickstreamAttribute['testUndefinedKey'] = undefined;
clickstreamAttribute['testNullKey'] = null;
clickstreamAttribute =
AnalyticsEventBuilder.getEventAttributesWithCheck(clickstreamAttribute);
expect(
Event.ReservedAttribute.ERROR_CODE in clickstreamAttribute
).toBeFalsy();
});

test('test check event attribute reached max attribute value length', () => {
let clickstreamAttribute: ClickstreamAttribute = {};
let longValue = '';
Expand Down Expand Up @@ -280,6 +291,25 @@ describe('AnalyticsEventBuilder test', () => {
);
});

test('test check item value for undefined or null', () => {
const clickstreamAttribute: ClickstreamAttribute = {};
const items: Item[] = [];
const item: Item = {
id: 'item_1',
undefinedKey: undefined,
nullKey: null,
};
items.push(item);
const resultItems = AnalyticsEventBuilder.getEventItemsWithCheck(
items,
clickstreamAttribute
);
expect(resultItems.length).toBe(1);
expect(
Event.ReservedAttribute.ERROR_CODE in clickstreamAttribute
).toBeFalsy();
});

test('test check event attributes will not affect global attributes', () => {
const customAttributes: ClickstreamAttribute = {
testKey: 'testValue',
Expand Down

0 comments on commit 1009e2b

Please sign in to comment.