@datadog/mobile-react-native 3.0.3 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DatadogSDKReactNative.podspec +10 -9
- package/android/build.gradle +10 -8
- package/android/src/main/kotlin/com/datadog/reactnative/DdFlagsImplementation.kt +237 -0
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkBridgeExt.kt +262 -78
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +3 -3
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfigurationExt.kt +18 -18
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkNativeInitialization.kt +3 -2
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkReactNativePackage.kt +3 -1
- package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
- package/android/src/newarch/kotlin/com/datadog/reactnative/DdFlags.kt +66 -0
- package/android/src/oldarch/kotlin/com/datadog/reactnative/DdFlags.kt +63 -0
- package/android/src/test/kotlin/com/datadog/reactnative/DdSdkBridgeExtTest.kt +242 -0
- package/android/src/test/kotlin/com/datadog/reactnative/DdSdkNativeInitializationTest.kt +4 -2
- package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +6 -3
- package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +3 -4
- package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +38 -0
- package/android/src/test/kotlin/com/datadog/tools/unit/MockRumMonitor.kt +3 -0
- package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +1 -1
- package/android/src/test/resources/input/complete-configuration.json +10 -10
- package/ios/Sources/DatadogSDKReactNative.h +2 -0
- package/ios/Sources/DdFlags.h +24 -0
- package/ios/Sources/DdFlags.mm +83 -0
- package/ios/Sources/DdFlagsImplementation.swift +263 -0
- package/ios/Sources/DdSdkConfiguration.swift +4 -4
- package/ios/Sources/DdSdkNativeInitialization.swift +2 -2
- package/ios/Sources/RNDdSdkConfiguration.swift +11 -9
- package/ios/Sources/SdkVersion.swift +1 -1
- package/lib/commonjs/InternalLog.js +9 -7
- package/lib/commonjs/InternalLog.js.map +1 -1
- package/lib/commonjs/flags/DdFlags.js +51 -0
- package/lib/commonjs/flags/DdFlags.js.map +1 -0
- package/lib/commonjs/flags/FlagsClient.js +218 -0
- package/lib/commonjs/flags/FlagsClient.js.map +1 -0
- package/lib/commonjs/flags/internal.js +40 -0
- package/lib/commonjs/flags/internal.js.map +1 -0
- package/lib/commonjs/flags/types.js +6 -0
- package/lib/commonjs/flags/types.js.map +1 -0
- package/lib/commonjs/index.js +7 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/sdk/DatadogProvider/Buffer/BufferSingleton.js +10 -8
- package/lib/commonjs/sdk/DatadogProvider/Buffer/BufferSingleton.js.map +1 -1
- package/lib/commonjs/specs/NativeDdFlags.js +19 -0
- package/lib/commonjs/specs/NativeDdFlags.js.map +1 -0
- package/lib/commonjs/version.js +1 -1
- package/lib/module/InternalLog.js +9 -6
- package/lib/module/InternalLog.js.map +1 -1
- package/lib/module/flags/DdFlags.js +45 -0
- package/lib/module/flags/DdFlags.js.map +1 -0
- package/lib/module/flags/FlagsClient.js +211 -0
- package/lib/module/flags/FlagsClient.js.map +1 -0
- package/lib/module/flags/internal.js +33 -0
- package/lib/module/flags/internal.js.map +1 -0
- package/lib/module/flags/types.js +2 -0
- package/lib/module/flags/types.js.map +1 -0
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/sdk/DatadogProvider/Buffer/BufferSingleton.js +10 -7
- package/lib/module/sdk/DatadogProvider/Buffer/BufferSingleton.js.map +1 -1
- package/lib/module/specs/NativeDdFlags.js +17 -0
- package/lib/module/specs/NativeDdFlags.js.map +1 -0
- package/lib/module/version.js +1 -1
- package/lib/typescript/InternalLog.d.ts +6 -4
- package/lib/typescript/InternalLog.d.ts.map +1 -1
- package/lib/typescript/flags/DdFlags.d.ts +3 -0
- package/lib/typescript/flags/DdFlags.d.ts.map +1 -0
- package/lib/typescript/flags/FlagsClient.d.ts +114 -0
- package/lib/typescript/flags/FlagsClient.d.ts.map +1 -0
- package/lib/typescript/flags/internal.d.ts +14 -0
- package/lib/typescript/flags/internal.d.ts.map +1 -0
- package/lib/typescript/flags/types.d.ts +202 -0
- package/lib/typescript/flags/types.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/nativeModulesTypes.d.ts +5 -0
- package/lib/typescript/nativeModulesTypes.d.ts.map +1 -1
- package/lib/typescript/sdk/DatadogProvider/Buffer/BufferSingleton.d.ts +7 -5
- package/lib/typescript/sdk/DatadogProvider/Buffer/BufferSingleton.d.ts.map +1 -1
- package/lib/typescript/specs/NativeDdFlags.d.ts +15 -0
- package/lib/typescript/specs/NativeDdFlags.d.ts.map +1 -0
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/InternalLog.tsx +14 -6
- package/src/flags/DdFlags.ts +59 -0
- package/src/flags/FlagsClient.ts +266 -0
- package/src/flags/__tests__/DdFlags.test.ts +65 -0
- package/src/flags/__tests__/FlagsClient.test.ts +320 -0
- package/src/flags/internal.ts +63 -0
- package/src/flags/types.ts +215 -0
- package/src/index.tsx +15 -1
- package/src/nativeModulesTypes.ts +6 -0
- package/src/sdk/DatadogProvider/Buffer/BufferSingleton.ts +16 -7
- package/src/specs/NativeDdFlags.ts +35 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { InternalLog } from '../InternalLog';
|
|
8
|
+
import { SdkVerbosity } from '../config/types/SdkVerbosity';
|
|
9
|
+
import type { DdNativeFlagsType } from '../nativeModulesTypes';
|
|
10
|
+
|
|
11
|
+
import { processEvaluationContext } from './internal';
|
|
12
|
+
import type { FlagCacheEntry } from './internal';
|
|
13
|
+
import type { JsonValue, EvaluationContext, FlagDetails } from './types';
|
|
14
|
+
|
|
15
|
+
export class FlagsClient {
|
|
16
|
+
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
|
|
17
|
+
private nativeFlags: DdNativeFlagsType = require('../specs/NativeDdFlags')
|
|
18
|
+
.default;
|
|
19
|
+
|
|
20
|
+
private clientName: string;
|
|
21
|
+
|
|
22
|
+
private evaluationContext: EvaluationContext | undefined = undefined;
|
|
23
|
+
|
|
24
|
+
private flagsCache: Record<string, FlagCacheEntry> = {};
|
|
25
|
+
|
|
26
|
+
constructor(clientName: string = 'default') {
|
|
27
|
+
this.clientName = clientName;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Sets the evaluation context for the client.
|
|
32
|
+
*
|
|
33
|
+
* Should be called before evaluating any flags. Otherwise, the client will fall back to serving default flag values.
|
|
34
|
+
*
|
|
35
|
+
* Throws an error if there is an error setting the evaluation context and logs an error message.
|
|
36
|
+
*
|
|
37
|
+
* @param context The evaluation context to associate with the current client.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const flagsClient = DdFlags.getClient();
|
|
42
|
+
*
|
|
43
|
+
* await flagsClient.setEvaluationContext({
|
|
44
|
+
* targetingKey: 'user-123',
|
|
45
|
+
* attributes: {
|
|
46
|
+
* favoriteFruit: 'apple'
|
|
47
|
+
* }
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* const flagValue = flagsClient.getBooleanValue('new-feature', false);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
setEvaluationContext = async (
|
|
54
|
+
context: EvaluationContext
|
|
55
|
+
): Promise<void> => {
|
|
56
|
+
// Make sure to process the incoming context because we don't support nested object values in context.
|
|
57
|
+
const processedContext = processEvaluationContext(context);
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const result = await this.nativeFlags.setEvaluationContext(
|
|
61
|
+
this.clientName,
|
|
62
|
+
processedContext.targetingKey,
|
|
63
|
+
processedContext.attributes ?? {}
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
this.evaluationContext = processedContext;
|
|
67
|
+
this.flagsCache = result;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (error instanceof Error) {
|
|
70
|
+
InternalLog.log(
|
|
71
|
+
`Error setting flag evaluation context: ${error.message}`,
|
|
72
|
+
SdkVerbosity.WARN
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
private track = (flag: FlagCacheEntry, context: EvaluationContext) => {
|
|
81
|
+
// A non-blocking call; don't await this.
|
|
82
|
+
this.nativeFlags
|
|
83
|
+
.trackEvaluation(
|
|
84
|
+
this.clientName,
|
|
85
|
+
flag.key,
|
|
86
|
+
flag,
|
|
87
|
+
context.targetingKey,
|
|
88
|
+
context.attributes ?? {}
|
|
89
|
+
)
|
|
90
|
+
.catch(error => {
|
|
91
|
+
if (error instanceof Error) {
|
|
92
|
+
InternalLog.log(
|
|
93
|
+
`Error tracking flag evaluation: ${error.message}`,
|
|
94
|
+
SdkVerbosity.WARN
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
private getDetails = <T>(
|
|
101
|
+
key: string,
|
|
102
|
+
defaultValue: T,
|
|
103
|
+
type: 'boolean' | 'string' | 'number' | 'object'
|
|
104
|
+
): FlagDetails<T> => {
|
|
105
|
+
if (!this.evaluationContext) {
|
|
106
|
+
return {
|
|
107
|
+
key,
|
|
108
|
+
value: defaultValue,
|
|
109
|
+
reason: 'ERROR',
|
|
110
|
+
errorCode: 'PROVIDER_NOT_READY',
|
|
111
|
+
errorMessage: `The evaluation context is not set for '${this.clientName}'. Please, set context before evaluating any flags.`
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Retrieve the flag from the cache.
|
|
116
|
+
const flag = this.flagsCache[key];
|
|
117
|
+
|
|
118
|
+
if (!flag) {
|
|
119
|
+
return {
|
|
120
|
+
key,
|
|
121
|
+
value: defaultValue,
|
|
122
|
+
reason: 'ERROR',
|
|
123
|
+
errorCode: 'FLAG_NOT_FOUND'
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Validate the expected type against the actual flag value type.
|
|
128
|
+
const actualType = typeof flag.value;
|
|
129
|
+
if (actualType !== type) {
|
|
130
|
+
return {
|
|
131
|
+
key,
|
|
132
|
+
value: defaultValue,
|
|
133
|
+
reason: 'ERROR',
|
|
134
|
+
errorCode: 'TYPE_MISMATCH',
|
|
135
|
+
errorMessage: `Flag "${key}" returned a value of type "${typeof flag.value}". Use the corresponding method instead of the one expecting "${type}".`
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
this.track(flag, this.evaluationContext);
|
|
140
|
+
|
|
141
|
+
const details: FlagDetails<T> = {
|
|
142
|
+
key: flag.key,
|
|
143
|
+
value: flag.value as T,
|
|
144
|
+
variant: flag.variationKey,
|
|
145
|
+
allocationKey: flag.allocationKey,
|
|
146
|
+
reason: flag.reason
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return details;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Evaluate a boolean feature flag with detailed evaluation information.
|
|
154
|
+
*
|
|
155
|
+
* @param key The key of the flag to evaluate.
|
|
156
|
+
* @param defaultValue Fallback value for when flag evaluation fails, flag is not found, or the client does not have an evaluation context set.
|
|
157
|
+
*/
|
|
158
|
+
getBooleanDetails = (
|
|
159
|
+
key: string,
|
|
160
|
+
defaultValue: boolean
|
|
161
|
+
): FlagDetails<boolean> => {
|
|
162
|
+
return this.getDetails(key, defaultValue, 'boolean');
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Evaluate a string feature flag with detailed evaluation information.
|
|
167
|
+
*
|
|
168
|
+
* @param key The key of the flag to evaluate.
|
|
169
|
+
* @param defaultValue Fallback value for when flag evaluation fails, flag is not found, or the client does not have an evaluation context set.
|
|
170
|
+
*/
|
|
171
|
+
getStringDetails = (
|
|
172
|
+
key: string,
|
|
173
|
+
defaultValue: string
|
|
174
|
+
): FlagDetails<string> => {
|
|
175
|
+
return this.getDetails(key, defaultValue, 'string');
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Evaluate a number feature flag with detailed evaluation information.
|
|
180
|
+
*
|
|
181
|
+
* @param key The key of the flag to evaluate.
|
|
182
|
+
* @param defaultValue Fallback value for when flag evaluation fails, flag is not found, or the client does not have an evaluation context set.
|
|
183
|
+
*/
|
|
184
|
+
getNumberDetails = (
|
|
185
|
+
key: string,
|
|
186
|
+
defaultValue: number
|
|
187
|
+
): FlagDetails<number> => {
|
|
188
|
+
return this.getDetails(key, defaultValue, 'number');
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Evaluate a JSON feature flag with detailed evaluation information.
|
|
193
|
+
*
|
|
194
|
+
* Even though the `defaultValue` is typed as `JsonValue`, the flag value should be a valid JSON object.
|
|
195
|
+
* Please use other typed methods to evaluate flags with primitive values.
|
|
196
|
+
*
|
|
197
|
+
* @param key The key of the flag to evaluate.
|
|
198
|
+
* @param defaultValue Fallback value for when flag evaluation fails, flag is not found, or the client does not have an evaluation context set.
|
|
199
|
+
*/
|
|
200
|
+
getObjectDetails = <T extends JsonValue>(
|
|
201
|
+
key: string,
|
|
202
|
+
defaultValue: T
|
|
203
|
+
): FlagDetails<T> => {
|
|
204
|
+
return this.getDetails(key, defaultValue, 'object');
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Evaluate a boolean feature flag value.
|
|
209
|
+
*
|
|
210
|
+
* @param key The key of the flag to evaluate.
|
|
211
|
+
* @param defaultValue Fallback value for when flag evaluation fails, flag is not found, or the client does not have an evaluation context set.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```ts
|
|
215
|
+
* const isNewFeatureEnabled = flagsClient.getBooleanValue('new-feature-enabled', false);
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
getBooleanValue = (key: string, defaultValue: boolean): boolean => {
|
|
219
|
+
return this.getBooleanDetails(key, defaultValue).value;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Evaluate a string feature flag value.
|
|
224
|
+
*
|
|
225
|
+
* @param key The key of the flag to evaluate.
|
|
226
|
+
* @param defaultValue Fallback value for when flag evaluation fails, flag is not found, or the client does not have an evaluation context set.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```ts
|
|
230
|
+
* const appTheme = flagsClient.getStringValue('app-theme', 'light');
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
getStringValue = (key: string, defaultValue: string): string => {
|
|
234
|
+
return this.getStringDetails(key, defaultValue).value;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Evaluate a number feature flag value.
|
|
239
|
+
*
|
|
240
|
+
* @param key The key of the flag to evaluate.
|
|
241
|
+
* @param defaultValue Fallback value for when flag evaluation fails, flag is not found, or the client does not have an evaluation context set.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* const ctaButtonSize = flagsClient.getNumberValue('cta-button-size', 16);
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
getNumberValue = (key: string, defaultValue: number): number => {
|
|
249
|
+
return this.getNumberDetails(key, defaultValue).value;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Evaluate an object feature flag value.
|
|
254
|
+
*
|
|
255
|
+
* @param key The key of the flag to evaluate.
|
|
256
|
+
* @param defaultValue Fallback value for when flag evaluation fails, flag is not found, or the client does not have an evaluation context set.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* const pageCalloutOptions = flagsClient.getObjectValue('page-callout', { color: 'purple', text: 'Woof!' });
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
getObjectValue = <T extends JsonValue>(key: string, defaultValue: T): T => {
|
|
264
|
+
return this.getObjectDetails(key, defaultValue).value;
|
|
265
|
+
};
|
|
266
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { NativeModules } from 'react-native';
|
|
8
|
+
|
|
9
|
+
import { InternalLog } from '../../InternalLog';
|
|
10
|
+
import { SdkVerbosity } from '../../config/types/SdkVerbosity';
|
|
11
|
+
import { DdFlags } from '../DdFlags';
|
|
12
|
+
|
|
13
|
+
jest.mock('../../InternalLog', () => {
|
|
14
|
+
return {
|
|
15
|
+
InternalLog: {
|
|
16
|
+
log: jest.fn()
|
|
17
|
+
},
|
|
18
|
+
DATADOG_MESSAGE_PREFIX: 'DATADOG:'
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('DdFlags', () => {
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
jest.clearAllMocks();
|
|
25
|
+
// Reset state of DdFlags instance.
|
|
26
|
+
Object.assign(DdFlags, {
|
|
27
|
+
isFeatureEnabled: false,
|
|
28
|
+
clients: {}
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should always call the native enable method with enabled set to true', async () => {
|
|
33
|
+
await DdFlags.enable();
|
|
34
|
+
|
|
35
|
+
expect(NativeModules.DdFlags.enable).toHaveBeenCalledWith({
|
|
36
|
+
enabled: true
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should call the native enable method with the correct configuration', async () => {
|
|
41
|
+
await DdFlags.enable({
|
|
42
|
+
customExposureEndpoint: 'https://example.com',
|
|
43
|
+
customFlagsEndpoint: 'https://example.com',
|
|
44
|
+
trackExposures: false,
|
|
45
|
+
rumIntegrationEnabled: false
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(NativeModules.DdFlags.enable).toHaveBeenCalledWith({
|
|
49
|
+
enabled: true,
|
|
50
|
+
customExposureEndpoint: 'https://example.com',
|
|
51
|
+
customFlagsEndpoint: 'https://example.com',
|
|
52
|
+
trackExposures: false,
|
|
53
|
+
rumIntegrationEnabled: false
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should print an error when trying to retrieve a client before DdFlags.enable() has been called', async () => {
|
|
58
|
+
DdFlags.getClient();
|
|
59
|
+
|
|
60
|
+
expect(InternalLog.log).toHaveBeenCalledWith(
|
|
61
|
+
'`DdFlags.getClient()` called before Datadog Flags feature have been enabled. Client will fall back to serving default flag values.',
|
|
62
|
+
SdkVerbosity.ERROR
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
|
|
3
|
+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
4
|
+
* Copyright 2016-Present Datadog, Inc.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { NativeModules } from 'react-native';
|
|
8
|
+
|
|
9
|
+
import { InternalLog } from '../../InternalLog';
|
|
10
|
+
import { SdkVerbosity } from '../../config/types/SdkVerbosity';
|
|
11
|
+
import { DdFlags } from '../DdFlags';
|
|
12
|
+
|
|
13
|
+
jest.spyOn(NativeModules.DdFlags, 'setEvaluationContext').mockResolvedValue({
|
|
14
|
+
'test-boolean-flag': {
|
|
15
|
+
key: 'test-boolean-flag',
|
|
16
|
+
value: true,
|
|
17
|
+
allocationKey: '',
|
|
18
|
+
variationKey: 'true',
|
|
19
|
+
reason: 'STATIC',
|
|
20
|
+
doLog: true,
|
|
21
|
+
// Internal fields for Android.
|
|
22
|
+
variationType: '',
|
|
23
|
+
variationValue: '',
|
|
24
|
+
extraLogging: {}
|
|
25
|
+
},
|
|
26
|
+
'test-string-flag': {
|
|
27
|
+
key: 'test-string-flag',
|
|
28
|
+
value: 'hello world',
|
|
29
|
+
allocationKey: '',
|
|
30
|
+
variationKey: 'Hello World',
|
|
31
|
+
reason: 'STATIC',
|
|
32
|
+
doLog: true,
|
|
33
|
+
// Internal fields for Android.
|
|
34
|
+
variationType: '',
|
|
35
|
+
variationValue: '',
|
|
36
|
+
extraLogging: {}
|
|
37
|
+
},
|
|
38
|
+
'test-number-flag': {
|
|
39
|
+
key: 'test-number-flag',
|
|
40
|
+
value: 42,
|
|
41
|
+
allocationKey: '',
|
|
42
|
+
variationKey: '42',
|
|
43
|
+
reason: 'STATIC',
|
|
44
|
+
doLog: true,
|
|
45
|
+
// Internal fields for Android.
|
|
46
|
+
variationType: '',
|
|
47
|
+
variationValue: '',
|
|
48
|
+
extraLogging: {}
|
|
49
|
+
},
|
|
50
|
+
'test-object-flag': {
|
|
51
|
+
key: 'test-object-flag',
|
|
52
|
+
value: { greeting: 'Greeting from the native side!' },
|
|
53
|
+
allocationKey: '',
|
|
54
|
+
variationKey: 'Native Greeting',
|
|
55
|
+
reason: 'STATIC',
|
|
56
|
+
doLog: true,
|
|
57
|
+
// Internal fields for Android.
|
|
58
|
+
variationType: '',
|
|
59
|
+
variationValue: '',
|
|
60
|
+
extraLogging: {}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
jest.mock('../../InternalLog', () => {
|
|
65
|
+
return {
|
|
66
|
+
InternalLog: { log: jest.fn() },
|
|
67
|
+
DATADOG_MESSAGE_PREFIX: 'DATADOG:'
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('FlagsClient', () => {
|
|
72
|
+
beforeEach(async () => {
|
|
73
|
+
jest.clearAllMocks();
|
|
74
|
+
|
|
75
|
+
// Reset state of the global DdFlags instance.
|
|
76
|
+
Object.assign(DdFlags, {
|
|
77
|
+
isFeatureEnabled: false,
|
|
78
|
+
clients: {}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
await DdFlags.enable();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('setEvaluationContext', () => {
|
|
85
|
+
it('should set the evaluation context', async () => {
|
|
86
|
+
const flagsClient = DdFlags.getClient();
|
|
87
|
+
await flagsClient.setEvaluationContext({
|
|
88
|
+
targetingKey: 'test-user-1',
|
|
89
|
+
attributes: { country: 'US' }
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
expect(
|
|
93
|
+
NativeModules.DdFlags.setEvaluationContext
|
|
94
|
+
).toHaveBeenCalledWith('default', 'test-user-1', { country: 'US' });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should throw an error if there is an error setting the evaluation context', async () => {
|
|
98
|
+
NativeModules.DdFlags.setEvaluationContext.mockRejectedValueOnce(
|
|
99
|
+
new Error(
|
|
100
|
+
"A network error occurred while fetching feature flags for client 'default'."
|
|
101
|
+
)
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const flagsClient = DdFlags.getClient();
|
|
105
|
+
|
|
106
|
+
await expect(
|
|
107
|
+
flagsClient.setEvaluationContext({
|
|
108
|
+
targetingKey: 'test-user-1',
|
|
109
|
+
attributes: { country: 'US' }
|
|
110
|
+
})
|
|
111
|
+
).rejects.toThrow(
|
|
112
|
+
"A network error occurred while fetching feature flags for client 'default'."
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
expect(InternalLog.log).toHaveBeenCalledWith(
|
|
116
|
+
"Error setting flag evaluation context: A network error occurred while fetching feature flags for client 'default'.",
|
|
117
|
+
SdkVerbosity.WARN
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('getDetails', () => {
|
|
123
|
+
it('should succesfully return flag details for flags', async () => {
|
|
124
|
+
// Flag values are mocked in the __mocks__/react-native.ts file.
|
|
125
|
+
const flagsClient = DdFlags.getClient();
|
|
126
|
+
await flagsClient.setEvaluationContext({
|
|
127
|
+
targetingKey: 'test-user-1',
|
|
128
|
+
attributes: { country: 'US' }
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const booleanDetails = flagsClient.getBooleanDetails(
|
|
132
|
+
'test-boolean-flag',
|
|
133
|
+
false
|
|
134
|
+
);
|
|
135
|
+
const stringDetails = flagsClient.getStringDetails(
|
|
136
|
+
'test-string-flag',
|
|
137
|
+
'Default value'
|
|
138
|
+
);
|
|
139
|
+
const numberDetails = flagsClient.getNumberDetails(
|
|
140
|
+
'test-number-flag',
|
|
141
|
+
-2
|
|
142
|
+
);
|
|
143
|
+
const objectDetails = flagsClient.getObjectDetails(
|
|
144
|
+
'test-object-flag',
|
|
145
|
+
{ greeting: 'Default value' }
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
expect(booleanDetails).toMatchObject({
|
|
149
|
+
value: true,
|
|
150
|
+
variant: 'true',
|
|
151
|
+
reason: 'STATIC'
|
|
152
|
+
});
|
|
153
|
+
expect(stringDetails).toMatchObject({
|
|
154
|
+
value: 'hello world',
|
|
155
|
+
variant: 'Hello World',
|
|
156
|
+
reason: 'STATIC'
|
|
157
|
+
});
|
|
158
|
+
expect(numberDetails).toMatchObject({
|
|
159
|
+
value: 42,
|
|
160
|
+
variant: '42',
|
|
161
|
+
reason: 'STATIC'
|
|
162
|
+
});
|
|
163
|
+
expect(objectDetails).toMatchObject({
|
|
164
|
+
value: { greeting: 'Greeting from the native side!' },
|
|
165
|
+
variant: 'Native Greeting',
|
|
166
|
+
reason: 'STATIC'
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should return PROVIDER_NOT_READY if evaluation context is not set', () => {
|
|
171
|
+
const flagsClient = DdFlags.getClient();
|
|
172
|
+
// Skip `setEvaluationContext` call here.
|
|
173
|
+
|
|
174
|
+
const details = flagsClient.getBooleanDetails(
|
|
175
|
+
'test-boolean-flag',
|
|
176
|
+
false
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
expect(details).toMatchObject({
|
|
180
|
+
value: false,
|
|
181
|
+
reason: 'ERROR',
|
|
182
|
+
errorCode: 'PROVIDER_NOT_READY',
|
|
183
|
+
errorMessage: expect.stringContaining(
|
|
184
|
+
'The evaluation context is not set'
|
|
185
|
+
)
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('should return FLAG_NOT_FOUND if flag is missing from context', async () => {
|
|
190
|
+
const flagsClient = DdFlags.getClient();
|
|
191
|
+
await flagsClient.setEvaluationContext({
|
|
192
|
+
targetingKey: 'test-user-1',
|
|
193
|
+
attributes: { country: 'US' }
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// 'unknown-flag' is not defined in the __mocks__/react-native.ts
|
|
197
|
+
const details = flagsClient.getBooleanDetails(
|
|
198
|
+
'unknown-flag',
|
|
199
|
+
false
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
expect(details).toMatchObject({
|
|
203
|
+
value: false,
|
|
204
|
+
reason: 'ERROR',
|
|
205
|
+
errorCode: 'FLAG_NOT_FOUND'
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('should return TYPE_MISMATCH when using wrong typed accessor method', async () => {
|
|
210
|
+
// Flag values are mocked in the __mocks__/react-native.ts file.
|
|
211
|
+
const flagsClient = DdFlags.getClient();
|
|
212
|
+
await flagsClient.setEvaluationContext({
|
|
213
|
+
targetingKey: 'test-user-1',
|
|
214
|
+
attributes: { country: 'US' }
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Call getStringDetails on a boolean flag.
|
|
218
|
+
const booleanFlagAsString = flagsClient.getStringDetails(
|
|
219
|
+
'test-boolean-flag',
|
|
220
|
+
'default'
|
|
221
|
+
);
|
|
222
|
+
// Call getBooleanDetails on a string flag.
|
|
223
|
+
const stringFlagAsBoolean = flagsClient.getBooleanDetails(
|
|
224
|
+
'test-string-flag',
|
|
225
|
+
false
|
|
226
|
+
);
|
|
227
|
+
// Call getStringDetails on a number flag.
|
|
228
|
+
const numberFlagAsString = flagsClient.getStringDetails(
|
|
229
|
+
'test-number-flag',
|
|
230
|
+
'default'
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
expect(booleanFlagAsString).toMatchObject({
|
|
234
|
+
key: 'test-boolean-flag',
|
|
235
|
+
value: 'default',
|
|
236
|
+
errorCode: 'TYPE_MISMATCH',
|
|
237
|
+
reason: 'ERROR',
|
|
238
|
+
errorMessage: expect.stringContaining('boolean')
|
|
239
|
+
});
|
|
240
|
+
expect(stringFlagAsBoolean).toMatchObject({
|
|
241
|
+
key: 'test-string-flag',
|
|
242
|
+
value: false,
|
|
243
|
+
errorCode: 'TYPE_MISMATCH',
|
|
244
|
+
reason: 'ERROR',
|
|
245
|
+
errorMessage: expect.stringContaining('string')
|
|
246
|
+
});
|
|
247
|
+
expect(numberFlagAsString).toMatchObject({
|
|
248
|
+
key: 'test-number-flag',
|
|
249
|
+
value: 'default',
|
|
250
|
+
errorCode: 'TYPE_MISMATCH',
|
|
251
|
+
reason: 'ERROR',
|
|
252
|
+
errorMessage: expect.stringContaining('number')
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
describe('getValue', () => {
|
|
258
|
+
it('should succesfully return flag values', async () => {
|
|
259
|
+
// Flag values are mocked in the __mocks__/react-native.ts file.
|
|
260
|
+
const flagsClient = DdFlags.getClient();
|
|
261
|
+
await flagsClient.setEvaluationContext({
|
|
262
|
+
targetingKey: 'test-user-1',
|
|
263
|
+
attributes: { country: 'US' }
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
const booleanValue = flagsClient.getBooleanValue(
|
|
267
|
+
'test-boolean-flag',
|
|
268
|
+
false
|
|
269
|
+
);
|
|
270
|
+
const stringValue = flagsClient.getStringValue(
|
|
271
|
+
'test-string-flag',
|
|
272
|
+
'Default value'
|
|
273
|
+
);
|
|
274
|
+
const numberValue = flagsClient.getNumberValue(
|
|
275
|
+
'test-number-flag',
|
|
276
|
+
-2
|
|
277
|
+
);
|
|
278
|
+
const objectValue = flagsClient.getObjectValue('test-object-flag', {
|
|
279
|
+
greeting: 'Default value'
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
expect(booleanValue).toBe(true);
|
|
283
|
+
expect(stringValue).toBe('hello world');
|
|
284
|
+
expect(numberValue).toBe(42);
|
|
285
|
+
expect(objectValue).toStrictEqual({
|
|
286
|
+
greeting: 'Greeting from the native side!'
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('should return the default value when using wrong typed accessor method', async () => {
|
|
291
|
+
// Flag values are mocked in the __mocks__/react-native.ts file.
|
|
292
|
+
const flagsClient = DdFlags.getClient();
|
|
293
|
+
await flagsClient.setEvaluationContext({
|
|
294
|
+
targetingKey: 'test-user-1',
|
|
295
|
+
attributes: { country: 'US' }
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// Call getStringValue on a boolean flag.
|
|
299
|
+
const booleanFlagAsString = flagsClient.getStringValue(
|
|
300
|
+
'test-boolean-flag',
|
|
301
|
+
'default'
|
|
302
|
+
);
|
|
303
|
+
// Call getBooleanValue on a string flag.
|
|
304
|
+
const stringFlagAsBoolean = flagsClient.getBooleanValue(
|
|
305
|
+
'test-string-flag',
|
|
306
|
+
false
|
|
307
|
+
);
|
|
308
|
+
// Call getStringValue on a number flag.
|
|
309
|
+
const numberFlagAsString = flagsClient.getStringValue(
|
|
310
|
+
'test-number-flag',
|
|
311
|
+
'default'
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
// The default value is returned due to TYPE_MISMATCH.
|
|
315
|
+
expect(booleanFlagAsString).toBe('default');
|
|
316
|
+
expect(stringFlagAsBoolean).toBe(false);
|
|
317
|
+
expect(numberFlagAsString).toBe('default');
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
});
|