Skip to content

Commit f034de4

Browse files
fix: removed a check that returns false on empty key. (#717)
* removed a check that returns false on empty key.
1 parent 247e91c commit f034de4

File tree

7 files changed

+21
-22
lines changed

7 files changed

+21
-22
lines changed

packages/optimizely-sdk/lib/core/event_builder/build_event_v1.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ function makeConversionSnapshot(conversion: ConversionEvent): Snapshot {
159159
function makeDecisionSnapshot(event: ImpressionEvent): Snapshot {
160160
const { layer, experiment, variation, ruleKey, flagKey, ruleType, enabled } = event
161161
const layerId = layer ? layer.id : null
162-
const experimentId = experiment ? experiment.id : null
163-
const variationId = variation ? variation.id : null
162+
const experimentId = experiment?.id ?? ''
163+
const variationId = variation?.id ?? ''
164164
const variationKey = variation ? variation.key : ''
165165

166166
return {

packages/optimizely-sdk/lib/optimizely/index.tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6717,8 +6717,8 @@ describe('lib/optimizely', function() {
67176717
decisions: [
67186718
{
67196719
campaign_id: null,
6720-
experiment_id: null,
6721-
variation_id: null,
6720+
experiment_id: '',
6721+
variation_id: '',
67226722
metadata: {
67236723
flag_key: 'test_feature',
67246724
rule_key: '',

packages/optimizely-sdk/lib/optimizely_user_context/index.tests.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { createNotificationCenter } from '../core/notification_center';
2626
import Optimizely from '../optimizely';
2727
import errorHandler from '../plugins/error_handler';
2828
import eventDispatcher from '../plugins/event_dispatcher/index.node';
29-
import { DECISION_MESSAGES, LOG_LEVEL, LOG_MESSAGES } from '../utils/enums';
29+
import { CONTROL_ATTRIBUTES, DECISION_MESSAGES, LOG_LEVEL, LOG_MESSAGES } from '../utils/enums';
3030
import testData from '../tests/test_data';
3131
import { OptimizelyDecideOption } from '../shared_types';
3232

@@ -328,7 +328,7 @@ describe('lib/optimizely_user_context', function() {
328328
assert.strictEqual(stubLogHandler.log.args[0][1], DECISION_MESSAGES.SDK_NOT_READY);
329329
});
330330

331-
it('should return false when provided empty string flagKey', function() {
331+
it('should return true when provided empty string flagKey', function() {
332332
fakeOptimizely = {
333333
isValidInstance: sinon.stub().returns(true)
334334
};
@@ -337,7 +337,7 @@ describe('lib/optimizely_user_context', function() {
337337
userId: 'user123',
338338
});
339339
var result = user.setForcedDecision({ flagKey: '' }, '3324490562');
340-
assert.strictEqual(result, false);
340+
assert.strictEqual(result, true);
341341
sinon.assert.notCalled(stubLogHandler.log);
342342
});
343343

@@ -400,7 +400,7 @@ describe('lib/optimizely_user_context', function() {
400400
assert.equal(decision.userContext.getUserId(), userId);
401401
assert.deepEqual(decision.userContext.getAttributes(), {});
402402
assert.deepEqual(Object.keys(decision.userContext.forcedDecisionsMap).length, 1);
403-
assert.deepEqual(decision.userContext.forcedDecisionsMap[featureKey]['$null-rule-key'], { variationKey });
403+
assert.deepEqual(decision.userContext.forcedDecisionsMap[featureKey][CONTROL_ATTRIBUTES.FORCED_DECISION_NULL_RULE_KEY], { variationKey });
404404
assert.equal(
405405
true,
406406
decision.reasons.includes(
@@ -429,7 +429,7 @@ describe('lib/optimizely_user_context', function() {
429429
assert.equal(decision.userContext.getUserId(), userId);
430430
assert.deepEqual(decision.userContext.getAttributes(), {});
431431
assert.deepEqual(Object.values(decision.userContext.forcedDecisionsMap).length, 1);
432-
assert.deepEqual(decision.userContext.forcedDecisionsMap[featureKey]['$null-rule-key'], { variationKey });
432+
assert.deepEqual(decision.userContext.forcedDecisionsMap[featureKey][CONTROL_ATTRIBUTES.FORCED_DECISION_NULL_RULE_KEY], { variationKey });
433433
assert.equal(
434434
true,
435435
decision.reasons.includes(
@@ -448,7 +448,7 @@ describe('lib/optimizely_user_context', function() {
448448
var eventDecision = impressionEvent.params.visitors[0].snapshots[0].decisions[0];
449449
var metadata = eventDecision.metadata;
450450

451-
assert.equal(eventDecision.experiment_id, null);
451+
assert.equal(eventDecision.experiment_id, '');
452452
assert.equal(eventDecision.variation_id, '3324490562');
453453

454454
assert.equal(metadata.flag_key, featureKey);
@@ -561,6 +561,7 @@ describe('lib/optimizely_user_context', function() {
561561
},
562562
decisionEventDispatched: true,
563563
reasons: [
564+
564565
sprintf(
565566
LOG_MESSAGES.USER_HAS_FORCED_DECISION_WITH_RULE_SPECIFIED,
566567
variationKey,
@@ -681,7 +682,7 @@ describe('lib/optimizely_user_context', function() {
681682
assert.equal(decision.variationKey, '18257766532');
682683
assert.equal(decision.ruleKey, '18322080788');
683684
assert.deepEqual(Object.keys(decision.userContext.forcedDecisionsMap).length, 1);
684-
assert.deepEqual(decision.userContext.forcedDecisionsMap[featureKey]['$null-rule-key'], { variationKey });
685+
assert.deepEqual(decision.userContext.forcedDecisionsMap[featureKey][CONTROL_ATTRIBUTES.FORCED_DECISION_NULL_RULE_KEY], { variationKey });
685686
assert.equal(
686687
true,
687688
decision.reasons.includes(

packages/optimizely-sdk/lib/optimizely_user_context/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
UserAttributes,
2727
Variation
2828
} from '../../lib/shared_types';
29-
import { DECISION_MESSAGES, LOG_MESSAGES } from '../utils/enums';
29+
import { DECISION_MESSAGES, LOG_MESSAGES, CONTROL_ATTRIBUTES } from '../utils/enums';
3030

3131
const logger = getLogger();
3232

@@ -137,11 +137,8 @@ export default class OptimizelyUserContext {
137137
}
138138

139139
const flagKey = context.flagKey;
140-
if (flagKey === '') {
141-
return false;
142-
}
143140

144-
const ruleKey = context.ruleKey ?? '$null-rule-key';
141+
const ruleKey = context.ruleKey ?? CONTROL_ATTRIBUTES.FORCED_DECISION_NULL_RULE_KEY;
145142
const variationKey = decision.variationKey;
146143
const forcedDecision = { variationKey };
147144

@@ -178,7 +175,7 @@ export default class OptimizelyUserContext {
178175
return false;
179176
}
180177

181-
const ruleKey = context.ruleKey ?? '$null-rule-key';
178+
const ruleKey = context.ruleKey ?? CONTROL_ATTRIBUTES.FORCED_DECISION_NULL_RULE_KEY;
182179
const flagKey = context.flagKey;
183180

184181
let isForcedDecisionRemoved = false;
@@ -218,7 +215,7 @@ export default class OptimizelyUserContext {
218215
*/
219216
private findForcedDecision(context: OptimizelyDecisionContext): OptimizelyForcedDecision | null {
220217
let variationKey;
221-
const validRuleKey = context.ruleKey ?? '$null-rule-key';
218+
const validRuleKey = context.ruleKey ?? CONTROL_ATTRIBUTES.FORCED_DECISION_NULL_RULE_KEY;
222219
const flagKey = context.flagKey;
223220

224221
if (this.forcedDecisionsMap.hasOwnProperty(context.flagKey)) {

packages/optimizely-sdk/lib/utils/enums/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export const CONTROL_ATTRIBUTES = {
174174
BUCKETING_ID: '$opt_bucketing_id',
175175
STICKY_BUCKETING_KEY: '$opt_experiment_bucket_map',
176176
USER_AGENT: '$opt_user_agent',
177+
FORCED_DECISION_NULL_RULE_KEY: '$opt_null_rule_key'
177178
};
178179

179180
export const JAVASCRIPT_CLIENT_ENGINE = 'javascript-sdk';

packages/optimizely-sdk/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/optimizely-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"homepage": "https://github.com/optimizely/javascript-sdk/tree/master/packages/optimizely-sdk",
4343
"dependencies": {
4444
"@optimizely/js-sdk-datafile-manager": "^0.9.1",
45-
"@optimizely/js-sdk-event-processor": "^0.9.1",
45+
"@optimizely/js-sdk-event-processor": "^0.9.2",
4646
"@optimizely/js-sdk-logging": "^0.3.1",
4747
"@optimizely/js-sdk-utils": "^0.4.0",
4848
"json-schema": "^0.2.3",

0 commit comments

Comments
 (0)