@@ -2,20 +2,23 @@ import { OpenFeature, Client } from '@openfeature/js-sdk';
22import { LDClient } from 'launchdarkly-node-server-sdk' ;
33import { LaunchDarklyProvider } from '../src' ;
44import translateContext from '../src/translateContext' ;
5+ import TestLogger from './TestLogger' ;
56
67const basicContext = { targetingKey : 'the-key' } ;
78const testFlagKey = 'a-key' ;
89
910describe ( 'given a mock LaunchDarkly client' , ( ) => {
1011 let ldClient : LDClient ;
1112 let ofClient : Client ;
13+ const logger : TestLogger = new TestLogger ( ) ;
1214
1315 beforeEach ( ( ) => {
1416 ldClient = {
1517 variationDetail : jest . fn ( ) ,
1618 } as any ;
17- OpenFeature . setProvider ( new LaunchDarklyProvider ( ldClient ) ) ;
19+ OpenFeature . setProvider ( new LaunchDarklyProvider ( ldClient , { logger } ) ) ;
1820 ofClient = OpenFeature . getClient ( ) ;
21+ logger . reset ( ) ;
1922 } ) ;
2023
2124 it ( 'calls the client correctly for boolean variations' , async ( ) => {
@@ -27,11 +30,11 @@ describe('given a mock LaunchDarkly client', () => {
2730 } ) ) ;
2831 await ofClient . getBooleanDetails ( testFlagKey , false , basicContext ) ;
2932 expect ( ldClient . variationDetail )
30- . toHaveBeenCalledWith ( testFlagKey , translateContext ( basicContext ) , false ) ;
33+ . toHaveBeenCalledWith ( testFlagKey , translateContext ( logger , basicContext ) , false ) ;
3134 jest . clearAllMocks ( ) ;
3235 await ofClient . getBooleanValue ( testFlagKey , false , basicContext ) ;
3336 expect ( ldClient . variationDetail )
34- . toHaveBeenCalledWith ( testFlagKey , translateContext ( basicContext ) , false ) ;
37+ . toHaveBeenCalledWith ( testFlagKey , translateContext ( logger , basicContext ) , false ) ;
3538 } ) ;
3639
3740 it ( 'handles correct return types for boolean variations' , async ( ) => {
@@ -74,11 +77,11 @@ describe('given a mock LaunchDarkly client', () => {
7477 } ) ) ;
7578 await ofClient . getStringDetails ( testFlagKey , 'default' , basicContext ) ;
7679 expect ( ldClient . variationDetail )
77- . toHaveBeenCalledWith ( testFlagKey , translateContext ( basicContext ) , 'default' ) ;
80+ . toHaveBeenCalledWith ( testFlagKey , translateContext ( logger , basicContext ) , 'default' ) ;
7881 jest . clearAllMocks ( ) ;
7982 await ofClient . getStringValue ( testFlagKey , 'default' , basicContext ) ;
8083 expect ( ldClient . variationDetail )
81- . toHaveBeenCalledWith ( testFlagKey , translateContext ( basicContext ) , 'default' ) ;
84+ . toHaveBeenCalledWith ( testFlagKey , translateContext ( logger , basicContext ) , 'default' ) ;
8285 } ) ;
8386
8487 it ( 'handles correct return types for string variations' , async ( ) => {
@@ -121,11 +124,11 @@ describe('given a mock LaunchDarkly client', () => {
121124 } ) ) ;
122125 await ofClient . getNumberDetails ( testFlagKey , 0 , basicContext ) ;
123126 expect ( ldClient . variationDetail )
124- . toHaveBeenCalledWith ( testFlagKey , translateContext ( basicContext ) , 0 ) ;
127+ . toHaveBeenCalledWith ( testFlagKey , translateContext ( logger , basicContext ) , 0 ) ;
125128 jest . clearAllMocks ( ) ;
126129 await ofClient . getNumberValue ( testFlagKey , 0 , basicContext ) ;
127130 expect ( ldClient . variationDetail )
128- . toHaveBeenCalledWith ( testFlagKey , translateContext ( basicContext ) , 0 ) ;
131+ . toHaveBeenCalledWith ( testFlagKey , translateContext ( logger , basicContext ) , 0 ) ;
129132 } ) ;
130133
131134 it ( 'handles correct return types for numeric variations' , async ( ) => {
@@ -168,11 +171,11 @@ describe('given a mock LaunchDarkly client', () => {
168171 } ) ) ;
169172 await ofClient . getObjectDetails ( testFlagKey , { } , basicContext ) ;
170173 expect ( ldClient . variationDetail )
171- . toHaveBeenCalledWith ( testFlagKey , translateContext ( basicContext ) , { } ) ;
174+ . toHaveBeenCalledWith ( testFlagKey , translateContext ( logger , basicContext ) , { } ) ;
172175 jest . clearAllMocks ( ) ;
173176 await ofClient . getObjectValue ( testFlagKey , { } , basicContext ) ;
174177 expect ( ldClient . variationDetail )
175- . toHaveBeenCalledWith ( testFlagKey , translateContext ( basicContext ) , { } ) ;
178+ . toHaveBeenCalledWith ( testFlagKey , translateContext ( logger , basicContext ) , { } ) ;
176179 } ) ;
177180
178181 it ( 'handles correct return types for object variations' , async ( ) => {
@@ -246,4 +249,16 @@ describe('given a mock LaunchDarkly client', () => {
246249 reason : 'OFF' ,
247250 } ) ;
248251 } ) ;
252+
253+ it ( 'logs information about missing keys' , async ( ) => {
254+ await ofClient . getObjectDetails ( testFlagKey , { } , { } ) ;
255+ expect ( logger . logs [ 0 ] ) . toEqual ( "The EvaluationContext must contain either a 'targetingKey' "
256+ + "or a 'key' and the type must be a string." ) ;
257+ } ) ;
258+
259+ it ( 'logs information about double keys' , async ( ) => {
260+ await ofClient . getObjectDetails ( testFlagKey , { } , { targetingKey : '1' , key : '2' } ) ;
261+ expect ( logger . logs [ 0 ] ) . toEqual ( "The EvaluationContext contained both a 'targetingKey' and a"
262+ + " 'key' attribute. The 'key' attribute will be discarded." ) ;
263+ } ) ;
249264} ) ;
0 commit comments