@@ -998,26 +998,26 @@ describe('lib/optimizely', function() {
998
998
} ) ;
999
999
} ) ;
1000
1000
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
+ } ) ;
1021
1021
} ) ;
1022
1022
1023
1023
describe ( '#track' , function ( ) {
@@ -1712,26 +1712,26 @@ describe('lib/optimizely', function() {
1712
1712
sinon . assert . calledOnce ( eventDispatcher . dispatchEvent ) ;
1713
1713
} ) ;
1714
1714
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
+ } ) ;
1724
1724
1725
- // createdLogger.log.reset();
1725
+ createdLogger . log . reset ( ) ;
1726
1726
1727
- // instance.track('testExperiment', 'testUser');
1727
+ instance . track ( 'testExperiment' , 'testUser' ) ;
1728
1728
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' ) ) ;
1732
1732
1733
- // sinon.assert.notCalled(eventDispatcher.dispatchEvent);
1734
- // });
1733
+ sinon . assert . notCalled ( eventDispatcher . dispatchEvent ) ;
1734
+ } ) ;
1735
1735
} ) ;
1736
1736
1737
1737
describe ( '#getVariation' , function ( ) {
@@ -1879,26 +1879,26 @@ describe('lib/optimizely', function() {
1879
1879
} ) ;
1880
1880
} ) ;
1881
1881
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
+ } ) ;
1891
1891
1892
- // createdLogger.log.reset();
1892
+ createdLogger . log . reset ( ) ;
1893
1893
1894
- // instance.getVariation('testExperiment', 'testUser');
1894
+ instance . getVariation ( 'testExperiment' , 'testUser' ) ;
1895
1895
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' ) ) ;
1899
1899
1900
- // sinon.assert.notCalled(eventDispatcher.dispatchEvent);
1901
- // });
1900
+ sinon . assert . notCalled ( eventDispatcher . dispatchEvent ) ;
1901
+ } ) ;
1902
1902
1903
1903
describe ( 'order of bucketing operations' , function ( ) {
1904
1904
it ( 'should properly follow the order of bucketing operations' , function ( ) {
@@ -9821,9 +9821,12 @@ describe('lib/optimizely', function() {
9821
9821
var optlyInstance ;
9822
9822
9823
9823
it ( 'should call the project config manager stop method when the close method is called' , function ( ) {
9824
+ const projectConfigManager = getMockProjectConfigManager ( ) ;
9825
+
9824
9826
optlyInstance = new Optimizely ( {
9825
9827
clientEngine : 'node-sdk' ,
9826
9828
errorHandler : errorHandler ,
9829
+ projectConfigManager,
9827
9830
eventDispatcher : eventDispatcher ,
9828
9831
jsonSchemaValidator : jsonSchemaValidator ,
9829
9832
logger : createdLogger ,
@@ -9833,14 +9836,14 @@ describe('lib/optimizely', function() {
9833
9836
notificationCenter,
9834
9837
} ) ;
9835
9838
optlyInstance . close ( ) ;
9836
- var fakeManager = projectConfigManager . createProjectConfigManager . getCall ( 0 ) . returnValue ;
9837
- sinon . assert . calledOnce ( fakeManager . stop ) ;
9839
+ sinon . assert . calledOnce ( projectConfigManager . stop ) ;
9838
9840
} ) ;
9839
9841
9840
- describe ( 'when no datafile is available yet ' , function ( ) {
9842
+ describe ( 'when no project config is available yet ' , function ( ) {
9841
9843
beforeEach ( function ( ) {
9842
9844
optlyInstance = new Optimizely ( {
9843
9845
clientEngine : 'node-sdk' ,
9846
+ projectConfigManager : getMockProjectConfigManager ( ) ,
9844
9847
errorHandler : errorHandler ,
9845
9848
eventDispatcher : eventDispatcher ,
9846
9849
jsonSchemaValidator : jsonSchemaValidator ,
@@ -9888,18 +9891,12 @@ describe('lib/optimizely', function() {
9888
9891
clearTimeoutSpy . restore ( ) ;
9889
9892
} ) ;
9890
9893
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
+
9901
9897
optlyInstance = new Optimizely ( {
9902
9898
clientEngine : 'node-sdk' ,
9899
+ projectConfigManager,
9903
9900
errorHandler : errorHandler ,
9904
9901
eventDispatcher : eventDispatcher ,
9905
9902
jsonSchemaValidator : jsonSchemaValidator ,
@@ -9909,15 +9906,17 @@ describe('lib/optimizely', function() {
9909
9906
notificationCenter,
9910
9907
eventProcessor,
9911
9908
} ) ;
9912
- return optlyInstance . onReady ( ) . then ( function ( result ) {
9913
- assert . deepEqual ( result , { success : true } ) ;
9914
- } ) ;
9909
+
9910
+ return optlyInstance . onReady ( ) ;
9915
9911
} ) ;
9916
9912
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
+
9918
9916
optlyInstance = new Optimizely ( {
9919
9917
clientEngine : 'node-sdk' ,
9920
9918
errorHandler : errorHandler ,
9919
+ projectConfigManager,
9921
9920
eventDispatcher : eventDispatcher ,
9922
9921
jsonSchemaValidator : jsonSchemaValidator ,
9923
9922
logger : createdLogger ,
@@ -9928,17 +9927,20 @@ describe('lib/optimizely', function() {
9928
9927
} ) ;
9929
9928
var readyPromise = optlyInstance . onReady ( { timeout : 500 } ) ;
9930
9929
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' )
9935
9934
} ) ;
9936
9935
} ) ;
9937
9936
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
+
9939
9940
optlyInstance = new Optimizely ( {
9940
9941
clientEngine : 'node-sdk' ,
9941
9942
errorHandler : errorHandler ,
9943
+ projectConfigManager,
9942
9944
eventDispatcher : eventDispatcher ,
9943
9945
jsonSchemaValidator : jsonSchemaValidator ,
9944
9946
logger : createdLogger ,
@@ -9949,17 +9951,20 @@ describe('lib/optimizely', function() {
9949
9951
} ) ;
9950
9952
var readyPromise = optlyInstance . onReady ( ) ;
9951
9953
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' )
9956
9958
} ) ;
9957
9959
} ) ;
9958
9960
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
+
9960
9964
optlyInstance = new Optimizely ( {
9961
9965
clientEngine : 'node-sdk' ,
9962
9966
errorHandler : errorHandler ,
9967
+ projectConfigManager,
9963
9968
eventDispatcher : eventDispatcher ,
9964
9969
jsonSchemaValidator : jsonSchemaValidator ,
9965
9970
logger : createdLogger ,
@@ -9970,17 +9975,18 @@ describe('lib/optimizely', function() {
9970
9975
} ) ;
9971
9976
var readyPromise = optlyInstance . onReady ( { timeout : 100 } ) ;
9972
9977
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' )
9977
9982
} ) ;
9978
9983
} ) ;
9979
9984
9980
9985
it ( 'can be called several times with different timeout values and the returned promises behave correctly' , function ( ) {
9981
9986
optlyInstance = new Optimizely ( {
9982
9987
clientEngine : 'node-sdk' ,
9983
9988
errorHandler : errorHandler ,
9989
+ projectConfigManager : getMockProjectConfigManager ( ) ,
9984
9990
eventDispatcher : eventDispatcher ,
9985
9991
jsonSchemaValidator : jsonSchemaValidator ,
9986
9992
logger : createdLogger ,
@@ -10007,17 +10013,10 @@ describe('lib/optimizely', function() {
10007
10013
} ) ;
10008
10014
10009
10015
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
- } ) ;
10018
10016
optlyInstance = new Optimizely ( {
10019
10017
clientEngine : 'node-sdk' ,
10020
10018
errorHandler : errorHandler ,
10019
+ projectConfigManager : getMockProjectConfigManager ( ) ,
10021
10020
eventDispatcher : eventDispatcher ,
10022
10021
jsonSchemaValidator : jsonSchemaValidator ,
10023
10022
logger : createdLogger ,
@@ -10038,18 +10037,13 @@ describe('lib/optimizely', function() {
10038
10037
describe ( 'project config updates' , function ( ) {
10039
10038
var fakeProjectConfigManager ;
10040
10039
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 ( ) ,
10048
10041
10049
10042
optlyInstance = new Optimizely ( {
10050
10043
clientEngine : 'node-sdk' ,
10051
10044
errorHandler : errorHandler ,
10052
10045
eventDispatcher : eventDispatcher ,
10046
+ projectConfigManager : fakeProjectConfigManager ,
10053
10047
jsonSchemaValidator : jsonSchemaValidator ,
10054
10048
logger : createdLogger ,
10055
10049
sdkKey : '12345' ,
@@ -10066,10 +10060,13 @@ describe('lib/optimizely', function() {
10066
10060
assert . isNull ( optlyInstance . activate ( 'myOtherExperiment' , 'user98765' ) ) ;
10067
10061
10068
10062
// 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 ) ;
10073
10070
10074
10071
// With the new project config containing this feature, should return true
10075
10072
assert . isTrue ( optlyInstance . isFeatureEnabled ( 'test_feature_for_experiment' , 'user45678' ) ) ;
@@ -10097,9 +10094,9 @@ describe('lib/optimizely', function() {
10097
10094
] ,
10098
10095
} ) ;
10099
10096
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 ) ;
10103
10100
10104
10101
// activate should return a variation for the new experiment
10105
10102
assert . strictEqual ( optlyInstance . activate ( 'myOtherExperiment' , 'user98765' ) , 'control' ) ;
@@ -10111,9 +10108,9 @@ describe('lib/optimizely', function() {
10111
10108
enums . NOTIFICATION_TYPES . OPTIMIZELY_CONFIG_UPDATE ,
10112
10109
listener
10113
10110
) ;
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
+
10117
10114
sinon . assert . calledOnce ( listener ) ;
10118
10115
} ) ;
10119
10116
} ) ;
@@ -10136,9 +10133,14 @@ describe('lib/optimizely', function() {
10136
10133
batchSize : 1 ,
10137
10134
notificationCenter : notificationCenter ,
10138
10135
} ) ;
10136
+
10137
+ const datafile = testData . getTestProjectConfig ( ) ;
10138
+ const mockConfigManager = getMockProjectConfigManager ( ) ;
10139
+ mockConfigManager . setConfig ( createProjectConfig ( datafile , JSON . stringify ( datafile ) ) ) ;
10140
+
10139
10141
optlyInstance = new Optimizely ( {
10140
10142
clientEngine : 'node-sdk' ,
10141
- datafile : testData . getTestProjectConfig ( ) ,
10143
+ projectConfigManager : mockConfigManager ,
10142
10144
errorHandler,
10143
10145
logger,
10144
10146
isValidInstance : true ,
@@ -10197,9 +10199,13 @@ describe('lib/optimizely', function() {
10197
10199
} ) ;
10198
10200
10199
10201
beforeEach ( function ( ) {
10202
+ const datafile = testData . getTestProjectConfig ( ) ;
10203
+ const mockConfigManager = getMockProjectConfigManager ( ) ;
10204
+ mockConfigManager . setConfig ( createProjectConfig ( datafile , JSON . stringify ( datafile ) ) ) ;
10205
+
10200
10206
optlyInstanceWithOdp = new Optimizely ( {
10201
10207
clientEngine : 'node-sdk' ,
10202
- datafile : testData . getTestProjectConfig ( ) ,
10208
+ projectConfigManager : mockConfigManager ,
10203
10209
errorHandler : errorHandler ,
10204
10210
eventDispatcher : eventDispatcher ,
10205
10211
jsonSchemaValidator : jsonSchemaValidator ,
0 commit comments