Skip to content
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
14 changes: 14 additions & 0 deletions packages/core/__mocks__/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ actualRN.NativeModules.DdSdk = {
clearAllData: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['clearAllData']>,
setAccountInfo: jest.fn().mockImplementation(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These mocks were missing.

() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['setAccountInfo']>,
clearAccountInfo: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['clearAccountInfo']>,
addAccountExtraInfo: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['addAccountExtraInfo']>,
getConstants: jest
.fn()
.mockImplementation(() => ({})) as jest.MockedFunction<
DdNativeSdkType['getConstants']
>,
addListener: jest.fn().mockImplementation((_: string) => {
/* empty */
}) as jest.MockedFunction<DdNativeSdkType['addListener']>,
Expand Down
30 changes: 15 additions & 15 deletions packages/core/src/DdSdkReactNative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { AttributesSingleton } from './sdk/AttributesSingleton/AttributesSinglet
import type { Attributes } from './sdk/AttributesSingleton/types';
import { registerNativeBridge } from './sdk/DatadogInternalBridge/DdSdkInternalNativeBridge';
import { BufferSingleton } from './sdk/DatadogProvider/Buffer/BufferSingleton';
import { NativeDdSdk } from './sdk/DdSdkInternal';
import { DdSdk } from './sdk/DdSdk';
import { GlobalState } from './sdk/GlobalState/GlobalState';
import { UserInfoSingleton } from './sdk/UserInfoSingleton/UserInfoSingleton';
import type { UserInfo } from './sdk/UserInfoSingleton/types';
Expand Down Expand Up @@ -86,7 +86,7 @@ export class DdSdkReactNative {
SdkVerbosity.WARN
);
if (!__DEV__) {
NativeDdSdk.telemetryDebug(
DdSdk.telemetryDebug(
'RN SDK was already initialized in javascript'
);
}
Expand All @@ -97,7 +97,7 @@ export class DdSdkReactNative {

registerNativeBridge();

await NativeDdSdk.initialize(
await DdSdk.initialize(
DdSdkReactNative.buildConfiguration(configuration, params)
);
Comment on lines +100 to 102

Expand Down Expand Up @@ -218,7 +218,7 @@ export class DdSdkReactNative {
`Adding attribute ${JSON.stringify(value)} for key ${key}`,
SdkVerbosity.DEBUG
);
await NativeDdSdk.addAttribute(key, { value });
await DdSdk.addAttribute(key, { value });
AttributesSingleton.getInstance().addAttribute(key, value);
};

Expand All @@ -231,7 +231,7 @@ export class DdSdkReactNative {
`Removing attribute for key ${key}`,
SdkVerbosity.DEBUG
);
await NativeDdSdk.removeAttribute(key);
await DdSdk.removeAttribute(key);
AttributesSingleton.getInstance().removeAttribute(key);
};

Expand All @@ -245,7 +245,7 @@ export class DdSdkReactNative {
`Adding attributes ${JSON.stringify(attributes)}`,
SdkVerbosity.DEBUG
);
await NativeDdSdk.addAttributes(attributes);
await DdSdk.addAttributes(attributes);
AttributesSingleton.getInstance().addAttributes(attributes);
};

Expand All @@ -258,7 +258,7 @@ export class DdSdkReactNative {
`Removing attributes for keys ${JSON.stringify(keys)}`,
SdkVerbosity.DEBUG
);
await NativeDdSdk.removeAttributes(keys);
await DdSdk.removeAttributes(keys);
AttributesSingleton.getInstance().removeAttributes(keys);
};

Expand All @@ -285,7 +285,7 @@ export class DdSdkReactNative {
SdkVerbosity.DEBUG
);

await NativeDdSdk.setUserInfo(userInfo);
await DdSdk.setUserInfo(userInfo);
UserInfoSingleton.getInstance().setUserInfo(userInfo);
};

Expand All @@ -295,7 +295,7 @@ export class DdSdkReactNative {
*/
static clearUserInfo = async (): Promise<void> => {
InternalLog.log('Clearing user info', SdkVerbosity.DEBUG);
await NativeDdSdk.clearUserInfo();
await DdSdk.clearUserInfo();
UserInfoSingleton.getInstance().clearUserInfo();
};

Expand All @@ -312,7 +312,7 @@ export class DdSdkReactNative {
SdkVerbosity.DEBUG
);

await NativeDdSdk.addUserExtraInfo(extraUserInfo);
await DdSdk.addUserExtraInfo(extraUserInfo);
UserInfoSingleton.getInstance().addUserExtraInfo(extraUserInfo);
};

Expand All @@ -333,7 +333,7 @@ export class DdSdkReactNative {
SdkVerbosity.DEBUG
);

await NativeDdSdk.setAccountInfo(accountInfo);
await DdSdk.setAccountInfo(accountInfo);
AccountInfoSingleton.getInstance().setAccountInfo(accountInfo);
};

