Skip to content

Commit ef85d65

Browse files
committed
fix tests
1 parent de99436 commit ef85d65

File tree

1 file changed

+116
-110
lines changed

1 file changed

+116
-110
lines changed

lib/optimizely/index.tests.js

Lines changed: 116 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -998,26 +998,26 @@ describe('lib/optimizely', function() {
998998
});
999999
});
10001000

1001-
// it('should not activate when optimizely object is not a valid instance', function() {
1002-
// var instance = new Optimizely({
1003-
// datafile: {},
1004-
// errorHandler: errorHandler,
1005-
// eventDispatcher: eventDispatcher,
1006-
// logger: createdLogger,
1007-
// eventProcessor,
1008-
// notificationCenter,
1009-
// });
1010-
1011-
// createdLogger.log.reset();
1012-
1013-
// instance.activate('testExperiment', 'testUser');
1014-
1015-
// sinon.assert.calledOnce(createdLogger.log);
1016-
// var logMessage = buildLogMessageFromArgs(createdLogger.log.args[0]);
1017-
// assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.INVALID_OBJECT, 'OPTIMIZELY', 'activate'));
1018-
1019-
// sinon.assert.notCalled(eventDispatcher.dispatchEvent);
1020-
// });
1001+
it('should not activate when optimizely object is not a valid instance', function() {
1002+
var instance = new Optimizely({
1003+
projectConfigManager: getMockProjectConfigManager(),
1004+
errorHandler: errorHandler,
1005+
eventDispatcher: eventDispatcher,
1006+
logger: createdLogger,
1007+
eventProcessor,
1008+
notificationCenter,
1009+
});
1010+
1011+
createdLogger.log.reset();
1012+
1013+
instance.activate('testExperiment', 'testUser');
1014+
1015+
sinon.assert.calledOnce(createdLogger.log);
1016+
var logMessage = buildLogMessageFromArgs(createdLogger.log.args[0]);
1017+
assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.INVALID_OBJECT, 'OPTIMIZELY', 'activate'));
1018+
1019+
sinon.assert.notCalled(eventDispatcher.dispatchEvent);
1020+
});
10211021
});
10221022

10231023
describe('#track', function() {
@@ -1712,26 +1712,26 @@ describe('lib/optimizely', function() {
17121712
sinon.assert.calledOnce(eventDispatcher.dispatchEvent);
17131713
});
17141714

1715-
// it('should not track when optimizely object is not a valid instance', function() {
1716-
// var instance = new Optimizely({
1717-
// datafile: {},
1718-
// errorHandler: errorHandler,
1719-
// eventDispatcher: eventDispatcher,
1720-
// logger: createdLogger,
1721-
// eventProcessor,
1722-
// notificationCenter,
1723-
// });
1715+
it('should not track when optimizely object is not a valid instance', function() {
1716+
var instance = new Optimizely({
1717+
projectConfigManager: getMockProjectConfigManager(),
1718+
errorHandler: errorHandler,
1719+
eventDispatcher: eventDispatcher,
1720+
logger: createdLogger,
1721+
eventProcessor,
1722+
notificationCenter,
1723+
});
17241724

1725-
// createdLogger.log.reset();
1725+
createdLogger.log.reset();
17261726

1727-
// instance.track('testExperiment', 'testUser');
1727+
instance.track('testExperiment', 'testUser');
17281728

1729-
// sinon.assert.calledOnce(createdLogger.log);
1730-
// var logMessage = buildLogMessageFromArgs(createdLogger.log.args[0]);
1731-
// assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.INVALID_OBJECT, 'OPTIMIZELY', 'track'));
1729+
sinon.assert.calledOnce(createdLogger.log);
1730+
var logMessage = buildLogMessageFromArgs(createdLogger.log.args[0]);
1731+
assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.INVALID_OBJECT, 'OPTIMIZELY', 'track'));
17321732

1733-
// sinon.assert.notCalled(eventDispatcher.dispatchEvent);
1734-
// });
1733+
sinon.assert.notCalled(eventDispatcher.dispatchEvent);
1734+
});
17351735
});
17361736

