Skip to content

Commit

Permalink
fix: Flag store should not access values from prototype. (launchdarkl…
Browse files Browse the repository at this point in the history
…y#567)

This should be merged after the test organization PR.
  • Loading branch information
kinyoklion authored Sep 6, 2024
1 parent ed7b27a commit fca4d92
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { DefaultFlagStore } from '../../src/flag-manager/FlagStore';

describe('given an empty flag store', () => {
let store: DefaultFlagStore;

beforeEach(() => {
store = new DefaultFlagStore();
});

it.each(['unknown', 'toString', 'length'])(
'gets undefined for a feature that does not exist',
(key) => {
expect(store.get(key)).toBeUndefined();
},
);

it('can set and get key', () => {
store.insertOrUpdate('toString', {
version: 1,
flag: {
version: 1,
flagVersion: 1,
value: 'test-value',
variation: 0,
trackEvents: false,
},
});

expect(store.get('toString')?.flag.value).toEqual('test-value');
});

it('replaces flags on init', () => {
store.insertOrUpdate('potato', {
version: 1,
flag: {
version: 1,
flagVersion: 1,
value: 'test-value',
variation: 0,
trackEvents: false,
},
});

store.init({
newFlag: {
version: 1,
flag: {
version: 1,
flagVersion: 1,
value: 'new-test-value',
variation: 0,
trackEvents: false,
},
},
});

const all = store.getAll();
expect(Object.keys(all)).toEqual(['newFlag']);
});
});
5 changes: 4 additions & 1 deletion packages/shared/sdk-client/src/flag-manager/FlagStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export class DefaultFlagStore implements FlagStore {
}

get(key: string): ItemDescriptor | undefined {
return this.flags[key];
if (Object.prototype.hasOwnProperty.call(this.flags, key)) {
return this.flags[key];
}
return undefined;
}

getAll(): { [key: string]: ItemDescriptor } {
Expand Down

0 comments on commit fca4d92

Please sign in to comment.