11import * as LDClient from '../index' ;
2- import * as errors from '../errors' ;
32import * as messages from '../messages' ;
43import * as utils from '../utils' ;
54
@@ -43,7 +42,7 @@ describe('LDClient', () => {
4342 } ) ;
4443
4544 describe ( 'initialization' , ( ) => {
46- it ( 'should trigger the ready event' , async ( ) => {
45+ it ( 'triggers " ready" event' , async ( ) => {
4746 await withServers ( async baseConfig => {
4847 await withClient ( user , baseConfig , async client => {
4948 const gotReady = eventSink ( client , 'ready' ) ;
@@ -54,7 +53,7 @@ describe('LDClient', () => {
5453 } ) ;
5554 } ) ;
5655
57- it ( 'should trigger the initialized event' , async ( ) => {
56+ it ( 'triggers " initialized" event' , async ( ) => {
5857 await withServers ( async baseConfig => {
5958 await withClient ( user , baseConfig , async client => {
6059 const gotInited = eventSink ( client , 'initialized' ) ;
@@ -63,49 +62,18 @@ describe('LDClient', () => {
6362 } ) ;
6463 } ) ;
6564
66- it ( 'should emit an error when initialize is called without an environment key' , async ( ) => {
67- const client = platform . testing . makeClient ( '' , user ) ;
68- const gotError = eventSink ( client , 'error' ) ;
69-
70- const err = await gotError . take ( ) ;
71- expect ( err . message ) . toEqual ( messages . environmentNotSpecified ( ) ) ;
72- } ) ;
73-
74- it ( 'should emit an error when an invalid environment key is specified' , async ( ) => {
75- await withServers ( async ( baseConfig , pollServer ) => {
76- pollServer . byDefault ( respond ( 404 ) ) ;
77- await withClient ( user , baseConfig , async client => {
78- const gotError = eventSink ( client , 'error' ) ;
79-
80- await expect ( client . waitForInitialization ( ) ) . rejects . toThrow ( ) ;
81-
82- const err = await gotError . take ( ) ;
83- expect ( err ) . toEqual ( new errors . LDInvalidEnvironmentIdError ( messages . environmentNotFound ( ) ) ) ;
84- } ) ;
85- } ) ;
86- } ) ;
87-
88- it ( 'should emit a failure event when an invalid environment key is specified' , async ( ) => {
89- await withServers ( async ( baseConfig , pollServer ) => {
90- pollServer . byDefault ( respond ( 404 ) ) ;
65+ it ( 'resolves waitForInitialization promise' , async ( ) => {
66+ await withServers ( async baseConfig => {
9167 await withClient ( user , baseConfig , async client => {
92- const gotFailed = eventSink ( client , 'failed' ) ;
93-
94- await expect ( client . waitForInitialization ( ) ) . rejects . toThrow ( ) ;
95-
96- const err = await gotFailed . take ( ) ;
97- expect ( err ) . toEqual ( new errors . LDInvalidEnvironmentIdError ( messages . environmentNotFound ( ) ) ) ;
68+ await client . waitForInitialization ( ) ;
9869 } ) ;
9970 } ) ;
10071 } ) ;
10172
102- it ( 'returns default values when an invalid environment key is specified' , async ( ) => {
103- await withServers ( async ( baseConfig , pollServer ) => {
104- pollServer . byDefault ( respond ( 404 ) ) ;
73+ it ( 'resolves waitUntilReady promise' , async ( ) => {
74+ await withServers ( async baseConfig => {
10575 await withClient ( user , baseConfig , async client => {
106- await expect ( client . waitForInitialization ( ) ) . rejects . toThrow ( ) ;
107-
108- expect ( client . variation ( 'flag-key' , 1 ) ) . toEqual ( 1 ) ;
76+ await client . waitUntilReady ( ) ;
10977 } ) ;
11078 } ) ;
11179 } ) ;
@@ -145,19 +113,6 @@ describe('LDClient', () => {
145113 expect ( result ) . toEqual ( 1 ) ;
146114 } ) ;
147115
148- it ( 'should emit an error event if there was an error fetching flags' , async ( ) => {
149- await withServers ( async ( baseConfig , pollServer ) => {
150- pollServer . byDefault ( respond ( 503 ) ) ;
151- await withClient ( user , baseConfig , async client => {
152- const gotError = eventSink ( client , 'error' ) ;
153-
154- await expect ( client . waitForInitialization ( ) ) . rejects . toThrow ( ) ;
155- const err = await gotError . take ( ) ;
156- expect ( err ) . toEqual ( new errors . LDFlagFetchError ( messages . errorFetchingFlags ( 503 ) ) ) ;
157- } ) ;
158- } ) ;
159- } ) ;
160-
161116 async function verifyCustomHeader ( sendLDHeaders , shouldGetHeaders ) {
162117 await withServers ( async ( baseConfig , pollServer ) => {
163118 await withClient ( user , { ...baseConfig , sendLDHeaders } , async client => {
@@ -231,6 +186,87 @@ describe('LDClient', () => {
231186 } ) ;
232187 } ) ;
233188
189+ describe ( 'failed initialization' , ( ) => {
190+ function doErrorTests ( expectedMessage , doWithClientAsyncFn ) {
191+ async function runTest ( asyncTest ) {
192+ try {
193+ await doWithClientAsyncFn ( asyncTest ) ;
194+ } finally {
195+ // sleep briefly so any unhandled promise rejections will show up in this test, instead of
196+ // in a later test
197+ await sleepAsync ( 2 ) ;
198+ }
199+ }
200+
201+ it ( 'rejects waitForInitialization promise' , async ( ) => {
202+ await runTest ( async client => {
203+ await expect ( client . waitForInitialization ( ) ) . rejects . toThrow ( ) ;
204+ } ) ;
205+ } ) ;
206+
207+ it ( 'resolves waitUntilReady promise' , async ( ) => {
208+ await runTest ( async client => {
209+ await client . waitUntilReady ( ) ;
210+ } ) ;
211+ } ) ;
212+
213+ it ( 'emits "error" event' , async ( ) => {
214+ await runTest ( async client => {
215+ const gotError = eventSink ( client , 'error' ) ;
216+ const err = await gotError . take ( ) ;
217+ expect ( err . message ) . toEqual ( expectedMessage ) ;
218+ } ) ;
219+ } ) ;
220+
221+ it ( 'emits "failed" event' , async ( ) => {
222+ await runTest ( async client => {
223+ const gotFailed = eventSink ( client , 'failed' ) ;
224+ const err = await gotFailed . take ( ) ;
225+ expect ( err . message ) . toEqual ( expectedMessage ) ;
226+ } ) ;
227+ } ) ;
228+
229+ it ( 'emits "ready" event' , async ( ) => {
230+ await runTest ( async client => {
231+ const gotReady = eventSink ( client , 'ready' ) ;
232+ await gotReady . take ( ) ;
233+ } ) ;
234+ } ) ;
235+
236+ it ( 'returns default values' , async ( ) => {
237+ await runTest ( async client => {
238+ await client . waitUntilReady ( ) ;
239+ expect ( client . variation ( 'flag-key' , 1 ) ) . toEqual ( 1 ) ;
240+ } ) ;
241+ } ) ;
242+ }
243+
244+ describe ( 'environment key not specified' , ( ) => {
245+ doErrorTests (
246+ messages . environmentNotSpecified ( ) ,
247+ async callback => await withCloseable ( platform . testing . makeClient ( '' , user ) , callback )
248+ ) ;
249+ } ) ;
250+
251+ describe ( 'invalid environment key (404 error)' , ( ) => {
252+ doErrorTests ( messages . environmentNotFound ( ) , async callback => {
253+ await withServers ( async ( baseConfig , pollServer ) => {
254+ pollServer . byDefault ( respond ( 404 ) ) ;
255+ await withClient ( user , baseConfig , callback ) ;
256+ } ) ;
257+ } ) ;
258+ } ) ;
259+
260+ describe ( 'HTTP error other than 404 on initial poll' , ( ) => {
261+ doErrorTests ( messages . errorFetchingFlags ( 503 ) , async callback => {
262+ await withServers ( async ( baseConfig , pollServer ) => {
263+ pollServer . byDefault ( respond ( 503 ) ) ;
264+ await withClient ( user , baseConfig , callback ) ;
265+ } ) ;
266+ } ) ;
267+ } ) ;
268+ } ) ;
269+
234270 describe ( 'initialization with bootstrap object' , ( ) => {
235271 it ( 'should not fetch flag settings' , async ( ) => {
236272 await withServers ( async ( baseConfig , pollServer ) => {
@@ -271,42 +307,6 @@ describe('LDClient', () => {
271307 } ) ;
272308 } ) ;
273309
274- describe ( 'waitUntilReady' , ( ) => {
275- it ( 'should resolve waitUntilReady promise when ready' , async ( ) => {
276- await withServers ( async baseConfig => {
277- await withClient ( user , baseConfig , async client => {
278- const gotReady = eventSink ( client , 'ready' ) ;
279-
280- await gotReady . take ( ) ;
281- await client . waitUntilReady ( ) ;
282- } ) ;
283- } ) ;
284- } ) ;
285- } ) ;
286-
287- describe ( 'waitForInitialization' , ( ) => {
288- it ( 'resolves promise on successful init' , async ( ) => {
289- await withServers ( async baseConfig => {
290- await withClient ( user , baseConfig , async client => {
291- const gotReady = eventSink ( client , 'ready' ) ;
292-
293- await gotReady . take ( ) ;
294- await client . waitForInitialization ( ) ;
295- } ) ;
296- } ) ;
297- } ) ;
298-
299- it ( 'rejects promise if flags request fails' , async ( ) => {
300- await withServers ( async ( baseConfig , pollServer ) => {
301- pollServer . byDefault ( respond ( 404 ) ) ;
302- await withClient ( user , baseConfig , async client => {
303- const err = new errors . LDInvalidEnvironmentIdError ( messages . environmentNotFound ( ) ) ;
304- await expect ( client . waitForInitialization ( ) ) . rejects . toThrow ( err ) ;
305- } ) ;
306- } ) ;
307- } ) ;
308- } ) ;
309-
310310 describe ( 'variation' , ( ) => {
311311 it ( 'returns value for an existing flag - from bootstrap' , async ( ) => {
312312 const config = {
0 commit comments