|
| 1 | +import { AsyncStoreFacade, LDFeatureStore } from '@launchdarkly/js-server-sdk-common'; |
| 2 | + |
| 3 | +import { EdgeFeatureStore } from '../../src/api/EdgeFeatureStore'; |
| 4 | +import mockEdgeProvider from '../utils/mockEdgeProvider'; |
| 5 | +import * as testData from './testData.json'; |
| 6 | + |
| 7 | +describe('EdgeFeatureStore', () => { |
| 8 | + const clientSideId = 'client-side-id'; |
| 9 | + const kvKey = `LD-Env-${clientSideId}`; |
| 10 | + const mockLogger = { |
| 11 | + error: jest.fn(), |
| 12 | + warn: jest.fn(), |
| 13 | + info: jest.fn(), |
| 14 | + debug: jest.fn(), |
| 15 | + }; |
| 16 | + const mockGet = mockEdgeProvider.get as jest.Mock; |
| 17 | + let featureStore: LDFeatureStore; |
| 18 | + let asyncFeatureStore: AsyncStoreFacade; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + mockGet.mockImplementation(() => Promise.resolve(JSON.stringify(testData))); |
| 22 | + featureStore = new EdgeFeatureStore( |
| 23 | + mockEdgeProvider, |
| 24 | + clientSideId, |
| 25 | + 'MockEdgeProvider', |
| 26 | + mockLogger, |
| 27 | + ); |
| 28 | + asyncFeatureStore = new AsyncStoreFacade(featureStore); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + jest.resetAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('get', () => { |
| 36 | + it('can retrieve valid flag', async () => { |
| 37 | + const flag = await asyncFeatureStore.get({ namespace: 'features' }, 'testFlag1'); |
| 38 | + |
| 39 | + expect(mockGet).toHaveBeenCalledWith(kvKey); |
| 40 | + expect(flag).toMatchObject(testData.flags.testFlag1); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns undefined for invalid flag key', async () => { |
| 44 | + const flag = await asyncFeatureStore.get({ namespace: 'features' }, 'invalid'); |
| 45 | + |
| 46 | + expect(flag).toBeUndefined(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('can retrieve valid segment', async () => { |
| 50 | + const segment = await asyncFeatureStore.get({ namespace: 'segments' }, 'testSegment1'); |
| 51 | + |
| 52 | + expect(mockGet).toHaveBeenCalledWith(kvKey); |
| 53 | + expect(segment).toMatchObject(testData.segments.testSegment1); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns undefined for invalid segment key', async () => { |
| 57 | + const segment = await asyncFeatureStore.get({ namespace: 'segments' }, 'invalid'); |
| 58 | + |
| 59 | + expect(segment).toBeUndefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns null for invalid kv key', async () => { |
| 63 | + mockGet.mockImplementation(() => Promise.resolve(null)); |
| 64 | + const flag = await asyncFeatureStore.get({ namespace: 'features' }, 'testFlag1'); |
| 65 | + |
| 66 | + expect(flag).toBeNull(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('all', () => { |
| 71 | + it('can retrieve all flags', async () => { |
| 72 | + const flags = await asyncFeatureStore.all({ namespace: 'features' }); |
| 73 | + |
| 74 | + expect(mockGet).toHaveBeenCalledWith(kvKey); |
| 75 | + expect(flags).toMatchObject(testData.flags); |
| 76 | + }); |
| 77 | + |
| 78 | + it('can retrieve all segments', async () => { |
| 79 | + const segment = await asyncFeatureStore.all({ namespace: 'segments' }); |
| 80 | + |
| 81 | + expect(mockGet).toHaveBeenCalledWith(kvKey); |
| 82 | + expect(segment).toMatchObject(testData.segments); |
| 83 | + }); |
| 84 | + |
| 85 | + it('returns empty object for invalid DataKind', async () => { |
| 86 | + const flag = await asyncFeatureStore.all({ namespace: 'InvalidDataKind' }); |
| 87 | + |
| 88 | + expect(flag).toEqual({}); |
| 89 | + }); |
| 90 | + |
| 91 | + it('returns empty object for invalid kv key', async () => { |
| 92 | + mockGet.mockImplementation(() => Promise.resolve(null)); |
| 93 | + const segment = await asyncFeatureStore.all({ namespace: 'segments' }); |
| 94 | + |
| 95 | + expect(segment).toEqual({}); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('initialized', () => { |
| 100 | + it('returns true when initialized', async () => { |
| 101 | + const isInitialized = await asyncFeatureStore.initialized(); |
| 102 | + |
| 103 | + expect(mockGet).toHaveBeenCalledWith(kvKey); |
| 104 | + expect(isInitialized).toBeTruthy(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('returns false when not initialized', async () => { |
| 108 | + mockGet.mockImplementation(() => Promise.resolve(null)); |
| 109 | + const isInitialized = await asyncFeatureStore.initialized(); |
| 110 | + |
| 111 | + expect(mockGet).toHaveBeenCalledWith(kvKey); |
| 112 | + expect(isInitialized).toBeFalsy(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('init & getDescription', () => { |
| 117 | + it('can initialize', (done) => { |
| 118 | + const cb = jest.fn(() => { |
| 119 | + done(); |
| 120 | + }); |
| 121 | + featureStore.init(testData, cb); |
| 122 | + }); |
| 123 | + |
| 124 | + it('can retrieve description', async () => { |
| 125 | + const description = featureStore.getDescription?.(); |
| 126 | + |
| 127 | + expect(description).toEqual('MockEdgeProvider'); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments