@@ -2,11 +2,6 @@ import { LDClient, LDFlagSet } from 'launchdarkly-js-client-sdk';
22import getFlagsProxy from './getFlagsProxy' ;
33import { defaultReactOptions } from './types' ;
44
5- // tslint:disable-next-line: no-unsafe-any
6- const variation = jest . fn ( ( k : string ) : string | undefined => rawFlags [ k ] ) ;
7-
8- const ldClient = ( { variation } as unknown ) as LDClient ;
9-
105const rawFlags : LDFlagSet = {
116 'foo-bar' : 'foobar' ,
127 'baz-qux' : 'bazqux' ,
@@ -17,8 +12,18 @@ const camelizedFlags: LDFlagSet = {
1712 bazQux : 'bazqux' ,
1813} ;
1914
15+ // cast as unknown first to be able to partially mock ldClient
16+ const ldClient = ( { variation : jest . fn ( ( flagKey ) => rawFlags [ flagKey ] as string ) } as unknown ) as LDClient ;
17+
2018beforeEach ( jest . clearAllMocks ) ;
2119
20+ test ( 'native Object functions should be ignored' , ( ) => {
21+ const { flags } = getFlagsProxy ( ldClient , rawFlags ) ;
22+ flags . hasOwnProperty ( 'fooBar' ) ;
23+ flags . propertyIsEnumerable ( 'bazQux' ) ;
24+ expect ( ldClient . variation ) . not . toHaveBeenCalled ( ) ;
25+ } ) ;
26+
2227test ( 'camel cases keys' , ( ) => {
2328 const { flags } = getFlagsProxy ( ldClient , rawFlags ) ;
2429
@@ -31,12 +36,12 @@ test('does not camel cases keys', () => {
3136 expect ( flags ) . toEqual ( rawFlags ) ;
3237} ) ;
3338
34- test ( 'proxy calls variation on flag read' , ( ) => {
39+ test ( 'proxy calls ldClient. variation on flag read' , ( ) => {
3540 const { flags } = getFlagsProxy ( ldClient , rawFlags ) ;
3641
3742 expect ( flags . fooBar ) . toBe ( 'foobar' ) ;
3843
39- expect ( variation ) . toHaveBeenCalledWith ( 'foo-bar' , 'foobar' ) ;
44+ expect ( ldClient . variation ) . toHaveBeenCalledWith ( 'foo-bar' , 'foobar' ) ;
4045} ) ;
4146
4247test ( 'returns flag key map' , ( ) => {
@@ -56,5 +61,5 @@ test('does not use proxy if option is false', () => {
5661
5762 expect ( flags [ 'foo-bar' ] ) . toBe ( 'foobar' ) ;
5863
59- expect ( variation ) . not . toHaveBeenCalled ( ) ;
64+ expect ( ldClient . variation ) . not . toHaveBeenCalled ( ) ;
6065} ) ;
0 commit comments