17371737
describe('#getVariation', function() {
@@ -1879,26 +1879,26 @@ describe('lib/optimizely', function() {
18791879
});
18801880
});
18811881

1882-
// it('should not return variation when optimizely object is not a valid instance', function() {
1883-
// var instance = new Optimizely({
1884-
// datafile: {},
1885-
// errorHandler: errorHandler,
1886-
// eventDispatcher: eventDispatcher,
1887-
// logger: createdLogger,
1888-
// eventProcessor,
1889-
// notificationCenter,
1890-
// });
1882+
it('should not return variation when optimizely object is not a valid instance', function() {
1883+
var instance = new Optimizely({
1884+
projectConfigManager: getMockProjectConfigManager(),
1885+
errorHandler: errorHandler,
1886+
eventDispatcher: eventDispatcher,
1887+
logger: createdLogger,
1888+
eventProcessor,
1889+
notificationCenter,
1890+
});
18911891

1892-
// createdLogger.log.reset();
1892+
createdLogger.log.reset();
18931893

1894-
// instance.getVariation('testExperiment', 'testUser');
1894+
instance.getVariation('testExperiment', 'testUser');
18951895

1896-
// sinon.assert.calledOnce(createdLogger.log);
1897-
// var logMessage = buildLogMessageFromArgs(createdLogger.log.args[0]);
1898-
// assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.INVALID_OBJECT, 'OPTIMIZELY', 'getVariation'));
1896+
sinon.assert.calledOnce(createdLogger.log);
1897+
var logMessage = buildLogMessageFromArgs(createdLogger.log.args[0]);
1898+
assert.strictEqual(logMessage, sprintf(LOG_MESSAGES.INVALID_OBJECT, 'OPTIMIZELY', 'getVariation'));
18991899

1900-
// sinon.assert.notCalled(eventDispatcher.dispatchEvent);
1901-
// });
1900+
sinon.assert.notCalled(eventDispatcher.dispatchEvent);
1901+
});
19021902

