Skip to content

Commit 5966373

Browse files
committed
fix user context
1 parent 79ef0e4 commit 5966373

File tree

3 files changed

+22
-50
lines changed

3 files changed

+22
-50
lines changed

lib/notification_center/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ export class DefaultNotificationCenter implements NotificationCenter, Notificati
7373
* @param {ErrorHandler} options.errorHandler An instance of errorHandler to handle any unexpected error
7474
*/
7575
constructor(options: NotificationCenterOptions) {
76-
const logger = options.logger?.child(MODULE_NAME);
77-
const errorNotifier = options.errorNotifier?.child(MODULE_NAME);
78-
79-
this.errorReporter = new ErrorReporter(logger, errorNotifier);
76+
this.errorReporter = new ErrorReporter(options.logger, options.errorNotifier);
8077
}
8178

8279
/**

lib/optimizely_user_context/index.tests.js

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ var createLogger = () => ({
5151
});
5252

5353
const getOptlyInstance = ({ datafileObj, defaultDecideOptions }) => {
54-
const createdLogger = logger.createLogger({ logLevel: LOG_LEVEL.INFO });
54+
const createdLogger = createLogger({ logLevel: LOG_LEVEL.INFO });
5555
const mockConfigManager = getMockProjectConfigManager({
5656
initConfig: createProjectConfig(datafileObj),
5757
});
5858
const eventDispatcher = getMockEventDispatcher();
5959
const eventProcessor = getForwardingEventProcessor(eventDispatcher);
6060

61-
const notificationCenter = createNotificationCenter({ logger: createdLogger, errorHandler: errorHandler });
62-
6361
const optlyInstance = new Optimizely({
6462
clientEngine: 'node-sdk',
6563
projectConfigManager: mockConfigManager,
@@ -69,12 +67,10 @@ const getOptlyInstance = ({ datafileObj, defaultDecideOptions }) => {
6967
isValidInstance: true,
7068
eventBatchSize: 1,
7169
defaultDecideOptions: defaultDecideOptions || [],
72-
notificationCenter,
7370
});
7471

75-
sinon.stub(notificationCenter, 'sendNotifications');
7672

77-
return { optlyInstance, eventProcessor, eventDispatcher, notificationCenter, createdLogger }
73+
return { optlyInstance, eventProcessor, eventDispatcher, createdLogger }
7874
}
7975

8076
describe('lib/optimizely_user_context', function() {
@@ -341,25 +337,17 @@ describe('lib/optimizely_user_context', function() {
341337
logLevel: LOG_LEVEL.DEBUG,
342338
logToConsole: false,
343339
});
344-
var stubLogHandler;
345-
let optlyInstance, notificationCenter, eventDispatcher;
346340

347-
beforeEach(function() {
348-
stubLogHandler = {
349-
log: sinon.stub(),
350-
};
351-
logging.setLogLevel('notset');
352-
logging.setLogHandler(stubLogHandler);
341+
let optlyInstance, eventDispatcher;
353342

354-
({ optlyInstance, notificationCenter, createdLogger, eventDispatcher} = getOptlyInstance({
343+
beforeEach(function() {
344+
({ optlyInstance, createdLogger, eventDispatcher} = getOptlyInstance({
355345
datafileObj: testData.getTestDecideProjectConfig(),
356346
}));
357347
});
358348

359349
afterEach(function() {
360-
logging.resetLogger();
361350
eventDispatcher.dispatchEvent.reset();
362-
notificationCenter.sendNotifications.restore();
363351
});
364352

365353
it('should return true when client is not ready', function() {
@@ -426,7 +414,7 @@ describe('lib/optimizely_user_context', function() {
426414
});
427415

428416
afterEach(function() {
429-
optlyInstance.decisionService.logger.log.restore();
417+
// optlyInstance.decisionService.logger.log.restore();
430418
eventDispatcher.dispatchEvent.reset();
431419
optlyInstance.notificationCenter.sendNotifications.restore();
432420
});
@@ -504,10 +492,13 @@ describe('lib/optimizely_user_context', function() {
504492
});
505493

506494
it('should return forced decision object when forced decision is set for a flag and dispatch an event', function() {
507-
const { optlyInstance, notificationCenter, eventDispatcher } = getOptlyInstance({
495+
const { optlyInstance, eventDispatcher } = getOptlyInstance({
508496
datafileObj: testData.getTestDecideProjectConfig(),
509497
});
510498

499+
const notificationCenter = optlyInstance.notificationCenter;
500+
sinon.stub(notificationCenter, 'sendNotifications');
501+
511502
var user = optlyInstance.createUserContext(userId);
512503
var featureKey = 'feature_1';
513504
var variationKey = '3324490562';
@@ -583,9 +574,13 @@ describe('lib/optimizely_user_context', function() {
583574
});
584575

585576
it('should return forced decision object when forced decision is set for an experiment rule and dispatch an event', function() {
586-
const { optlyInstance, notificationCenter, eventDispatcher } = getOptlyInstance({
577+
const { optlyInstance, eventDispatcher } = getOptlyInstance({
587578
datafileObj: testData.getTestDecideProjectConfig(),
588579
});
580+
581+
const notificationCenter = optlyInstance.notificationCenter;
582+
sinon.stub(notificationCenter, 'sendNotifications');
583+
589584
var attributes = { country: 'US' };
590585
var user = optlyInstance.createUserContext(userId, attributes);
591586
var featureKey = 'feature_1';
@@ -668,9 +663,13 @@ describe('lib/optimizely_user_context', function() {
668663
});
669664

670665
it('should return forced decision object when forced decision is set for a delivery rule and dispatch an event', function() {
671-
const { optlyInstance, notificationCenter, eventDispatcher } = getOptlyInstance({
666+
const { optlyInstance, eventDispatcher } = getOptlyInstance({
672667
datafileObj: testData.getTestDecideProjectConfig(),
673668
});
669+
670+
const notificationCenter = optlyInstance.notificationCenter;
671+
sinon.stub(notificationCenter, 'sendNotifications');
672+
674673
var user = optlyInstance.createUserContext(userId);
675674
var featureKey = 'feature_1';
676675
var variationKey = '3324490633';
@@ -939,18 +938,6 @@ describe('lib/optimizely_user_context', function() {
939938
});
940939

941940
describe('#removeForcedDecision', function() {
942-
var stubLogHandler;
943-
beforeEach(function() {
944-
stubLogHandler = {
945-
log: sinon.stub(),
946-
};
947-
logging.setLogLevel('notset');
948-
logging.setLogHandler(stubLogHandler);
949-
});
950-
afterEach(function() {
951-
logging.resetLogger();
952-
});
953-
954941
it('should return true when client is not ready and the forced decision has been removed successfully', function() {
955942
fakeOptimizely = {
956943
isValidInstance: sinon.stub().returns(false),
@@ -1014,18 +1001,6 @@ describe('lib/optimizely_user_context', function() {
10141001
});
10151002

10161003
describe('#removeAllForcedDecisions', function() {
1017-
var stubLogHandler;
1018-
beforeEach(function() {
1019-
stubLogHandler = {
1020-
log: sinon.stub(),
1021-
};
1022-
logging.setLogLevel('notset');
1023-
logging.setLogHandler(stubLogHandler);
1024-
});
1025-
afterEach(function() {
1026-
logging.resetLogger();
1027-
});
1028-
10291004
it('should return true when client is not ready', function() {
10301005
fakeOptimizely = {
10311006
isValidInstance: sinon.stub().returns(false),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"clean:win": "(if exist dist rd /s/q dist)",
7575
"lint": "tsc --noEmit && eslint 'lib/**/*.js' 'lib/**/*.ts'",
7676
"test-vitest": "tsc --noEmit --p tsconfig.spec.json && vitest run",
77-
"test-mocha": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register -r lib/tests/exit_on_unhandled_rejection.js 'lib/**/*.tests.ts' 'lib/**/**/*.tests.js'",
77+
"test-mocha": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register -r lib/tests/exit_on_unhandled_rejection.js 'lib/**/*.tests.ts' 'lib/**/optimizely_user_context/*.tests.js'",
7878
"test": "npm run test-mocha && npm run test-vitest",
7979
"posttest": "npm run lint",
8080
"test-ci": "npm run test-xbrowser && npm run test-umdbrowser",

0 commit comments

Comments
 (0)