@@ -40,6 +40,7 @@ var ERROR_MESSAGES = enums.ERROR_MESSAGES;
40
40
var LOG_LEVEL = enums . LOG_LEVEL ;
41
41
var LOG_MESSAGES = enums . LOG_MESSAGES ;
42
42
var DECISION_SOURCES = enums . DECISION_SOURCES ;
43
+ var DECISION_INFO_TYPES = enums . DECISION_INFO_TYPES ;
43
44
44
45
describe ( 'lib/optimizely' , function ( ) {
45
46
describe ( 'constructor' , function ( ) {
@@ -356,7 +357,6 @@ describe('lib/optimizely', function() {
356
357
var optlyInstance ;
357
358
var bucketStub ;
358
359
var clock ;
359
-
360
360
var createdLogger = logger . createLogger ( {
361
361
logLevel : LOG_LEVEL . INFO ,
362
362
logToConsole : false ,
@@ -394,8 +394,8 @@ describe('lib/optimizely', function() {
394
394
describe ( '#activate' , function ( ) {
395
395
it ( 'should call bucketer and dispatchEvent with proper args and return variation key' , function ( ) {
396
396
bucketStub . returns ( '111129' ) ;
397
- var activate = optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
398
- assert . strictEqual ( activate , 'variation' ) ;
397
+ var variation = optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
398
+ assert . strictEqual ( variation , 'variation' ) ;
399
399
400
400
sinon . assert . calledOnce ( bucketer . bucket ) ;
401
401
sinon . assert . calledOnce ( eventDispatcher . dispatchEvent ) ;
@@ -1622,9 +1622,9 @@ describe('lib/optimizely', function() {
1622
1622
describe ( '#getVariation' , function ( ) {
1623
1623
it ( 'should call bucketer and return variation key' , function ( ) {
1624
1624
bucketStub . returns ( '111129' ) ;
1625
- var getVariation = optlyInstance . getVariation ( 'testExperiment' , 'testUser' ) ;
1625
+ var variation = optlyInstance . getVariation ( 'testExperiment' , 'testUser' ) ;
1626
1626
1627
- assert . strictEqual ( getVariation , 'variation' ) ;
1627
+ assert . strictEqual ( variation , 'variation' ) ;
1628
1628
1629
1629
sinon . assert . calledOnce ( bucketer . bucket ) ;
1630
1630
sinon . assert . called ( createdLogger . log ) ;
@@ -2064,15 +2064,15 @@ describe('lib/optimizely', function() {
2064
2064
} ) ;
2065
2065
2066
2066
describe ( 'notification listeners' , function ( ) {
2067
- var decisionListener ;
2067
+ var activateListener ;
2068
2068
var trackListener ;
2069
- var decisionListener2 ;
2069
+ var activateListener2 ;
2070
2070
var trackListener2 ;
2071
2071
2072
2072
beforeEach ( function ( ) {
2073
- decisionListener = sinon . spy ( ) ;
2073
+ activateListener = sinon . spy ( ) ;
2074
2074
trackListener = sinon . spy ( ) ;
2075
- decisionListener2 = sinon . spy ( ) ;
2075
+ activateListener2 = sinon . spy ( ) ;
2076
2076
trackListener2 = sinon . spy ( ) ;
2077
2077
bucketStub . returns ( '111129' ) ;
2078
2078
sinon . stub ( fns , 'currentTimestamp' ) . returns ( 1509489766569 ) ;
@@ -2085,11 +2085,11 @@ describe('lib/optimizely', function() {
2085
2085
it ( 'should call a listener added for activate when activate is called' , function ( ) {
2086
2086
optlyInstance . notificationCenter . addNotificationListener (
2087
2087
enums . NOTIFICATION_TYPES . ACTIVATE ,
2088
- decisionListener
2088
+ activateListener
2089
2089
) ;
2090
2090
var variationKey = optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2091
2091
assert . strictEqual ( variationKey , 'variation' ) ;
2092
- sinon . assert . calledOnce ( decisionListener ) ;
2092
+ sinon . assert . calledOnce ( activateListener ) ;
2093
2093
} ) ;
2094
2094
2095
2095
it ( 'should call a listener added for track when track is called' , function ( ) {
@@ -2105,12 +2105,12 @@ describe('lib/optimizely', function() {
2105
2105
it ( 'should not call a removed activate listener when activate is called' , function ( ) {
2106
2106
var listenerId = optlyInstance . notificationCenter . addNotificationListener (
2107
2107
enums . NOTIFICATION_TYPES . ACTIVATE ,
2108
- decisionListener
2108
+ activateListener
2109
2109
) ;
2110
2110
optlyInstance . notificationCenter . removeNotificationListener ( listenerId ) ;
2111
2111
var variationKey = optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2112
2112
assert . strictEqual ( variationKey , 'variation' ) ;
2113
- sinon . assert . notCalled ( decisionListener ) ;
2113
+ sinon . assert . notCalled ( activateListener ) ;
2114
2114
} ) ;
2115
2115
2116
2116
it ( 'should not call a removed track listener when track is called' , function ( ) {
@@ -2127,7 +2127,7 @@ describe('lib/optimizely', function() {
2127
2127
it ( 'removeNotificationListener should only remove the listener with the argument ID' , function ( ) {
2128
2128
optlyInstance . notificationCenter . addNotificationListener (
2129
2129
enums . NOTIFICATION_TYPES . ACTIVATE ,
2130
- decisionListener
2130
+ activateListener
2131
2131
) ;
2132
2132
var trackListenerId = optlyInstance . notificationCenter . addNotificationListener (
2133
2133
enums . NOTIFICATION_TYPES . TRACK ,
@@ -2136,13 +2136,13 @@ describe('lib/optimizely', function() {
2136
2136
optlyInstance . notificationCenter . removeNotificationListener ( trackListenerId ) ;
2137
2137
optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2138
2138
optlyInstance . track ( 'testEvent' , 'testUser' ) ;
2139
- sinon . assert . calledOnce ( decisionListener ) ;
2139
+ sinon . assert . calledOnce ( activateListener ) ;
2140
2140
} ) ;
2141
2141
2142
2142
it ( 'should clear all notification listeners when clearAllNotificationListeners is called' , function ( ) {
2143
2143
optlyInstance . notificationCenter . addNotificationListener (
2144
2144
enums . NOTIFICATION_TYPES . ACTIVATE ,
2145
- decisionListener
2145
+ activateListener
2146
2146
) ;
2147
2147
optlyInstance . notificationCenter . addNotificationListener (
2148
2148
enums . NOTIFICATION_TYPES . TRACK ,
@@ -2152,14 +2152,14 @@ describe('lib/optimizely', function() {
2152
2152
optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2153
2153
optlyInstance . track ( 'testEvent' , 'testUser' ) ;
2154
2154
2155
- sinon . assert . notCalled ( decisionListener ) ;
2155
+ sinon . assert . notCalled ( activateListener ) ;
2156
2156
sinon . assert . notCalled ( trackListener ) ;
2157
2157
} ) ;
2158
2158
2159
2159
it ( 'should clear listeners of certain notification type when clearNotificationListeners is called' , function ( ) {
2160
2160
optlyInstance . notificationCenter . addNotificationListener (
2161
2161
enums . NOTIFICATION_TYPES . ACTIVATE ,
2162
- decisionListener
2162
+ activateListener
2163
2163
) ;
2164
2164
optlyInstance . notificationCenter . addNotificationListener (
2165
2165
enums . NOTIFICATION_TYPES . TRACK ,
@@ -2169,47 +2169,47 @@ describe('lib/optimizely', function() {
2169
2169
optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2170
2170
optlyInstance . track ( 'testEvent' , 'testUser' ) ;
2171
2171
2172
- sinon . assert . notCalled ( decisionListener ) ;
2172
+ sinon . assert . notCalled ( activateListener ) ;
2173
2173
sinon . assert . calledOnce ( trackListener ) ;
2174
2174
} ) ;
2175
2175
2176
2176
it ( 'should only call the listener once after the same listener was added twice' , function ( ) {
2177
2177
optlyInstance . notificationCenter . addNotificationListener (
2178
2178
enums . NOTIFICATION_TYPES . ACTIVATE ,
2179
- decisionListener
2179
+ activateListener
2180
2180
) ;
2181
2181
optlyInstance . notificationCenter . addNotificationListener (
2182
2182
enums . NOTIFICATION_TYPES . ACTIVATE ,
2183
- decisionListener
2183
+ activateListener
2184
2184
) ;
2185
2185
optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2186
- sinon . assert . calledOnce ( decisionListener ) ;
2186
+ sinon . assert . calledOnce ( activateListener ) ;
2187
2187
} ) ;
2188
2188
2189
2189
it ( 'should not add a listener with an invalid type argument' , function ( ) {
2190
2190
var listenerId = optlyInstance . notificationCenter . addNotificationListener (
2191
2191
'not a notification type' ,
2192
- decisionListener
2192
+ activateListener
2193
2193
) ;
2194
2194
assert . strictEqual ( listenerId , - 1 ) ;
2195
2195
optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2196
- sinon . assert . notCalled ( decisionListener ) ;
2196
+ sinon . assert . notCalled ( activateListener ) ;
2197
2197
optlyInstance . track ( 'testEvent' , 'testUser' ) ;
2198
- sinon . assert . notCalled ( decisionListener ) ;
2198
+ sinon . assert . notCalled ( activateListener ) ;
2199
2199
} ) ;
2200
2200
2201
2201
it ( 'should call multiple notification listeners for activate when activate is called' , function ( ) {
2202
2202
optlyInstance . notificationCenter . addNotificationListener (
2203
2203
enums . NOTIFICATION_TYPES . ACTIVATE ,
2204
- decisionListener
2204
+ activateListener
2205
2205
) ;
2206
2206
optlyInstance . notificationCenter . addNotificationListener (
2207
2207
enums . NOTIFICATION_TYPES . ACTIVATE ,
2208
- decisionListener2
2208
+ activateListener2
2209
2209
) ;
2210
2210
optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2211
- sinon . assert . calledOnce ( decisionListener ) ;
2212
- sinon . assert . calledOnce ( decisionListener2 ) ;
2211
+ sinon . assert . calledOnce ( activateListener ) ;
2212
+ sinon . assert . calledOnce ( activateListener2 ) ;
2213
2213
} ) ;
2214
2214
2215
2215
it ( 'should call multiple notification listeners for track when track is called' , function ( ) {
@@ -2230,7 +2230,7 @@ describe('lib/optimizely', function() {
2230
2230
it ( 'should pass the correct arguments to an activate listener when activate is called' , function ( ) {
2231
2231
optlyInstance . notificationCenter . addNotificationListener (
2232
2232
enums . NOTIFICATION_TYPES . ACTIVATE ,
2233
- decisionListener
2233
+ activateListener
2234
2234
) ;
2235
2235
optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2236
2236
var expectedImpressionEvent = {
@@ -2279,7 +2279,7 @@ describe('lib/optimizely', function() {
2279
2279
variation : instanceExperiments [ 0 ] . variations [ 1 ] ,
2280
2280
logEvent : expectedImpressionEvent ,
2281
2281
} ;
2282
- sinon . assert . calledWith ( decisionListener , expectedArgument ) ;
2282
+ sinon . assert . calledWith ( activateListener , expectedArgument ) ;
2283
2283
} ) ;
2284
2284
2285
2285
it ( 'should pass the correct arguments to an activate listener when activate is called with attributes' , function ( ) {
@@ -2288,7 +2288,7 @@ describe('lib/optimizely', function() {
2288
2288
} ;
2289
2289
optlyInstance . notificationCenter . addNotificationListener (
2290
2290
enums . NOTIFICATION_TYPES . ACTIVATE ,
2291
- decisionListener
2291
+ activateListener
2292
2292
) ;
2293
2293
optlyInstance . activate ( 'testExperiment' , 'testUser' , attributes ) ;
2294
2294
var expectedImpressionEvent = {
@@ -2344,7 +2344,7 @@ describe('lib/optimizely', function() {
2344
2344
variation : instanceExperiments [ 0 ] . variations [ 1 ] ,
2345
2345
logEvent : expectedImpressionEvent ,
2346
2346
} ;
2347
- sinon . assert . calledWith ( decisionListener , expectedArgument ) ;
2347
+ sinon . assert . calledWith ( activateListener , expectedArgument ) ;
2348
2348
} ) ;
2349
2349
2350
2350
it ( 'should pass the correct arguments to a track listener when track is called' , function ( ) {
@@ -2519,6 +2519,80 @@ describe('lib/optimizely', function() {
2519
2519
} ;
2520
2520
sinon . assert . calledWith ( trackListener , expectedArgument ) ;
2521
2521
} ) ;
2522
+
2523
+ describe ( 'Decision Listener' , function ( ) {
2524
+ var decisionListener ;
2525
+ beforeEach ( function ( ) {
2526
+ decisionListener = sinon . spy ( ) ;
2527
+ optlyInstance . notificationCenter . addNotificationListener (
2528
+ enums . NOTIFICATION_TYPES . DECISION ,
2529
+ decisionListener
2530
+ ) ;
2531
+ } ) ;
2532
+
2533
+ describe ( 'activate' , function ( ) {
2534
+ it ( 'should send notification with actual variation key when activate returns variation' , function ( ) {
2535
+ bucketStub . returns ( '111129' ) ;
2536
+ var variation = optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2537
+ assert . strictEqual ( variation , 'variation' ) ;
2538
+ sinon . assert . calledWith ( decisionListener , {
2539
+ type : DECISION_INFO_TYPES . EXPERIMENT ,
2540
+ userId : 'testUser' ,
2541
+ attributes : { } ,
2542
+ decisionInfo : {
2543
+ experimentKey : 'testExperiment' ,
2544
+ variationKey : variation
2545
+ }
2546
+ } ) ;
2547
+ } ) ;
2548
+
2549
+ it ( 'should send notification with null variation key when activate returns null' , function ( ) {
2550
+ bucketStub . returns ( null ) ;
2551
+ var variation = optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
2552
+ assert . isNull ( variation ) ;
2553
+ sinon . assert . calledWith ( decisionListener , {
2554
+ type : DECISION_INFO_TYPES . EXPERIMENT ,
2555
+ userId : 'testUser' ,
2556
+ attributes : { } ,
2557
+ decisionInfo : {
2558
+ experimentKey : 'testExperiment' ,
2559
+ variationKey : null
2560
+ }
2561
+ } ) ;
2562
+ } ) ;
2563
+ } ) ;
2564
+
2565
+ describe ( 'getVariation' , function ( ) {
2566
+ it ( 'should send notification with actual variation key when getVariation returns variation' , function ( ) {
2567
+ bucketStub . returns ( '111129' ) ;
2568
+ var variation = optlyInstance . getVariation ( 'testExperiment' , 'testUser' ) ;
2569
+ assert . strictEqual ( variation , 'variation' ) ;
2570
+ sinon . assert . calledWith ( decisionListener , {
2571
+ type : DECISION_INFO_TYPES . EXPERIMENT ,
2572
+ userId : 'testUser' ,
2573
+ attributes : { } ,
2574
+ decisionInfo : {
2575
+ experimentKey : 'testExperiment' ,
2576
+ variationKey : variation
2577
+ }
2578
+ } ) ;
2579
+ } ) ;
2580
+
2581
+ it ( 'should send notification with null variation key when getVariation returns null' , function ( ) {
2582
+ var variation = optlyInstance . getVariation ( 'testExperimentWithAudiences' , 'testUser' , { } ) ;
2583
+ assert . isNull ( variation ) ;
2584
+ sinon . assert . calledWith ( decisionListener , {
2585
+ type : DECISION_INFO_TYPES . EXPERIMENT ,
2586
+ userId : 'testUser' ,
2587
+ attributes : { } ,
2588
+ decisionInfo : {
2589
+ experimentKey : 'testExperimentWithAudiences' ,
2590
+ variationKey : null
2591
+ }
2592
+ } ) ;
2593
+ } ) ;
2594
+ } ) ;
2595
+ } ) ;
2522
2596
} ) ;
2523
2597
} ) ;
2524
2598
0 commit comments