19031903
describe('order of bucketing operations', function() {
19041904
it('should properly follow the order of bucketing operations', function() {
@@ -9821,9 +9821,12 @@ describe('lib/optimizely', function() {
98219821
var optlyInstance;
98229822

98239823
it('should call the project config manager stop method when the close method is called', function() {
9824+
const projectConfigManager = getMockProjectConfigManager();
9825+
98249826
optlyInstance = new Optimizely({
98259827
clientEngine: 'node-sdk',
98269828
errorHandler: errorHandler,
9829+
projectConfigManager,
98279830
eventDispatcher: eventDispatcher,
98289831
jsonSchemaValidator: jsonSchemaValidator,
98299832
logger: createdLogger,
@@ -9833,14 +9836,14 @@ describe('lib/optimizely', function() {
98339836
notificationCenter,
98349837
});
98359838
optlyInstance.close();
9836-
var fakeManager = projectConfigManager.createProjectConfigManager.getCall(0).returnValue;
9837-
sinon.assert.calledOnce(fakeManager.stop);
9839+
sinon.assert.calledOnce(projectConfigManager.stop);
98389840
});
98399841

9840-
describe('when no datafile is available yet ', function() {
9842+
describe('when no project config is available yet ', function() {
98419843
beforeEach(function() {
98429844
optlyInstance = new Optimizely({
98439845
clientEngine: 'node-sdk',
9846+
projectConfigManager: getMockProjectConfigManager(),
98449847
errorHandler: errorHandler,
98459848
eventDispatcher: eventDispatcher,
98469849
jsonSchemaValidator: jsonSchemaValidator,
@@ -9888,18 +9891,12 @@ describe('lib/optimizely', function() {
98889891
clearTimeoutSpy.restore();
98899892
});
98909893

9891-
it('fulfills the promise with the value from the project config manager ready promise after the project config manager ready promise is fulfilled', function() {
9892-
projectConfigManager.createProjectConfigManager.callsFake(function(config) {
9893-
var currentConfig = config.datafile ? projectConfig.createProjectConfig(config.datafile) : null;
9894-
return {
9895-
stop: sinon.stub(),
9896-
getConfig: sinon.stub().returns(currentConfig),
9897-
onUpdate: sinon.stub().returns(function() {}),
9898-
onReady: sinon.stub().returns(Promise.resolve({ success: true })),
9899-
};
9900-
});
9894+
it('fulfills the promise after the project config manager onRunning promise is fulfilled', function() {
9895+
const projectConfigManager = getMockProjectConfigManager();
9896+
99019897
optlyInstance = new Optimizely({
99029898
clientEngine: 'node-sdk',
9899+
projectConfigManager,
99039900
errorHandler: errorHandler,
99049901
eventDispatcher: eventDispatcher,
99059902
jsonSchemaValidator: jsonSchemaValidator,
@@ -9909,15 +9906,17 @@ describe('lib/optimizely', function() {
99099906
notificationCenter,
99109907
eventProcessor,
99119908
});
9912-
return optlyInstance.onReady().then(function(result) {
9913-
assert.deepEqual(result, { success: true });
9914-
});
9909+
9910+
return optlyInstance.onReady();
99159911
});
99169912

9917-
it('fulfills the promise with an unsuccessful result after the timeout has expired when the project config manager onReady promise still has not resolved', function() {
9913+
it('rejects the promise after the timeout has expired when the project config manager onReady promise still has not resolved', function() {
9914+
const projectConfigManager = getMockProjectConfigManager({ onRunning: new Promise(function() {}) });
9915+
99189916
optlyInstance = new Optimizely({
99199917
clientEngine: 'node-sdk',
99209918
errorHandler: errorHandler,
9919+
projectConfigManager,
99219920
eventDispatcher: eventDispatcher,
99229921
jsonSchemaValidator: jsonSchemaValidator,
99239922
logger: createdLogger,
@@ -9928,17 +9927,20 @@ describe('lib/optimizely', function() {
99289927
});
99299928
var readyPromise = optlyInstance.onReady({ timeout: 500 });
99309929
clock.tick(501);
9931-
return readyPromise.then(function(result) {
9932-
assert.include(result, {
9933-
success: false,
9934-
});
9930+
return readyPromise.then(() => {
9931+
return Promise.reject(new Error('Promise should not have resolved'));
9932+
}, (err) => {
9933+
assert.equal(err.message, 'onReady timeout expired after 500 ms')
99359934
});
99369935
});
99379936

9938-
it('fulfills the promise with an unsuccessful result after 30 seconds when no timeout argument is provided and the project config manager onReady promise still has not resolved', function() {
9937+
it('rejects the promise after 30 seconds when no timeout argument is provided and the project config manager onReady promise still has not resolved', function() {
9938+
const projectConfigManager = getMockProjectConfigManager({ onRunning: new Promise(function() {}) });
9939+
99399940
optlyInstance = new Optimizely({
99409941
clientEngine: 'node-sdk',
99419942
errorHandler: errorHandler,
9943+
projectConfigManager,
99429944
eventDispatcher: eventDispatcher,
99439945
jsonSchemaValidator: jsonSchemaValidator,
99449946
logger: createdLogger,
@@ -9949,17 +9951,20 @@ describe('lib/optimizely', function() {
99499951
});
99509952
var readyPromise = optlyInstance.onReady();
99519953
clock.tick(300001);
9952-
return readyPromise.then(function(result) {
9953-
assert.include(result, {
9954-
success: false,
9955-
});
9954+
return readyPromise.then(() => {
9955+
return Promise.reject(new Error('Promise should not have resolved'));
9956+
}, (err) => {
9957+
assert.equal(err.message, 'onReady timeout expired after 30000 ms')
99569958
});
99579959
});
99589960

9959-
it('fulfills the promise with an unsuccessful result after the instance is closed', function() {
9961+
it('rejects the promise after the instance is closed', function() {
9962+
const projectConfigManager = getMockProjectConfigManager({ onRunning: new Promise(function() {}) });
9963+
99609964
optlyInstance = new Optimizely({
99619965
clientEngine: 'node-sdk',
99629966
errorHandler: errorHandler,
9967+
projectConfigManager,
99639968
eventDispatcher: eventDispatcher,
99649969
jsonSchemaValidator: jsonSchemaValidator,
99659970
logger: createdLogger,
@@ -9970,17 +9975,18 @@ describe('lib/optimizely', function() {
99709975
});
99719976
var readyPromise = optlyInstance.onReady({ timeout: 100 });
99729977
optlyInstance.close();
9973-
return readyPromise.then(function(result) {
9974-
assert.include(result, {
9975-
success: false,
9976-
});
9978+
return readyPromise.then(() => {
9979+
return Promise.reject(new Error('Promise should not have resolved'));
9980+
}, (err) => {
9981+
assert.equal(err.message, 'Instance closed')
99779982
});
99789983
});
99799984

99809985
it('can be called several times with different timeout values and the returned promises behave correctly', function() {
99819986
optlyInstance = new Optimizely({
99829987
clientEngine: 'node-sdk',
99839988
errorHandler: errorHandler,
9989+
projectConfigManager: getMockProjectConfigManager(),
99849990
eventDispatcher: eventDispatcher,
99859991
jsonSchemaValidator: jsonSchemaValidator,
99869992
logger: createdLogger,
@@ -10007,17 +10013,10 @@ describe('lib/optimizely', function() {
1000710013
});
1000810014

1000910015
it('clears the timeout when the project config manager ready promise fulfills', function() {
10010-
projectConfigManager.createProjectConfigManager.callsFake(function(config) {
10011-
return {
10012-
stop: sinon.stub(),
10013-
getConfig: sinon.stub().returns(null),
10014-
onUpdate: sinon.stub().returns(function() {}),
10015-
onReady: sinon.stub().returns(Promise.resolve({ success: true })),
10016-
};
10017-
});
1001810016
optlyInstance = new Optimizely({
1001910017
clientEngine: 'node-sdk',
1002010018
errorHandler: errorHandler,
10019+
projectConfigManager: getMockProjectConfigManager(),
1002110020
eventDispatcher: eventDispatcher,
1002210021
jsonSchemaValidator: jsonSchemaValidator,
1002310022
logger: createdLogger,
@@ -10038,18 +10037,13 @@ describe('lib/optimizely', function() {
1003810037
describe('project config updates', function() {
1003910038
var fakeProjectConfigManager;
1004010039
beforeEach(function() {
10041-
fakeProjectConfigManager = {
10042-
stop: sinon.stub(),
10043-
getConfig: sinon.stub().returns(null),
10044-
onUpdate: sinon.stub().returns(function() {}),
10045-
onReady: sinon.stub().returns({ then: function() {} }),
10046-
};
10047-
projectConfigManager.createProjectConfigManager.returns(fakeProjectConfigManager);
10040+
fakeProjectConfigManager = getMockProjectConfigManager(),
1004810041

1004910042
optlyInstance = new Optimizely({
1005010043
clientEngine: 'node-sdk',
1005110044
errorHandler: errorHandler,
1005210045
eventDispatcher: eventDispatcher,
10046+
projectConfigManager: fakeProjectConfigManager,
1005310047
jsonSchemaValidator: jsonSchemaValidator,
1005410048
logger: createdLogger,
1005510049
sdkKey: '12345',
@@ -10066,10 +10060,13 @@ describe('lib/optimizely', function() {
1006610060
assert.isNull(optlyInstance.activate('myOtherExperiment', 'user98765'));
1006710061

1006810062
// Project config manager receives new project config object - should use this now
10069-
var newConfig = projectConfig.createProjectConfig(testData.getTestProjectConfigWithFeatures());
10070-
fakeProjectConfigManager.getConfig.returns(newConfig);
10071-
var updateListener = fakeProjectConfigManager.onUpdate.getCall(0).args[0];
10072-
updateListener(newConfig);
10063+
10064+
const datafile = testData.getTestProjectConfigWithFeatures();
10065+
10066+
const newConfig = createProjectConfig(datafile, JSON.stringify(datafile));
10067+
10068+
fakeProjectConfigManager.setConfig(newConfig);
10069+
fakeProjectConfigManager.pushUpdate(newConfig);
1007310070

1007410071
// With the new project config containing this feature, should return true
1007510072
assert.isTrue(optlyInstance.isFeatureEnabled('test_feature_for_experiment', 'user45678'));
@@ -10097,9 +10094,9 @@ describe('lib/optimizely', function() {
1009710094
],
1009810095
});
1009910096
differentDatafile.revision = '44';
10100-
var differentConfig = projectConfig.createProjectConfig(differentDatafile);
10101-
fakeProjectConfigManager.getConfig.returns(differentConfig);
10102-
updateListener(differentConfig);
10097+
var differentConfig = createProjectConfig(differentDatafile, JSON.stringify(differentDatafile));
10098+
fakeProjectConfigManager.setConfig(differentConfig);
10099+
fakeProjectConfigManager.pushUpdate(differentConfig);
1010310100

1010410101
// activate should return a variation for the new experiment
1010510102
assert.strictEqual(optlyInstance.activate('myOtherExperiment', 'user98765'), 'control');
@@ -10111,9 +10108,9 @@ describe('lib/optimizely', function() {
1011110108
enums.NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE,
1011210109
listener
1011310110
);
10114-
var newConfig = projectConfig.createProjectConfig(testData.getTestProjectConfigWithFeatures());
10115-
var updateListener = fakeProjectConfigManager.onUpdate.getCall(0).args[0];
10116-
updateListener(newConfig);
10111+
var newConfig = createProjectConfig(testData.getTestProjectConfigWithFeatures());
10112+
fakeProjectConfigManager.pushUpdate(newConfig);
10113+
1011710114
sinon.assert.calledOnce(listener);
1011810115
});
1011910116
});
@@ -10136,9 +10133,14 @@ describe('lib/optimizely', function() {
1013610133
batchSize: 1,
1013710134
notificationCenter: notificationCenter,
1013810135
});
10136+
10137+
const datafile = testData.getTestProjectConfig();
10138+
const mockConfigManager = getMockProjectConfigManager();
10139+
mockConfigManager.setConfig(createProjectConfig(datafile, JSON.stringify(datafile)));
10140+
1013910141
optlyInstance = new Optimizely({
1014010142
clientEngine: 'node-sdk',
10141-
datafile: testData.getTestProjectConfig(),
10143+
projectConfigManager: mockConfigManager,
1014210144
errorHandler,
1014310145
logger,
1014410146
isValidInstance: true,
@@ -10197,9 +10199,13 @@ describe('lib/optimizely', function() {
1019710199
});
1019810200

1019910201
beforeEach(function() {
10202+
const datafile = testData.getTestProjectConfig();
10203+
const mockConfigManager = getMockProjectConfigManager();
10204+
mockConfigManager.setConfig(createProjectConfig(datafile, JSON.stringify(datafile)));
10205+
1020010206
optlyInstanceWithOdp = new Optimizely({
1020110207
clientEngine: 'node-sdk',
10202-
datafile: testData.getTestProjectConfig(),
10208+
projectConfigManager: mockConfigManager,
1020310209
errorHandler: errorHandler,
1020410210
eventDispatcher: eventDispatcher,
1020510211
jsonSchemaValidator: jsonSchemaValidator,

0 commit comments

Comments
 (0)