@@ -55,19 +55,21 @@ describe('lib/core/project_config/project_config_manager', function() {
55
55
logging . resetLogger ( ) ;
56
56
} ) ;
57
57
58
- it ( 'should throw an error if neither datafile nor sdkKey are passed into the constructor' , function ( done ) {
58
+ it ( 'should call the error handler and fulfill onReady with an unsuccessful result if neither datafile nor sdkKey are passed into the constructor' , function ( ) {
59
59
var manager = new projectConfigManager . ProjectConfigManager ( {
60
60
skipJSONValidation : true ,
61
61
} ) ;
62
62
sinon . assert . calledOnce ( globalStubErrorHandler . handleError ) ;
63
63
var errorMessage = globalStubErrorHandler . handleError . lastCall . args [ 0 ] . message ;
64
64
assert . strictEqual ( errorMessage , sprintf ( ERROR_MESSAGES . DATAFILE_AND_SDK_KEY_MISSING , 'PROJECT_CONFIG_MANAGER' ) ) ;
65
- manager . onReady ( ) . catch ( function ( ) {
66
- done ( ) ;
65
+ return manager . onReady ( ) . then ( function ( result ) {
66
+ assert . include ( result , {
67
+ success : false ,
68
+ } ) ;
67
69
} ) ;
68
70
} ) ;
69
71
70
- it ( 'should throw an error if the datafile JSON is malformed' , function ( done ) {
72
+ it ( 'should call the error handler and fulfill onReady with an unsuccessful result if the datafile JSON is malformed' , function ( ) {
71
73
var invalidDatafileJSON = 'abc' ;
72
74
var manager = new projectConfigManager . ProjectConfigManager ( {
73
75
datafile : invalidDatafileJSON ,
@@ -76,12 +78,14 @@ describe('lib/core/project_config/project_config_manager', function() {
76
78
sinon . assert . calledOnce ( globalStubErrorHandler . handleError ) ;
77
79
var errorMessage = globalStubErrorHandler . handleError . lastCall . args [ 0 ] . message ;
78
80
assert . strictEqual ( errorMessage , sprintf ( ERROR_MESSAGES . INVALID_DATAFILE_MALFORMED , 'CONFIG_VALIDATOR' ) ) ;
79
- manager . onReady ( ) . catch ( function ( ) {
80
- done ( ) ;
81
+ return manager . onReady ( ) . then ( function ( result ) {
82
+ assert . include ( result , {
83
+ success : false ,
84
+ } ) ;
81
85
} ) ;
82
86
} ) ;
83
87
84
- it ( 'should throw an error if the datafile is not valid' , function ( done ) {
88
+ it ( 'should call the error handler and fulfill onReady with an unsuccessful result if the datafile is not valid' , function ( ) {
85
89
var invalidDatafile = testData . getTestProjectConfig ( ) ;
86
90
delete invalidDatafile [ 'projectId' ] ;
87
91
var manager = new projectConfigManager . ProjectConfigManager ( {
@@ -91,21 +95,25 @@ describe('lib/core/project_config/project_config_manager', function() {
91
95
sinon . assert . calledOnce ( globalStubErrorHandler . handleError ) ;
92
96
var errorMessage = globalStubErrorHandler . handleError . lastCall . args [ 0 ] . message ;
93
97
assert . strictEqual ( errorMessage , sprintf ( ERROR_MESSAGES . INVALID_DATAFILE , 'JSON_SCHEMA_VALIDATOR' , 'projectId' , 'is missing and it is required' ) ) ;
94
- manager . onReady ( ) . catch ( function ( ) {
95
- done ( ) ;
98
+ return manager . onReady ( ) . then ( function ( result ) {
99
+ assert . include ( result , {
100
+ success : false ,
101
+ } ) ;
96
102
} ) ;
97
103
} ) ;
98
104
99
- it ( 'should throw an error if the datafile version is not supported' , function ( done ) {
105
+ it ( 'should call the error handler and fulfill onReady with an unsuccessful result if the datafile version is not supported' , function ( ) {
100
106
var manager = new projectConfigManager . ProjectConfigManager ( {
101
107
datafile : testData . getUnsupportedVersionConfig ( ) ,
102
108
jsonSchemaValidator : jsonSchemaValidator ,
103
109
} ) ;
104
110
sinon . assert . calledOnce ( globalStubErrorHandler . handleError ) ;
105
111
var errorMessage = globalStubErrorHandler . handleError . lastCall . args [ 0 ] . message ;
106
112
assert . strictEqual ( errorMessage , sprintf ( ERROR_MESSAGES . INVALID_DATAFILE_VERSION , 'CONFIG_VALIDATOR' , '5' ) ) ;
107
- manager . onReady ( ) . catch ( function ( ) {
108
- done ( ) ;
113
+ return manager . onReady ( ) . then ( function ( result ) {
114
+ assert . include ( result , {
115
+ success : false ,
116
+ } ) ;
109
117
} ) ;
110
118
} ) ;
111
119
@@ -141,7 +149,7 @@ describe('lib/core/project_config/project_config_manager', function() {
141
149
} ) ;
142
150
} ) ;
143
151
144
- it ( 'should return a valid datafile from getConfig and resolve onReady' , function ( ) {
152
+ it ( 'should return a valid datafile from getConfig and resolve onReady with a successful result ' , function ( ) {
145
153
var configWithFeatures = testData . getTestProjectConfigWithFeatures ( ) ;
146
154
var manager = new projectConfigManager . ProjectConfigManager ( {
147
155
datafile : configWithFeatures ,
@@ -150,7 +158,11 @@ describe('lib/core/project_config/project_config_manager', function() {
150
158
manager . getConfig ( ) ,
151
159
projectConfig . createProjectConfig ( configWithFeatures )
152
160
) ;
153
- return manager . onReady ( ) ;
161
+ return manager . onReady ( ) . then ( function ( result ) {
162
+ assert . include ( result , {
163
+ success : true ,
164
+ } ) ;
165
+ } ) ;
154
166
} ) ;
155
167
156
168
it ( 'does not call onUpdate listeners after becoming ready when constructed with a valid datafile and without sdkKey' , function ( ) {
@@ -185,7 +197,7 @@ describe('lib/core/project_config/project_config_manager', function() {
185
197
} ) ;
186
198
187
199
describe ( 'when constructed with sdkKey and without datafile' , function ( ) {
188
- it ( 'updates itself when the datafile manager is ready and then emits updates' , function ( ) {
200
+ it ( 'updates itself when the datafile manager is ready, fulfills its onReady promise with a successful result, and then emits updates' , function ( ) {
189
201
var configWithFeatures = testData . getTestProjectConfigWithFeatures ( ) ;
190
202
datafileManager . DatafileManager . returns ( {
191
203
start : sinon . stub ( ) ,
@@ -198,7 +210,10 @@ describe('lib/core/project_config/project_config_manager', function() {
198
210
sdkKey : '12345' ,
199
211
} ) ;
200
212
assert . isNull ( manager . getConfig ( ) ) ;
201
- return manager . onReady ( ) . then ( function ( ) {
213
+ return manager . onReady ( ) . then ( function ( result ) {
214
+ assert . include ( result , {
215
+ success : true ,
216
+ } ) ;
202
217
assert . deepEqual (
203
218
manager . getConfig ( ) ,
204
219
projectConfig . createProjectConfig ( configWithFeatures )
@@ -290,7 +305,7 @@ describe('lib/core/project_config/project_config_manager', function() {
290
305
} ) ;
291
306
} ) ;
292
307
293
- it ( 'rejects its ready promise when the datafile manager emits an invalid datafile' , function ( done ) {
308
+ it ( 'fulfills its ready promise with an unsuccessful result when the datafile manager emits an invalid datafile' , function ( ) {
294
309
var invalidDatafile = testData . getTestProjectConfig ( ) ;
295
310
delete invalidDatafile [ 'projectId' ] ;
296
311
datafileManager . DatafileManager . returns ( {
@@ -304,8 +319,29 @@ describe('lib/core/project_config/project_config_manager', function() {
304
319
jsonSchemaValidator : jsonSchemaValidator ,
305
320
sdkKey : '12345' ,
306
321
} ) ;
307
- manager . onReady ( ) . catch ( function ( ) {
308
- done ( ) ;
322
+ return manager . onReady ( ) . then ( function ( result ) {
323
+ assert . include ( result , {
324
+ success : false ,
325
+ } ) ;
326
+ } ) ;
327
+ } ) ;
328
+
329
+ it ( 'fullfils its ready promise with an unsuccessful result when the datafile manager onReady promise rejects' , function ( ) {
330
+ datafileManager . DatafileManager . returns ( {
331
+ start : sinon . stub ( ) ,
332
+ stop : sinon . stub ( ) ,
333
+ get : sinon . stub ( ) . returns ( null ) ,
334
+ on : sinon . stub ( ) . returns ( function ( ) { } ) ,
335
+ onReady : sinon . stub ( ) . returns ( Promise . reject ( new Error ( 'Failed to become ready' ) ) )
336
+ } ) ;
337
+ var manager = new projectConfigManager . ProjectConfigManager ( {
338
+ jsonSchemaValidator : jsonSchemaValidator ,
339
+ sdkKey : '12345' ,
340
+ } ) ;
341
+ return manager . onReady ( ) . then ( function ( result ) {
342
+ assert . include ( result , {
343
+ success : false ,
344
+ } ) ;
309
345
} ) ;
310
346
} ) ;
311
347
@@ -318,8 +354,8 @@ describe('lib/core/project_config/project_config_manager', function() {
318
354
} ) ;
319
355
} ) ;
320
356
321
- describe ( 'when constructed with sdkKey and with a valid datafile' , function ( ) {
322
- it ( 'does not call onUpdate listeners after becoming ready' , function ( ) {
357
+ describe ( 'when constructed with sdkKey and with a valid datafile object ' , function ( ) {
358
+ it ( 'fulfills its onReady promise with a successful result, and does not call onUpdate listeners after becoming ready' , function ( ) {
323
359
datafileManager . DatafileManager . returns ( {
324
360
start : sinon . stub ( ) ,
325
361
stop : sinon . stub ( ) ,
@@ -334,7 +370,37 @@ describe('lib/core/project_config/project_config_manager', function() {
334
370
} ) ;
335
371
var onUpdateSpy = sinon . spy ( ) ;
336
372
manager . onUpdate ( onUpdateSpy ) ;
337
- return manager . onReady ( ) . then ( function ( ) {
373
+ return manager . onReady ( ) . then ( function ( result ) {
374
+ assert . include ( result , {
375
+ success : true ,
376
+ } ) ;
377
+ // Datafile is the same as what it was constructed with, so should
378
+ // not have called update listener
379
+ sinon . assert . notCalled ( onUpdateSpy ) ;
380
+ } ) ;
381
+ } ) ;
382
+ } ) ;
383
+
384
+ describe ( 'when constructed with sdkKey and with a valid datafile string' , function ( ) {
385
+ it ( 'fulfills its onReady promise with a successful result, and does not call onUpdate listeners after becoming ready' , function ( ) {
386
+ datafileManager . DatafileManager . returns ( {
387
+ start : sinon . stub ( ) ,
388
+ stop : sinon . stub ( ) ,
389
+ get : sinon . stub ( ) . returns ( testData . getTestProjectConfigWithFeatures ( ) ) ,
390
+ on : sinon . stub ( ) . returns ( function ( ) { } ) ,
391
+ onReady : sinon . stub ( ) . returns ( Promise . resolve ( ) )
392
+ } ) ;
393
+ var configWithFeatures = testData . getTestProjectConfigWithFeatures ( ) ;
394
+ var manager = new projectConfigManager . ProjectConfigManager ( {
395
+ datafile : JSON . stringify ( configWithFeatures ) ,
396
+ sdkKey : '12345' ,
397
+ } ) ;
398
+ var onUpdateSpy = sinon . spy ( ) ;
399
+ manager . onUpdate ( onUpdateSpy ) ;
400
+ return manager . onReady ( ) . then ( function ( result ) {
401
+ assert . include ( result , {
402
+ success : true ,
403
+ } ) ;
338
404
// Datafile is the same as what it was constructed with, so should
339
405
// not have called update listener
340
406
sinon . assert . notCalled ( onUpdateSpy ) ;
0 commit comments