@@ -10,14 +10,6 @@ import * as mockResponseJson from './evaluation/mockResponse.json';
1010import LDClientImpl from './LDClientImpl' ;
1111import { Flags } from './types' ;
1212
13- let mockPlatform : ReturnType < typeof createBasicPlatform > ;
14- let logger : ReturnType < typeof createLogger > ;
15-
16- beforeEach ( ( ) => {
17- mockPlatform = createBasicPlatform ( ) ;
18- logger = createLogger ( ) ;
19- } ) ;
20-
2113jest . mock ( '@launchdarkly/js-sdk-common' , ( ) => {
2214 const actual = jest . requireActual ( '@launchdarkly/js-sdk-common' ) ;
2315 const actualMock = jest . requireActual ( '@launchdarkly/private-js-mocks' ) ;
@@ -49,11 +41,15 @@ const autoEnv = {
4941 os : { name : 'An OS' , version : '1.0.1' , family : 'orange' } ,
5042 } ,
5143} ;
52- let ldc : LDClientImpl ;
53- let defaultPutResponse : Flags ;
54-
5544describe ( 'sdk-client object' , ( ) => {
45+ let ldc : LDClientImpl ;
46+ let defaultPutResponse : Flags ;
47+ let mockPlatform : ReturnType < typeof createBasicPlatform > ;
48+ let logger : ReturnType < typeof createLogger > ;
49+
5650 beforeEach ( ( ) => {
51+ mockPlatform = createBasicPlatform ( ) ;
52+ logger = createLogger ( ) ;
5753 defaultPutResponse = clone < Flags > ( mockResponseJson ) ;
5854 setupMockStreamingProcessor ( false , defaultPutResponse ) ;
5955 mockPlatform . crypto . randomUUID . mockReturnValue ( 'random1' ) ;
@@ -97,6 +93,8 @@ describe('sdk-client object', () => {
9793 defaultPutResponse [ 'dev-test-flag' ] . value = false ;
9894 const carContext : LDContext = { kind : 'car' , key : 'test-car' } ;
9995
96+ mockPlatform . crypto . randomUUID . mockReturnValue ( 'random1' ) ;
97+
10098 await ldc . identify ( carContext ) ;
10199 const c = ldc . getContext ( ) ;
102100 const all = ldc . allFlags ( ) ;
@@ -161,6 +159,8 @@ describe('sdk-client object', () => {
161159 defaultPutResponse [ 'dev-test-flag' ] . value = false ;
162160 const carContext : LDContext = { kind : 'car' , anonymous : true , key : '' } ;
163161
162+ mockPlatform . crypto . randomUUID . mockReturnValue ( 'random1' ) ;
163+
164164 await ldc . identify ( carContext ) ;
165165 const c = ldc . getContext ( ) ;
166166 const all = ldc . allFlags ( ) ;
@@ -207,4 +207,46 @@ describe('sdk-client object', () => {
207207 expect ( emitter . listenerCount ( 'change' ) ) . toEqual ( 1 ) ;
208208 expect ( emitter . listenerCount ( 'error' ) ) . toEqual ( 1 ) ;
209209 } ) ;
210+
211+ test ( 'can complete identification using storage' , async ( ) => {
212+ const data : Record < string , string > = { } ;
213+ mockPlatform . storage . get . mockImplementation ( ( key ) => data [ key ] ) ;
214+ mockPlatform . storage . set . mockImplementation ( ( key : string , value : string ) => {
215+ data [ key ] = value ;
216+ } ) ;
217+ mockPlatform . storage . clear . mockImplementation ( ( key : string ) => {
218+ delete data [ key ] ;
219+ } ) ;
220+
221+ // First identify should populate storage.
222+ await ldc . identify ( context ) ;
223+
224+ expect ( logger . debug ) . not . toHaveBeenCalledWith ( 'Identify completing with cached flags' ) ;
225+
226+ // Second identify should use storage.
227+ await ldc . identify ( context ) ;
228+
229+ expect ( logger . debug ) . toHaveBeenCalledWith ( 'Identify completing with cached flags' ) ;
230+ } ) ;
231+
232+ test ( 'does not complete identify using storage when instructed to wait for the network response' , async ( ) => {
233+ const data : Record < string , string > = { } ;
234+ mockPlatform . storage . get . mockImplementation ( ( key ) => data [ key ] ) ;
235+ mockPlatform . storage . set . mockImplementation ( ( key : string , value : string ) => {
236+ data [ key ] = value ;
237+ } ) ;
238+ mockPlatform . storage . clear . mockImplementation ( ( key : string ) => {
239+ delete data [ key ] ;
240+ } ) ;
241+
242+ // First identify should populate storage.
243+ await ldc . identify ( context ) ;
244+
245+ expect ( logger . debug ) . not . toHaveBeenCalledWith ( 'Identify completing with cached flags' ) ;
246+
247+ // Second identify would use storage, but we instruct it not to.
248+ await ldc . identify ( context , { waitForNetworkResults : true , timeout : 5 } ) ;
249+
250+ expect ( logger . debug ) . not . toHaveBeenCalledWith ( 'Identify completing with cached flags' ) ;
251+ } ) ;
210252} ) ;
0 commit comments