@@ -9,21 +9,65 @@ const logger: TestLogger = new TestLogger();
9
9
const testFlagKey = 'a-key' ;
10
10
describe ( 'LaunchDarklyClientProvider' , ( ) => {
11
11
let ldClient : LDClient ;
12
+ let ldProvider : LaunchDarklyClientProvider
12
13
let ofClient : Client ;
13
14
14
15
beforeAll ( ( ) => {
15
16
ldClient = {
16
17
variationDetail : jest . fn ( ) ,
17
18
waitUntilReady : jest . fn ( ) . mockResolvedValue ( { } ) ,
19
+ getContext : jest . fn ( ) . mockReturnValue ( { } ) ,
18
20
} as any ;
19
- OpenFeature . setProvider ( new LaunchDarklyClientProvider ( ldClient , { logger } ) ) ;
21
+ ldProvider = new LaunchDarklyClientProvider ( ldClient , { logger } ) ;
22
+ OpenFeature . setProvider ( ldProvider ) ;
20
23
ofClient = OpenFeature . getClient ( ) ;
21
24
} )
22
25
beforeEach ( ( ) => {
23
26
logger . reset ( ) ;
24
27
jest . clearAllMocks ( ) ;
25
28
} ) ;
26
29
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
+
27
71
describe ( 'resolveBooleanEvaluation' , ( ) => {
28
72
it ( 'calls the client correctly for boolean variations' , ( ) => {
29
73
// @ts -ignore we don't care about the arguments
0 commit comments