@@ -51,6 +51,25 @@ describe('LDClientImpl', () => {
5151 expect ( callbacks . onError ) . not . toBeCalled ( ) ;
5252 } ) ;
5353
54+ it ( 'wait for initialization completes even if initialization completes before it is called' , ( done ) => {
55+ setupMockStreamingProcessor ( ) ;
56+ client = createClient ( ) ;
57+
58+ setTimeout ( async ( ) => {
59+ const initializedClient = await client . waitForInitialization ( ) ;
60+ expect ( initializedClient ) . toEqual ( client ) ;
61+ done ( ) ;
62+ } , 10 ) ;
63+ } ) ;
64+
65+ it ( 'waiting for initialization the second time produces the same result' , async ( ) => {
66+ client = createClient ( ) ;
67+ await client . waitForInitialization ( ) ;
68+
69+ const initializedClient = await client . waitForInitialization ( ) ;
70+ expect ( initializedClient ) . toEqual ( client ) ;
71+ } ) ;
72+
5473 it ( 'fires ready event in offline mode' , async ( ) => {
5574 client = createClient ( { offline : true } ) ;
5675 const initializedClient = await client . waitForInitialization ( ) ;
@@ -74,6 +93,29 @@ describe('LDClientImpl', () => {
7493 expect ( callbacks . onError ) . toBeCalled ( ) ;
7594 } ) ;
7695
96+ it ( 'initialization promise is rejected even if the failure happens before wait is called' , ( done ) => {
97+ setupMockStreamingProcessor ( true ) ;
98+ client = createClient ( ) ;
99+
100+ setTimeout ( async ( ) => {
101+ await expect ( client . waitForInitialization ( ) ) . rejects . toThrow ( 'failed' ) ;
102+
103+ expect ( client . initialized ( ) ) . toBeFalsy ( ) ;
104+ expect ( callbacks . onReady ) . not . toBeCalled ( ) ;
105+ expect ( callbacks . onFailed ) . toBeCalled ( ) ;
106+ expect ( callbacks . onError ) . toBeCalled ( ) ;
107+ done ( ) ;
108+ } , 10 ) ;
109+ } ) ;
110+
111+ it ( 'waiting a second time results in the same rejection' , async ( ) => {
112+ setupMockStreamingProcessor ( true ) ;
113+ client = createClient ( ) ;
114+
115+ await expect ( client . waitForInitialization ( ) ) . rejects . toThrow ( 'failed' ) ;
116+ await expect ( client . waitForInitialization ( ) ) . rejects . toThrow ( 'failed' ) ;
117+ } ) ;
118+
77119 it ( 'isOffline returns true in offline mode' , ( ) => {
78120 client = createClient ( { offline : true } ) ;
79121 expect ( client . isOffline ( ) ) . toEqual ( true ) ;
0 commit comments