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
29 changes: 13 additions & 16 deletions packages/optimizely-sdk/lib/optimizely_user_context/index.tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2020, Optimizely, Inc. and contributors *
* Copyright 2020-2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -26,7 +26,7 @@ import { createNotificationCenter } from '../core/notification_center';
import Optimizely from '../optimizely';
import errorHandler from '../plugins/error_handler';
import eventDispatcher from '../plugins/event_dispatcher/index.node';
import { CONTROL_ATTRIBUTES, DECISION_MESSAGES, LOG_LEVEL, LOG_MESSAGES } from '../utils/enums';
import { CONTROL_ATTRIBUTES, LOG_LEVEL, LOG_MESSAGES } from '../utils/enums';
import testData from '../tests/test_data';
import { OptimizelyDecideOption } from '../shared_types';

Expand Down Expand Up @@ -314,7 +314,7 @@ describe('lib/optimizely_user_context', function() {
afterEach(function() {
logging.resetLogger();
});
it('should return false when client is not ready', function() {
it('should return true when client is not ready', function() {
fakeOptimizely = {
isValidInstance: sinon.stub().returns(false)
};
Expand All @@ -323,9 +323,8 @@ describe('lib/optimizely_user_context', function() {
userId,
});
var result = user.setForcedDecision({ flagKey: 'feature_1' }, '3324490562');
assert.strictEqual(result, false);
sinon.assert.calledOnce(stubLogHandler.log);
assert.strictEqual(stubLogHandler.log.args[0][1], DECISION_MESSAGES.SDK_NOT_READY);
assert.strictEqual(result, true);
sinon.assert.notCalled(stubLogHandler.log);
});

it('should return true when provided empty string flagKey', function() {
Expand Down Expand Up @@ -848,18 +847,17 @@ describe('lib/optimizely_user_context', function() {
logging.resetLogger();
});

it('should return false when client is not ready', function() {
it('should return true when client is not ready and the forced decision has been removed successfully', function() {
fakeOptimizely = {
isValidInstance: sinon.stub().returns(false)
};
var user = new OptimizelyUserContext({
optimizely: fakeOptimizely,
userId,
userId: 'user123',
});
var result = user.removeForcedDecision('feature_1');
assert.strictEqual(result, false);
sinon.assert.calledOnce(stubLogHandler.log);
assert.strictEqual(stubLogHandler.log.args[0][1], DECISION_MESSAGES.SDK_NOT_READY);
user.setForcedDecision({ flagKey: 'feature_1' }, '3324490562');
var result = user.removeForcedDecision({ flagKey: 'feature_1' });
assert.strictEqual(result, true);
});

it('should return true when the forced decision has been removed successfully and false otherwise', function() {
Expand Down Expand Up @@ -923,7 +921,7 @@ describe('lib/optimizely_user_context', function() {
logging.resetLogger();
});

it('should return false when client is not ready', function() {
it('should return true when client is not ready', function() {
fakeOptimizely = {
isValidInstance: sinon.stub().returns(false)
};
Expand All @@ -932,9 +930,8 @@ describe('lib/optimizely_user_context', function() {
userId,
});
var result = user.removeAllForcedDecisions();
assert.strictEqual(result, false);
sinon.assert.calledOnce(stubLogHandler.log);
assert.strictEqual(stubLogHandler.log.args[0][1], DECISION_MESSAGES.SDK_NOT_READY);
assert.strictEqual(result, true);
sinon.assert.notCalled(stubLogHandler.log);
});

it('should return true when all forced decisions have been removed successfully', function() {
Expand Down
24 changes: 2 additions & 22 deletions packages/optimizely-sdk/lib/optimizely_user_context/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2020, Optimizely, Inc. and contributors *
* Copyright 2020-2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -26,7 +26,7 @@ import {
UserAttributes,
Variation
} from '../../lib/shared_types';
import { DECISION_MESSAGES, LOG_MESSAGES, CONTROL_ATTRIBUTES } from '../utils/enums';
import { LOG_MESSAGES, CONTROL_ATTRIBUTES } from '../utils/enums';

const logger = getLogger();

Expand Down Expand Up @@ -131,11 +131,6 @@ export default class OptimizelyUserContext {
* @return {boolean} true if the forced decision has been set successfully.
*/
setForcedDecision(context: OptimizelyDecisionContext, decision: OptimizelyForcedDecision): boolean {
if (!this.optimizely.isValidInstance()) {
logger.error(DECISION_MESSAGES.SDK_NOT_READY);
return false;
}

const flagKey = context.flagKey;

const ruleKey = context.ruleKey ?? CONTROL_ATTRIBUTES.FORCED_DECISION_NULL_RULE_KEY;
Expand All @@ -156,11 +151,6 @@ export default class OptimizelyUserContext {
* @return {OptimizelyForcedDecision|null} OptimizelyForcedDecision for specified context if exists or null.
*/
getForcedDecision(context: OptimizelyDecisionContext): OptimizelyForcedDecision | null {
if (!this.optimizely.isValidInstance()) {
logger.error(DECISION_MESSAGES.SDK_NOT_READY);
return null;
}

return this.findForcedDecision(context);
}

Expand All @@ -170,11 +160,6 @@ export default class OptimizelyUserContext {
* @return {boolean} true if the forced decision has been removed successfully
*/
removeForcedDecision(context: OptimizelyDecisionContext): boolean {
if (!this.optimizely.isValidInstance()) {
logger.error(DECISION_MESSAGES.SDK_NOT_READY);
return false;
}

const ruleKey = context.ruleKey ?? CONTROL_ATTRIBUTES.FORCED_DECISION_NULL_RULE_KEY;
const flagKey = context.flagKey;

Expand All @@ -199,11 +184,6 @@ export default class OptimizelyUserContext {
* @return {boolean} true if the forced decision has been removed successfully
*/
removeAllForcedDecisions(): boolean {
if (!this.optimizely.isValidInstance()) {
logger.error(DECISION_MESSAGES.SDK_NOT_READY);
return false;

}
this.forcedDecisionsMap = {};
return true;
}
Expand Down