From 25958addc8e206c960cca32dfba19f2c4626ef02 Mon Sep 17 00:00:00 2001 From: Yasir Ali Date: Fri, 3 Dec 2021 14:13:36 +0500 Subject: [PATCH] remove config-ready check from forced-decision apis --- .../optimizely_user_context/index.tests.js | 29 +++++++++---------- .../lib/optimizely_user_context/index.ts | 24 ++------------- 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/packages/optimizely-sdk/lib/optimizely_user_context/index.tests.js b/packages/optimizely-sdk/lib/optimizely_user_context/index.tests.js index a5196ceff..eb13aad69 100644 --- a/packages/optimizely-sdk/lib/optimizely_user_context/index.tests.js +++ b/packages/optimizely-sdk/lib/optimizely_user_context/index.tests.js @@ -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. * @@ -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'; @@ -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) }; @@ -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() { @@ -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() { @@ -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) }; @@ -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() { diff --git a/packages/optimizely-sdk/lib/optimizely_user_context/index.ts b/packages/optimizely-sdk/lib/optimizely_user_context/index.ts index 0e5e2e08f..a7a3d4a62 100644 --- a/packages/optimizely-sdk/lib/optimizely_user_context/index.ts +++ b/packages/optimizely-sdk/lib/optimizely_user_context/index.ts @@ -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. * @@ -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(); @@ -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; @@ -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); } @@ -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; @@ -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; }