Skip to content

Commit fc40e4b

Browse files
jamesnroktclaude
andauthored
fix: Coerce event name to string (#1234)
* fix: Coerce event name to string * fix: update EventName comparisons and tests for string coercion Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Move waitForCondition to beforeEach Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent fc744e8 commit fc40e4b

6 files changed

Lines changed: 70 additions & 8 deletions

File tree

src/apiClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export default function APIClient(
176176
// https://go.mparticle.com/work/SQDSDKS-6935
177177
// While Event Name is 'usually' a string, there are some cases where it is a number
178178
// in that it could be a type of MessageType Enum
179-
if (event.EventName as unknown as number !== Types.MessageType.AppStateTransition) {
179+
if (event.EventName !== String(Types.MessageType.AppStateTransition)) {
180180
if (kitBlocker && kitBlocker.kitBlockingEnabled) {
181181
event = kitBlocker.createBlockedEvent(event);
182182
}

src/serverModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export default function ServerModel(
302302
// events do not have names, but are denoted by their `event_type`
303303
EventName:
304304
event.name ||
305-
((event.messageType as unknown) as string),
305+
String(event.messageType),
306306
EventCategory: event.eventType,
307307
EventAttributes: mpInstance._Helpers.sanitizeAttributes(
308308
event.data,

test/src/tests-batchUploader_3.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ describe('batch uploader', () => {
130130
let pendingEvents = window.mParticle.getInstance()._APIClient.uploader.eventsQueuedForProcessing;
131131

132132
pendingEvents.length.should.equal(3);
133-
pendingEvents[0].EventName.should.equal(1);
134-
pendingEvents[1].EventName.should.equal(10);
133+
pendingEvents[0].EventName.should.equal('1');
134+
pendingEvents[1].EventName.should.equal('10');
135135
pendingEvents[2].EventName.should.equal('Test Event');
136136

137137
fetchMock.post(urls.events, 200);

test/src/tests-batchUploader_4.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ describe('batch uploader', () => {
201201
const pendingEvents = window.mParticle.getInstance()._APIClient.uploader.eventsQueuedForProcessing;
202202

203203
pendingEvents.length.should.equal(3)
204-
pendingEvents[0].EventName.should.equal(1);
205-
pendingEvents[1].EventName.should.equal(10);
204+
pendingEvents[0].EventName.should.equal('1');
205+
pendingEvents[1].EventName.should.equal('10');
206206
pendingEvents[2].EventName.should.equal('Test Event');
207207

208208
window.mParticle.upload();

test/src/tests-forwarders.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,7 @@ describe('forwarders', function() {
22362236
mParticle.init(apiKey, window.mParticle.config);
22372237
await waitForCondition(hasIdentityCallInflightReturned);
22382238

2239-
window.MockForwarder1.instance.receivedEvent.EventName.should.equal(1);
2239+
window.MockForwarder1.instance.receivedEvent.EventName.should.equal('1');
22402240
window.MockForwarder1.instance.receivedEvent = null;
22412241

22422242
mParticle.logEvent(
@@ -2271,7 +2271,7 @@ describe('forwarders', function() {
22712271
mParticle.init(apiKey, window.mParticle.config);
22722272
await waitForCondition(hasIdentityCallInflightReturned);
22732273

2274-
window.MockForwarder1.instance.receivedEvent.EventName.should.equal(1);
2274+
window.MockForwarder1.instance.receivedEvent.EventName.should.equal('1');
22752275
window.MockForwarder1.instance.receivedEvent = null;
22762276

22772277
mParticle.logEvent(

test/src/tests-serverModel.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,68 @@ describe('ServerModel', () => {
468468
expect(mPStore.sessionStartDate).to.eql(null);
469469
});
470470

471+
describe('EventName coercion for lifecycle events with no name', () => {
472+
beforeEach(async () => {
473+
await waitForCondition(hasIdentifyReturned);
474+
});
475+
476+
it('should produce a string EventName for SessionStart', () => {
477+
const event: BaseEvent = {
478+
messageType: Types.MessageType.SessionStart,
479+
eventType: Types.EventType.Other,
480+
};
481+
482+
const actualEventObject = mParticle
483+
.getInstance()
484+
._ServerModel.createEventObject(event) as IUploadObject;
485+
486+
expect(typeof actualEventObject.EventName).to.equal('string');
487+
expect(actualEventObject.EventName).to.equal('1');
488+
});
489+
490+
it('should produce a string EventName for SessionEnd', () => {
491+
const event: BaseEvent = {
492+
messageType: Types.MessageType.SessionEnd,
493+
eventType: Types.EventType.Other,
494+
};
495+
496+
const actualEventObject = mParticle
497+
.getInstance()
498+
._ServerModel.createEventObject(event) as IUploadObject;
499+
500+
expect(typeof actualEventObject.EventName).to.equal('string');
501+
expect(actualEventObject.EventName).to.equal('2');
502+
});
503+
504+
it('should produce a string EventName for AppStateTransition', () => {
505+
const event: BaseEvent = {
506+
messageType: Types.MessageType.AppStateTransition,
507+
eventType: Types.EventType.Other,
508+
};
509+
510+
const actualEventObject = mParticle
511+
.getInstance()
512+
._ServerModel.createEventObject(event) as IUploadObject;
513+
514+
expect(typeof actualEventObject.EventName).to.equal('string');
515+
expect(actualEventObject.EventName).to.equal('10');
516+
});
517+
518+
it('should use the event name when provided, not the messageType', () => {
519+
const event: BaseEvent = {
520+
name: 'My Custom Event',
521+
messageType: Types.MessageType.PageEvent,
522+
eventType: Types.EventType.Navigation,
523+
};
524+
525+
const actualEventObject = mParticle
526+
.getInstance()
527+
._ServerModel.createEventObject(event) as IUploadObject;
528+
529+
expect(actualEventObject.EventName).to.equal('My Custom Event');
530+
});
531+
});
532+
471533
it('should set necessary attributes if MessageType is AppStateTransition', () => {
472534
const event: BaseEvent = {
473535
name: 'Test Event',

0 commit comments

Comments
 (0)