Skip to content

Commit cb4294c

Browse files
committed
Merge branch '2.x'
2 parents 4325689 + b419d8c commit cb4294c

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"check-typescript": "tsc",
3030
"prepublishOnly": "npm run build",
3131
"prettier": "prettier --write 'src/*.@(js|ts|tsx|json|css)'",
32-
"link-dev": "./link-dev.sh"
32+
"link-dev": "./link-dev.sh",
33+
"check": "npm i && npm run prettier && npm run lint && tsc && npm run test"
3334
},
3435
"repository": {
3536
"type": "git",

src/getFlagsProxy.test.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ import { LDClient, LDFlagSet } from 'launchdarkly-js-client-sdk';
22
import getFlagsProxy from './getFlagsProxy';
33
import { 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-
105
const 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+
2018
beforeEach(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+
2227
test('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

4247
test('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
});

src/getFlagsProxy.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,19 @@ function toFlagsProxy(ldClient: LDClient, flags: LDFlagSet, flagKeyMap: LDFlagKe
5858
// trap for reading a flag value using `LDClient#variation` to trigger an evaluation event
5959
get(target, prop, receiver) {
6060
const currentValue = Reflect.get(target, prop, receiver);
61-
if (typeof prop === 'symbol') {
61+
62+
// only process flag keys and ignore symbols and native Object functions
63+
if (typeof prop === 'symbol' || !hasFlag(flagKeyMap, prop)) {
6264
return currentValue;
6365
}
66+
6467
if (currentValue === undefined) {
6568
return;
6669
}
67-
const originalFlagKey = hasFlag(flagKeyMap, prop) ? flagKeyMap[prop] : prop;
68-
const nextValue = ldClient.variation(originalFlagKey, currentValue);
6970

70-
return nextValue;
71+
return ldClient.variation(flagKeyMap[prop], currentValue);
7172
},
73+
7274
// disable all mutation functions to make proxy readonly
7375
setPrototypeOf: () => false,
7476
set: () => false,

0 commit comments

Comments
 (0)