Expand All @@ -343,7 +343,7 @@ export class DdSdkReactNative {
*/
static clearAccountInfo = async (): Promise<void> => {
InternalLog.log('Clearing account info', SdkVerbosity.DEBUG);
await NativeDdSdk.clearAccountInfo();
await DdSdk.clearAccountInfo();
AccountInfoSingleton.getInstance().clearAccountInfo();
};

Expand Down Expand Up @@ -375,7 +375,7 @@ export class DdSdkReactNative {
...extraAccountInfo
};

await NativeDdSdk.addAccountExtraInfo(extraInfo);
await DdSdk.addAccountExtraInfo(extraInfo);
AccountInfoSingleton.getInstance().addAccountExtraInfo(
extraAccountInfo
);
Expand All @@ -388,7 +388,7 @@ export class DdSdkReactNative {
*/
static setTrackingConsent = (consent: TrackingConsent): Promise<void> => {
InternalLog.log(`Setting consent ${consent}`, SdkVerbosity.DEBUG);
return NativeDdSdk.setTrackingConsent(consent);
return DdSdk.setTrackingConsent(consent);
};

/**
Expand All @@ -397,7 +397,7 @@ export class DdSdkReactNative {
*/
static clearAllData = (): Promise<void> => {
InternalLog.log('Clearing all data', SdkVerbosity.DEBUG);
return NativeDdSdk.clearAllData();
return DdSdk.clearAllData();
};

private static buildConfiguration = (
Expand Down
158 changes: 139 additions & 19 deletions packages/core/src/__tests__/DdSdkReactNative.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import { DdRumUserInteractionTracking } from '../rum/instrumentation/interaction
import { DdRumResourceTracking } from '../rum/instrumentation/resourceTracking/DdRumResourceTracking';
import { PropagatorType, RumActionType } from '../rum/types';
import { AttributesSingleton } from '../sdk/AttributesSingleton/AttributesSingleton';
import { NativeDdSdk } from '../sdk/DdSdkInternal';
import { DdSdkWrapper } from '../sdk/DdSdkInternal';
import { GlobalState } from '../sdk/GlobalState/GlobalState';
import { UserInfoSingleton } from '../sdk/UserInfoSingleton/UserInfoSingleton';
import NativeDdSdkSpec from '../specs/NativeDdSdk';
import type { LogEvent } from '../types';
import { ErrorSource } from '../types';
import { version as sdkVersion } from '../version';
Expand Down Expand Up @@ -1184,8 +1185,8 @@ describe('DdSdkReactNative', () => {
await DdSdkReactNative.addAttribute(key, value);

// THEN
expect(NativeDdSdk.addAttribute).toHaveBeenCalledTimes(1);
expect(NativeDdSdk.addAttribute).toHaveBeenCalledWith(key, {
expect(NativeDdSdkSpec.addAttribute).toHaveBeenCalledTimes(1);
expect(NativeDdSdkSpec.addAttribute).toHaveBeenCalledWith(key, {
value
});
expect(AttributesSingleton.getInstance().getAttribute(key)).toEqual(
Expand All @@ -1205,8 +1206,8 @@ describe('DdSdkReactNative', () => {
await DdSdkReactNative.removeAttribute(key);

// THEN
expect(NativeDdSdk.removeAttribute).toHaveBeenCalledTimes(1);
expect(NativeDdSdk.removeAttribute).toHaveBeenCalledWith(key);
expect(NativeDdSdkSpec.removeAttribute).toHaveBeenCalledTimes(1);
expect(NativeDdSdkSpec.removeAttribute).toHaveBeenCalledWith(key);
expect(AttributesSingleton.getInstance().getAttribute(key)).toEqual(
undefined
);
Expand All @@ -1223,8 +1224,10 @@ describe('DdSdkReactNative', () => {
await DdSdkReactNative.addAttributes(attributes);

// THEN
expect(NativeDdSdk.addAttributes).toHaveBeenCalledTimes(1);
expect(NativeDdSdk.addAttributes).toHaveBeenCalledWith(attributes);
expect(NativeDdSdkSpec.addAttributes).toHaveBeenCalledTimes(1);
expect(NativeDdSdkSpec.addAttributes).toHaveBeenCalledWith(
attributes
);
expect(AttributesSingleton.getInstance().getAttributes()).toEqual({
foo: 'bar'
});
Expand All @@ -1241,8 +1244,8 @@ describe('DdSdkReactNative', () => {
await DdSdkReactNative.removeAttributes(['foo', 'baz']);

// THEN
expect(NativeDdSdk.removeAttributes).toHaveBeenCalledTimes(1);
expect(NativeDdSdk.removeAttributes).toHaveBeenCalledWith([
expect(NativeDdSdkSpec.removeAttributes).toHaveBeenCalledTimes(1);
expect(NativeDdSdkSpec.removeAttributes).toHaveBeenCalledWith([
'foo',
'baz'
]);
Expand All @@ -1268,8 +1271,8 @@ describe('DdSdkReactNative', () => {
await DdSdkReactNative.setUserInfo(userInfo);

// THEN
expect(NativeDdSdk.setUserInfo).toHaveBeenCalledTimes(1);
expect(NativeDdSdk.setUserInfo).toHaveBeenCalledWith(userInfo);
expect(NativeDdSdkSpec.setUserInfo).toHaveBeenCalledTimes(1);
expect(NativeDdSdkSpec.setUserInfo).toHaveBeenCalledWith(userInfo);
expect(UserInfoSingleton.getInstance().getUserInfo()).toEqual(
userInfo
);
Expand All @@ -1289,8 +1292,8 @@ describe('DdSdkReactNative', () => {
await DdSdkReactNative.addUserExtraInfo(extraInfo);

// THEN
expect(NativeDdSdk.addUserExtraInfo).toHaveBeenCalledTimes(1);
expect(NativeDdSdk.addUserExtraInfo).toHaveBeenCalledWith(
expect(NativeDdSdkSpec.addUserExtraInfo).toHaveBeenCalledTimes(1);
expect(NativeDdSdkSpec.addUserExtraInfo).toHaveBeenCalledWith(
extraInfo
);
expect(UserInfoSingleton.getInstance().getUserInfo()).toEqual({
Expand All @@ -1310,7 +1313,7 @@ describe('DdSdkReactNative', () => {
await DdSdkReactNative.addUserExtraInfo(extraInfo);

// THEN
expect(NativeDdSdk.addUserExtraInfo).toHaveBeenCalledWith(
expect(NativeDdSdkSpec.addUserExtraInfo).toHaveBeenCalledWith(
extraInfo
);
expect(UserInfoSingleton.getInstance().getUserInfo()).toEqual({
Expand Down Expand Up @@ -1339,8 +1342,8 @@ describe('DdSdkReactNative', () => {
await DdSdkReactNative.clearUserInfo();

// THEN
expect(NativeDdSdk.clearUserInfo).toHaveBeenCalledTimes(1);
expect(NativeDdSdk.setUserInfo).toHaveBeenCalled();
expect(NativeDdSdkSpec.clearUserInfo).toHaveBeenCalledTimes(1);
expect(NativeDdSdkSpec.setUserInfo).toHaveBeenCalled();
expect(
UserInfoSingleton.getInstance().getUserInfo()
).toBeUndefined();
Expand All @@ -1357,8 +1360,8 @@ describe('DdSdkReactNative', () => {
DdSdkReactNative.setTrackingConsent(consent);

// THEN
expect(NativeDdSdk.setTrackingConsent).toHaveBeenCalledTimes(1);
expect(NativeDdSdk.setTrackingConsent).toHaveBeenCalledWith(
expect(NativeDdSdkSpec.setTrackingConsent).toHaveBeenCalledTimes(1);
expect(NativeDdSdkSpec.setTrackingConsent).toHaveBeenCalledWith(
consent
);
});
Expand All @@ -1370,7 +1373,7 @@ describe('DdSdkReactNative', () => {
DdSdkReactNative.clearAllData();

// THEN
expect(NativeDdSdk.clearAllData).toHaveBeenCalledTimes(1);
expect(NativeDdSdkSpec.clearAllData).toHaveBeenCalledTimes(1);
});
});

Expand Down Expand Up @@ -1500,4 +1503,121 @@ describe('DdSdkReactNative', () => {
});
}
);
describe('routing through DdSdkWrapper', () => {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new set of tests make sure that the DdSdkWrapper exported from DdsdkInternal is actually being called and not skipped.

it('DdSdkReactNative.addAttribute reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'addAttribute');

await DdSdkReactNative.addAttribute('key', 'value');

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.removeAttribute reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'removeAttribute');

await DdSdkReactNative.removeAttribute('key');

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.addAttributes reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'addAttributes');

await DdSdkReactNative.addAttributes({ key: 'value' });

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.removeAttributes reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'removeAttributes');

await DdSdkReactNative.removeAttributes(['key']);

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.setUserInfo reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'setUserInfo');

await DdSdkReactNative.setUserInfo({ id: 'user-id' });

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.clearUserInfo reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'clearUserInfo');

await DdSdkReactNative.clearUserInfo();

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.addUserExtraInfo reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'addUserExtraInfo');

await DdSdkReactNative.addUserExtraInfo({ role: 'admin' });

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.setAccountInfo reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'setAccountInfo');

await DdSdkReactNative.setAccountInfo({ id: 'account-id' });

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.clearAccountInfo reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'clearAccountInfo');

await DdSdkReactNative.clearAccountInfo();

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.addAccountExtraInfo reaches the native spec via the wrapper', async () => {
// addAccountExtraInfo bails out early unless an account ID is set.
await DdSdkReactNative.setAccountInfo({ id: 'account-id' });
const spy = jest.spyOn(
DdSdkWrapper.prototype,
'addAccountExtraInfo'
);

await DdSdkReactNative.addAccountExtraInfo({ tier: 'premium' });

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.setTrackingConsent reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(
DdSdkWrapper.prototype,
'setTrackingConsent'
);

await DdSdkReactNative.setTrackingConsent(TrackingConsent.GRANTED);

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

it('DdSdkReactNative.clearAllData reaches the native spec via the wrapper', async () => {
const spy = jest.spyOn(DdSdkWrapper.prototype, 'clearAllData');

await DdSdkReactNative.clearAllData();

expect(spy).toHaveBeenCalled();
spy.mockRestore();
});
});
});
Loading
Loading