@@ -9,21 +9,65 @@ const logger: TestLogger = new TestLogger();
99const testFlagKey = 'a-key' ;
1010describe ( 'LaunchDarklyClientProvider' , ( ) => {
1111 let ldClient : LDClient ;
12+ let ldProvider : LaunchDarklyClientProvider
1213 let ofClient : Client ;
1314
1415 beforeAll ( ( ) => {
1516 ldClient = {
1617 variationDetail : jest . fn ( ) ,
1718 waitUntilReady : jest . fn ( ) . mockResolvedValue ( { } ) ,
19+ getContext : jest . fn ( ) . mockReturnValue ( { } ) ,
1820 } as any ;
19- OpenFeature . setProvider ( new LaunchDarklyClientProvider ( ldClient , { logger } ) ) ;
21+ ldProvider = new LaunchDarklyClientProvider ( ldClient , { logger } ) ;
22+ OpenFeature . setProvider ( ldProvider ) ;
2023 ofClient = OpenFeature . getClient ( ) ;
2124 } )
2225 beforeEach ( ( ) => {
2326 logger . reset ( ) ;
2427 jest . clearAllMocks ( ) ;
2528 } ) ;
2629
30+ describe ( 'initialize' , ( ) => {
31+ test ( 'should not update context if it did not change' , async ( ) => {
32+ const newContext = { kind : 'user' , targetingKey : 'test-key' } ;
33+ const oldContext = { kind : 'user' , key : 'test-key' } ;
34+ ldClient . getContext = jest . fn ( ) . mockReturnValue ( oldContext ) ;
35+ ldClient . identify = jest . fn ( ) . mockResolvedValue ( undefined ) ;
36+ await ldProvider . initialize ( newContext ) ;
37+ expect ( ldClient . identify ) . not . toHaveBeenCalled ( ) ;
38+ } ) ;
39+
40+ test ( 'should update context if it changed' , async ( ) => {
41+ const newContext = { kind : 'organization' , targetingKey : 'test-key' } ;
42+ const oldContext = { kind : 'user' , key : 'test-key' } ;
43+ ldClient . getContext = jest . fn ( ) . mockReturnValue ( oldContext ) ;
44+ ldClient . identify = jest . fn ( ) . mockResolvedValue ( undefined ) ;
45+ await ldProvider . initialize ( newContext ) ;
46+ expect ( ldClient . identify ) . toHaveBeenCalledTimes ( 1 ) ;
47+ expect ( ldClient . identify ) . toHaveBeenCalledWith ( translateContext ( logger , newContext ) ) ;
48+ } ) ;
49+
50+ test ( 'should not update context if anonymous and no key' , async ( ) => {
51+ const newContext = { anonymous : true } ;
52+ const oldContext = { kind : 'user' , anonymous : true , key : 'generated-key' } ;
53+ ldClient . getContext = jest . fn ( ) . mockReturnValue ( oldContext ) ;
54+ ldClient . identify = jest . fn ( ) . mockResolvedValue ( undefined ) ;
55+ await ldProvider . initialize ( newContext ) ;
56+ expect ( ldClient . identify ) . not . toHaveBeenCalled ( ) ;
57+ } ) ;
58+
59+ test ( 'should update context if anonymous and custom key' , async ( ) => {
60+ const newContext = { anonymous : true , key : 'custom-key' } ;
61+ const oldContext = { kind : 'user' , anonymous : true , key : 'generated-key' } ;
62+ ldClient . getContext = jest . fn ( ) . mockReturnValue ( oldContext ) ;
63+ ldClient . identify = jest . fn ( ) . mockResolvedValue ( undefined ) ;
64+ await ldProvider . initialize ( newContext ) ;
65+ expect ( ldClient . identify ) . toHaveBeenCalledTimes ( 1 ) ;
66+ expect ( ldClient . identify ) . toHaveBeenCalledWith ( translateContext ( logger , newContext ) ) ;
67+ } ) ;
68+
69+ } ) ;
70+
2771 describe ( 'resolveBooleanEvaluation' , ( ) => {
2872 it ( 'calls the client correctly for boolean variations' , ( ) => {
2973 // @ts -ignore we don't care about the arguments
0 commit comments