16
16
var configValidator = require ( './utils/config_validator' ) ;
17
17
var enums = require ( './utils/enums' ) ;
18
18
var logger = require ( './plugins/logger' ) ;
19
- var Optimizely = require ( './optimizely' ) ;
19
+
20
20
var packageJSON = require ( '../package.json' ) ;
21
21
var eventDispatcher = require ( './plugins/event_dispatcher/index.browser' ) ;
22
22
var testData = require ( './tests/test_data' ) ;
23
23
24
24
var chai = require ( 'chai' ) ;
25
25
var assert = chai . assert ;
26
- var find = require ( 'lodash/find' ) ;
27
26
var sinon = require ( 'sinon' ) ;
28
27
29
28
describe ( 'javascript-sdk' , function ( ) {
@@ -40,7 +39,6 @@ describe('javascript-sdk', function() {
40
39
logLevel : enums . LOG_LEVEL . INFO ,
41
40
logToConsole : false ,
42
41
} ) ;
43
- sinon . spy ( console , 'error' ) ;
44
42
sinon . stub ( configValidator , 'validate' ) ;
45
43
46
44
xhr = sinon . useFakeXMLHttpRequest ( ) ;
@@ -49,14 +47,53 @@ describe('javascript-sdk', function() {
49
47
xhr . onCreate = function ( req ) {
50
48
requests . push ( req ) ;
51
49
} ;
50
+
51
+ sinon . spy ( console , 'log' ) ;
52
+ sinon . spy ( console , 'info' ) ;
53
+ sinon . spy ( console , 'warn' ) ;
54
+ sinon . spy ( console , 'error' ) ;
52
55
} ) ;
53
56
54
57
afterEach ( function ( ) {
58
+ console . log . restore ( ) ;
59
+ console . info . restore ( ) ;
60
+ console . warn . restore ( ) ;
55
61
console . error . restore ( ) ;
56
62
configValidator . validate . restore ( ) ;
57
63
xhr . restore ( ) ;
58
64
} ) ;
59
65
66
+ // this test has to come first due to local state of the logLevel
67
+ it ( 'should default to INFO when no logLevel is provided' , function ( ) {
68
+ // checking that INFO logs log for an unspecified logLevel
69
+ var optlyInstance = window . optimizelySdk . createInstance ( {
70
+ datafile : testData . getTestProjectConfig ( ) ,
71
+ skipJSONValidation : true ,
72
+ } ) ;
73
+ assert . strictEqual ( console . info . getCalls ( ) . length , 1 ) ;
74
+ call = console . info . getCalls ( ) [ 0 ] ;
75
+ assert . strictEqual ( call . args . length , 1 ) ;
76
+ assert ( call . args [ 0 ] . indexOf ( 'OPTIMIZELY: Skipping JSON schema validation.' ) > - 1 ) ;
77
+ } ) ;
78
+
79
+ it ( 'should instantiate the logger with a custom logLevel when provided' , function ( ) {
80
+ // checking that INFO logs do not log for a logLevel of ERROR
81
+ var optlyInstance = window . optimizelySdk . createInstance ( {
82
+ datafile : testData . getTestProjectConfig ( ) ,
83
+ logLevel : enums . LOG_LEVEL . ERROR ,
84
+ skipJSONValidation : true ,
85
+ } ) ;
86
+ assert . strictEqual ( console . log . getCalls ( ) . length , 0 ) ;
87
+
88
+ // checking that ERROR logs do log for a logLevel of ERROR
89
+ var optlyInstanceInvalid = window . optimizelySdk . createInstance ( {
90
+ datafile : { } ,
91
+ logLevel : enums . LOG_LEVEL . ERROR ,
92
+ } ) ;
93
+ optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
94
+ assert . strictEqual ( console . error . getCalls ( ) . length , 1 ) ;
95
+ } ) ;
96
+
60
97
it ( 'should not throw if the provided config is not valid' , function ( ) {
61
98
configValidator . validate . throws ( new Error ( 'Invalid config or something' ) ) ;
62
99
assert . doesNotThrow ( function ( ) {
@@ -249,51 +286,6 @@ describe('javascript-sdk', function() {
249
286
var variation = optlyInstance . getVariation ( 'testExperimentNotRunning' , 'testUser' ) ;
250
287
assert . strictEqual ( variation , null ) ;
251
288
} ) ;
252
-
253
- describe ( 'automatically created logger instances' , function ( ) {
254
- beforeEach ( function ( ) {
255
- sinon . spy ( console , 'log' ) ;
256
- sinon . spy ( console , 'info' ) ;
257
- sinon . spy ( console , 'warn' ) ;
258
- } ) ;
259
-
260
- afterEach ( function ( ) {
261
- console . log . restore ( ) ;
262
- console . info . restore ( ) ;
263
- console . warn . restore ( ) ;
264
- } ) ;
265
-
266
- // this test has to come first due to local state of the logLevel
267
- it ( 'should default to INFO when no logLevel is provided' , function ( ) {
268
- // checking that INFO logs log for an unspecified logLevel
269
- var optlyInstance = window . optimizelySdk . createInstance ( {
270
- datafile : testData . getTestProjectConfig ( ) ,
271
- skipJSONValidation : true ,
272
- } ) ;
273
- assert . strictEqual ( console . info . getCalls ( ) . length , 1 ) ;
274
- call = console . info . getCalls ( ) [ 0 ] ;
275
- assert . strictEqual ( call . args . length , 1 ) ;
276
- assert ( call . args [ 0 ] . indexOf ( 'OPTIMIZELY: Skipping JSON schema validation.' ) > - 1 ) ;
277
- } ) ;
278
-
279
- it ( 'should instantiate the logger with a custom logLevel when provided' , function ( ) {
280
- // checking that INFO logs do not log for a logLevel of ERROR
281
- var optlyInstance = window . optimizelySdk . createInstance ( {
282
- datafile : testData . getTestProjectConfig ( ) ,
283
- logLevel : enums . LOG_LEVEL . ERROR ,
284
- skipJSONValidation : true ,
285
- } ) ;
286
- assert . strictEqual ( console . log . getCalls ( ) . length , 0 ) ;
287
-
288
- // checking that ERROR logs do log for a logLevel of ERROR
289
- var optlyInstanceInvalid = window . optimizelySdk . createInstance ( {
290
- datafile : { } ,
291
- logLevel : enums . LOG_LEVEL . ERROR ,
292
- } ) ;
293
- optlyInstance . activate ( 'testExperiment' , 'testUser' ) ;
294
- assert . strictEqual ( console . error . getCalls ( ) . length , 1 ) ;
295
- } ) ;
296
- } ) ;
297
289
} ) ;
298
290
} ) ;
299
291
} ) ;
0 commit comments