Skip to content

Commit c90d8fc

Browse files
alanjcharlesAlan Charles
and
Alan Charles
authored
refactor: fix identify argument type and tests (#436)
* refactor: fix identify argument type and tests * refactor: fix test to account for traits object updates Co-authored-by: Alan Charles <[email protected]>
1 parent f698356 commit c90d8fc

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

packages/core/src/__tests__/methods/identify.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,27 @@ describe('methods #identify', () => {
7979
userId: 'new-user-id',
8080
});
8181
});
82+
83+
it('does not update userId when userId is undefined', () => {
84+
const client = new SegmentClient(clientArgs);
85+
jest.spyOn(client, 'process');
86+
87+
client.identify(undefined, { name: 'Mary' });
88+
89+
const expectedEvent = {
90+
traits: { name: 'Mary', age: 30 },
91+
userId: undefined,
92+
type: 'identify',
93+
};
94+
95+
expect(client.process).toHaveBeenCalledTimes(1);
96+
expect(client.process).toHaveBeenCalledWith(expectedEvent);
97+
expect(client.userInfo.get()).toEqual({
98+
...initialUserInfo,
99+
traits: {
100+
age: 30,
101+
name: 'Mary',
102+
},
103+
});
104+
});
82105
});

packages/core/src/analytics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ export class SegmentClient {
475475
this.logger.info('TRACK event saved', event);
476476
}
477477

478-
identify(userId: string, userTraits?: UserTraits) {
478+
identify(userId?: string, userTraits?: UserTraits) {
479479
const userInfo = this.store.userInfo.get();
480480
const { traits: currentUserTraits } = userInfo;
481481

@@ -491,7 +491,7 @@ export class SegmentClient {
491491

492492
this.store.userInfo.set({
493493
...userInfo,
494-
userId: userId,
494+
...(userId !== undefined ? { userId } : {}),
495495
traits: mergedTraits,
496496
});
497497

0 commit comments

Comments
 (0)