From 510d255f6eefbb2a9ae28af92d21b229e03116bf Mon Sep 17 00:00:00 2001 From: mateo-villa Date: Sat, 5 Oct 2024 18:13:03 -0400 Subject: [PATCH 1/2] [RUM-5418] Add RN API to append to existing user info --- packages/core/src/DdSdkReactNative.tsx | 16 ++++++++++++++++ .../src/__tests__/DdSdkReactNative.test.tsx | 19 +++++++++++++++++++ packages/core/src/types.tsx | 6 ++++++ 3 files changed, 41 insertions(+) diff --git a/packages/core/src/DdSdkReactNative.tsx b/packages/core/src/DdSdkReactNative.tsx index 3a70739cb..498065fac 100644 --- a/packages/core/src/DdSdkReactNative.tsx +++ b/packages/core/src/DdSdkReactNative.tsx @@ -196,6 +196,22 @@ export class DdSdkReactNative { UserInfoSingleton.getInstance().setUserInfo(user); }; + /** + * Set the user information. + * @param extraUserInfo: The extra info object (use builtin attributes: 'id', 'email', 'name', and/or any custom attribute). + * @returns a Promise. + */ + static addUserExtraInfo = async (extraUserInfo: UserInfo): Promise => { + InternalLog.log( + `Adding extra user info ${JSON.stringify(extraUserInfo)}`, + SdkVerbosity.DEBUG + ); + const userInfo = UserInfoSingleton.getInstance().getUserInfo(); + const updatedUserInfo = { ...userInfo, ...extraUserInfo }; + await DdSdk.setUser(updatedUserInfo); + UserInfoSingleton.getInstance().setUserInfo(updatedUserInfo); + }; + /** * Set the tracking consent regarding the data collection. * @param trackingConsent: One of TrackingConsent values. diff --git a/packages/core/src/__tests__/DdSdkReactNative.test.tsx b/packages/core/src/__tests__/DdSdkReactNative.test.tsx index 65a161718..c667c1a08 100644 --- a/packages/core/src/__tests__/DdSdkReactNative.test.tsx +++ b/packages/core/src/__tests__/DdSdkReactNative.test.tsx @@ -1055,6 +1055,25 @@ describe('DdSdkReactNative', () => { }); }); + describe('addUserExtraInfo', () => { + it('calls SDK method when addUserExtraInfo, and updates the user in UserProvider', async () => { + // GIVEN + await DdSdkReactNative.setUser({ id: 'id' }); + const extraInfo = { id: 'updatedId', foo: 'bar' }; + + // WHEN + await DdSdkReactNative.addUserExtraInfo(extraInfo); + + // THEN + expect(DdSdk.setUser).toHaveBeenCalledTimes(2); + expect(DdSdk.setUser).toHaveBeenCalledWith(extraInfo); + expect(UserInfoSingleton.getInstance().getUserInfo()).toEqual({ + id: 'updatedId', + foo: 'bar' + }); + }); + }); + describe('setTrackingConsent', () => { it('calls SDK method when setTrackingConsent', async () => { // GIVEN diff --git a/packages/core/src/types.tsx b/packages/core/src/types.tsx index 9d0b0087f..34a3f8cda 100644 --- a/packages/core/src/types.tsx +++ b/packages/core/src/types.tsx @@ -82,6 +82,12 @@ export type DdSdkType = { */ setUser(user: object): Promise; + /** + * Add additional user information. + * @param extraUserInfo: The extra info object (use builtin attributes: 'id', 'email', 'name', and/or any custom attribute). + */ + addUserExtraInfo(extraUserInfo: object): Promise; + /** * Set the tracking consent regarding the data collection. * @param trackingConsent: Consent, which can take one of the following values: 'pending', 'granted', 'not_granted'. From eae542bba73876cfd6fec049c087950692c59a76 Mon Sep 17 00:00:00 2001 From: mateo-villa Date: Mon, 7 Oct 2024 10:42:31 -0400 Subject: [PATCH 2/2] [RUM-5418] Add new RN API to mock and fix linting --- packages/core/jest/mock.js | 3 +++ packages/core/src/DdSdkReactNative.tsx | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/core/jest/mock.js b/packages/core/jest/mock.js index fca9a7e4e..193c98a36 100644 --- a/packages/core/jest/mock.js +++ b/packages/core/jest/mock.js @@ -30,6 +30,9 @@ module.exports = { setUser: jest .fn() .mockImplementation(() => new Promise(resolve => resolve())), + addUserExtraInfo: jest + .fn() + .mockImplementation(() => new Promise(resolve => resolve())), setAttributes: jest .fn() .mockImplementation(() => new Promise(resolve => resolve())), diff --git a/packages/core/src/DdSdkReactNative.tsx b/packages/core/src/DdSdkReactNative.tsx index 498065fac..65642744c 100644 --- a/packages/core/src/DdSdkReactNative.tsx +++ b/packages/core/src/DdSdkReactNative.tsx @@ -201,7 +201,9 @@ export class DdSdkReactNative { * @param extraUserInfo: The extra info object (use builtin attributes: 'id', 'email', 'name', and/or any custom attribute). * @returns a Promise. */ - static addUserExtraInfo = async (extraUserInfo: UserInfo): Promise => { + static addUserExtraInfo = async ( + extraUserInfo: UserInfo + ): Promise => { InternalLog.log( `Adding extra user info ${JSON.stringify(extraUserInfo)}`, SdkVerbosity.DEBUG