diff --git a/packages/shared/sdk-client/__tests__/flag-manager/FlagStore.test.ts b/packages/shared/sdk-client/__tests__/flag-manager/FlagStore.test.ts new file mode 100644 index 000000000..8cb3389ec --- /dev/null +++ b/packages/shared/sdk-client/__tests__/flag-manager/FlagStore.test.ts @@ -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']); + }); +}); diff --git a/packages/shared/sdk-client/src/flag-manager/FlagStore.ts b/packages/shared/sdk-client/src/flag-manager/FlagStore.ts index d9ce91b11..f58959721 100644 --- a/packages/shared/sdk-client/src/flag-manager/FlagStore.ts +++ b/packages/shared/sdk-client/src/flag-manager/FlagStore.ts @@ -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 } {