1- import { LDClientImpl , LDOptions } from '../src' ;
1+ import { LDClientContext , LDStreamingError } from '@launchdarkly/js-sdk-common' ;
2+
3+ import { LDOptions } from '../src/api/options/LDOptions' ;
4+ import { LDFeatureStore } from '../src/api/subsystems/LDFeatureStore' ;
5+ import LDClientImpl from '../src/LDClientImpl' ;
26import { createBasicPlatform } from './createBasicPlatform' ;
37import TestLogger , { LogLevel } from './Logger' ;
4- import { MockStreamingProcessor , setupMockStreamingProcessor } from './streamingProcessor' ;
5-
6- jest . mock ( '@launchdarkly/js-sdk-common' , ( ) => {
7- const actual = jest . requireActual ( '@launchdarkly/js-sdk-common' ) ;
8- return {
9- ...actual ,
10- ...{
11- internal : {
12- ...actual . internal ,
13- StreamingProcessor : MockStreamingProcessor ,
14- } ,
15- } ,
16- } ;
17- } ) ;
8+
9+ function getUpdateProcessorFactory ( shouldError : boolean = false , initTimeoutMs : number = 0 ) {
10+ let initTimeoutHandle : any ;
11+ let patchTimeoutHandle : any ;
12+ let deleteTimeoutHandle : any ;
13+
14+ return (
15+ _clientContext : LDClientContext ,
16+ featureStore : LDFeatureStore ,
17+ initSuccessHandler : VoidFunction ,
18+ errorHandler ?: ( e : Error ) => void ,
19+ ) => ( {
20+ start : jest . fn ( async ( ) => {
21+ if ( shouldError ) {
22+ initTimeoutHandle = setTimeout ( ( ) => {
23+ const unauthorized = new Error ( 'test-error' ) as LDStreamingError ;
24+ // @ts -ignore
25+ unauthorized . code = 401 ;
26+ errorHandler ?.( unauthorized ) ;
27+ } , 0 ) ;
28+ } else {
29+ // execute put which will resolve the identify promise
30+ initTimeoutHandle = setTimeout ( ( ) => {
31+ featureStore . init ( { } , ( ) => { } ) ;
32+ initSuccessHandler ( ) ;
33+ } , initTimeoutMs ) ;
34+ }
35+ } ) ,
36+ close : jest . fn ( ( ) => {
37+ clearTimeout ( initTimeoutHandle ) ;
38+ clearTimeout ( patchTimeoutHandle ) ;
39+ clearTimeout ( deleteTimeoutHandle ) ;
40+ } ) ,
41+ eventSource : { } ,
42+ } ) ;
43+ }
1844
1945describe ( 'LDClientImpl' , ( ) => {
2046 let client : LDClientImpl ;
@@ -26,19 +52,15 @@ describe('LDClientImpl', () => {
2652 hasEventListeners : jest . fn ( ) . mockName ( 'hasEventListeners' ) ,
2753 } ;
2854 const createClient = ( options : LDOptions = { } ) =>
29- new LDClientImpl ( 'sdk-key' , createBasicPlatform ( ) , options , callbacks ) ;
30-
31- beforeEach ( ( ) => {
32- setupMockStreamingProcessor ( ) ;
33- } ) ;
55+ new LDClientImpl ( 'sdk-key-ldclientimpl.test' , createBasicPlatform ( ) , options , callbacks ) ;
3456
3557 afterEach ( ( ) => {
3658 client . close ( ) ;
3759 jest . resetAllMocks ( ) ;
3860 } ) ;
3961
4062 it ( 'fires ready event in online mode' , async ( ) => {
41- client = createClient ( ) ;
63+ client = createClient ( { updateProcessor : getUpdateProcessorFactory ( ) } ) ;
4264 const initializedClient = await client . waitForInitialization ( { timeout : 10 } ) ;
4365
4466 expect ( initializedClient ) . toEqual ( client ) ;
@@ -49,8 +71,7 @@ describe('LDClientImpl', () => {
4971 } ) ;
5072
5173 it ( 'wait for initialization completes even if initialization completes before it is called' , ( done ) => {
52- setupMockStreamingProcessor ( ) ;
53- client = createClient ( ) ;
74+ client = createClient ( { updateProcessor : getUpdateProcessorFactory ( ) } ) ;
5475
5576 setTimeout ( async ( ) => {
5677 const initializedClient = await client . waitForInitialization ( { timeout : 10 } ) ;
@@ -60,7 +81,7 @@ describe('LDClientImpl', () => {
6081 } ) ;
6182
6283 it ( 'waiting for initialization the second time produces the same result' , async ( ) => {
63- client = createClient ( ) ;
84+ client = createClient ( { updateProcessor : getUpdateProcessorFactory ( ) } ) ;
6485 await client . waitForInitialization ( { timeout : 10 } ) ;
6586
6687 const initializedClient = await client . waitForInitialization ( { timeout : 10 } ) ;
@@ -79,9 +100,7 @@ describe('LDClientImpl', () => {
79100 } ) ;
80101
81102 it ( 'initialization fails: failed event fires and initialization promise rejects' , async ( ) => {
82- setupMockStreamingProcessor ( true ) ;
83- client = createClient ( ) ;
84-
103+ client = createClient ( { updateProcessor : getUpdateProcessorFactory ( true ) } ) ;
85104 await expect ( client . waitForInitialization ( { timeout : 10 } ) ) . rejects . toThrow ( 'failed' ) ;
86105
87106 expect ( client . initialized ( ) ) . toBeFalsy ( ) ;
@@ -91,8 +110,7 @@ describe('LDClientImpl', () => {
91110 } ) ;
92111
93112 it ( 'initialization promise is rejected even if the failure happens before wait is called' , ( done ) => {
94- setupMockStreamingProcessor ( true ) ;
95- client = createClient ( ) ;
113+ client = createClient ( { updateProcessor : getUpdateProcessorFactory ( true ) } ) ;
96114
97115 setTimeout ( async ( ) => {
98116 await expect ( client . waitForInitialization ( { timeout : 10 } ) ) . rejects . toThrow ( 'failed' ) ;
@@ -106,8 +124,7 @@ describe('LDClientImpl', () => {
106124 } ) ;
107125
108126 it ( 'waiting a second time results in the same rejection' , async ( ) => {
109- setupMockStreamingProcessor ( true ) ;
110- client = createClient ( ) ;
127+ client = createClient ( { updateProcessor : getUpdateProcessorFactory ( true ) } ) ;
111128
112129 await expect ( client . waitForInitialization ( { timeout : 10 } ) ) . rejects . toThrow ( 'failed' ) ;
113130 await expect ( client . waitForInitialization ( { timeout : 10 } ) ) . rejects . toThrow ( 'failed' ) ;
@@ -124,31 +141,34 @@ describe('LDClientImpl', () => {
124141 } ) ;
125142
126143 it ( 'resolves immediately if the client is already ready' , async ( ) => {
127- client = createClient ( ) ;
144+ client = createClient ( { updateProcessor : getUpdateProcessorFactory ( ) } ) ;
128145 await client . waitForInitialization ( { timeout : 10 } ) ;
129146 await client . waitForInitialization ( { timeout : 10 } ) ;
130147 } ) ;
131148
132149 it ( 'creates only one Promise when waiting for initialization - when not using a timeout' , async ( ) => {
133- client = createClient ( ) ;
150+ client = createClient ( { updateProcessor : getUpdateProcessorFactory ( ) } ) ;
134151 const p1 = client . waitForInitialization ( ) ;
135152 const p2 = client . waitForInitialization ( ) ;
136153
137154 expect ( p2 ) . toBe ( p1 ) ;
138155 } ) ;
139156
140157 it ( 'rejects the returned promise when initialization does not complete within the timeout' , async ( ) => {
141- setupMockStreamingProcessor ( undefined , undefined , undefined , undefined , undefined , 10000 ) ;
142- client = createClient ( ) ;
158+ client = createClient ( {
159+ updateProcessor : getUpdateProcessorFactory ( false , 10000 ) ,
160+ } ) ;
143161 await expect ( async ( ) => client . waitForInitialization ( { timeout : 1 } ) ) . rejects . toThrow (
144162 'waitForInitialization timed out after 1 seconds.' ,
145163 ) ;
146164 } ) ;
147165
148166 it ( 'logs an error when the initialization does not complete within the timeout' , async ( ) => {
149- setupMockStreamingProcessor ( undefined , undefined , undefined , undefined , undefined , 10000 ) ;
150167 const logger = new TestLogger ( ) ;
151- client = createClient ( { logger } ) ;
168+ client = createClient ( {
169+ logger,
170+ updateProcessor : getUpdateProcessorFactory ( false , 10000 ) ,
171+ } ) ;
152172 try {
153173 await client . waitForInitialization ( { timeout : 1 } ) ;
154174 } catch {
@@ -163,14 +183,18 @@ describe('LDClientImpl', () => {
163183 } ) ;
164184
165185 it ( 'does not reject the returned promise when initialization completes within the timeout' , async ( ) => {
166- setupMockStreamingProcessor ( undefined , undefined , undefined , undefined , undefined , 1000 ) ;
167- client = createClient ( ) ;
168- await expect ( async ( ) => client . waitForInitialization ( { timeout : 5 } ) ) . not . toThrow ( ) ;
186+ client = createClient ( {
187+ updateProcessor : getUpdateProcessorFactory ( false , 100 ) ,
188+ } ) ;
189+ await expect ( client . waitForInitialization ( { timeout : 3 } ) ) . resolves . not . toThrow ( ) ;
169190 } ) ;
170191
171192 it ( 'logs when no timeout is set' , async ( ) => {
172193 const logger = new TestLogger ( ) ;
173- client = createClient ( { logger } ) ;
194+ client = createClient ( {
195+ logger,
196+ updateProcessor : getUpdateProcessorFactory ( ) ,
197+ } ) ;
174198 await client . waitForInitialization ( ) ;
175199 logger . expectMessages ( [
176200 {
@@ -183,7 +207,10 @@ describe('LDClientImpl', () => {
183207
184208 it ( 'logs when the timeout is too high' , async ( ) => {
185209 const logger = new TestLogger ( ) ;
186- client = createClient ( { logger } ) ;
210+ client = createClient ( {
211+ logger,
212+ updateProcessor : getUpdateProcessorFactory ( ) ,
213+ } ) ;
187214 await client . waitForInitialization ( { timeout : Number . MAX_SAFE_INTEGER } ) ;
188215
189216 logger . expectMessages ( [
@@ -199,7 +226,7 @@ describe('LDClientImpl', () => {
199226 'does not log when timeout is under high timeout threshold' ,
200227 async ( timeout ) => {
201228 const logger = new TestLogger ( ) ;
202- client = createClient ( { logger } ) ;
229+ client = createClient ( { logger, updateProcessor : getUpdateProcessorFactory ( ) } ) ;
203230 await client . waitForInitialization ( { timeout } ) ;
204231 expect ( logger . getCount ( LogLevel . Warn ) ) . toBe ( 0 ) ;
205232 } ,
0 commit comments