diff --git a/packages/sdk/browser/__tests__/BrowserDataManager.test.ts b/packages/sdk/browser/__tests__/BrowserDataManager.test.ts index 7a95f1edf..6e69ddd22 100644 --- a/packages/sdk/browser/__tests__/BrowserDataManager.test.ts +++ b/packages/sdk/browser/__tests__/BrowserDataManager.test.ts @@ -208,19 +208,27 @@ describe('given a BrowserDataManager with mocked dependencies', () => { it('should load cached flags and continue to poll to complete identify', async () => { const context = Context.fromLDContext({ kind: 'user', key: 'test-user' }); const identifyOptions: LDIdentifyOptions = { waitForNetworkResults: false }; - const identifyResolve = jest.fn(); - const identifyReject = jest.fn(); - flagManager.loadCached.mockResolvedValue(true); - await dataManager.identify(identifyResolve, identifyReject, context, identifyOptions); + let identifyResolve: () => void; + let identifyReject: (err: Error) => void; + const identifyResolveCalled = new Promise((resolve) => { + identifyResolve = jest.fn().mockImplementation(() => { + resolve(); + }); + identifyReject = jest.fn(); + + // this is the function under test + dataManager.identify(identifyResolve, identifyReject, context, identifyOptions); + }); + await identifyResolveCalled; expect(logger.debug).toHaveBeenCalledWith( '[BrowserDataManager] Identify - Flags loaded from cache. Continuing to initialize via a poll.', ); expect(flagManager.loadCached).toHaveBeenCalledWith(context); - expect(identifyResolve).toHaveBeenCalled(); + expect(identifyResolve!).toHaveBeenCalled(); expect(flagManager.init).toHaveBeenCalledWith( expect.anything(), expect.objectContaining({ flagA: { flag: true, version: undefined } }), @@ -231,19 +239,26 @@ describe('given a BrowserDataManager with mocked dependencies', () => { it('should identify from polling when there are no cached flags', async () => { const context = Context.fromLDContext({ kind: 'user', key: 'test-user' }); const identifyOptions: LDIdentifyOptions = { waitForNetworkResults: false }; - const identifyResolve = jest.fn(); - const identifyReject = jest.fn(); - flagManager.loadCached.mockResolvedValue(false); + let identifyResolve: () => void; + let identifyReject: (err: Error) => void; + const identifyResolveCalled = new Promise((resolve) => { + identifyResolve = jest.fn().mockImplementation(() => { + resolve(); + }); + identifyReject = jest.fn(); - await dataManager.identify(identifyResolve, identifyReject, context, identifyOptions); + // this is the function under test + dataManager.identify(identifyResolve, identifyReject, context, identifyOptions); + }); + await identifyResolveCalled; expect(logger.debug).not.toHaveBeenCalledWith( 'Identify - Flags loaded from cache. Continuing to initialize via a poll.', ); expect(flagManager.loadCached).toHaveBeenCalledWith(context); - expect(identifyResolve).toHaveBeenCalled(); + expect(identifyResolve!).toHaveBeenCalled(); expect(flagManager.init).toHaveBeenCalledWith( expect.anything(), expect.objectContaining({ flagA: { flag: true, version: undefined } }), diff --git a/packages/shared/sdk-client/src/DataManager.ts b/packages/shared/sdk-client/src/DataManager.ts index c9ad06a76..9b13e3ef5 100644 --- a/packages/shared/sdk-client/src/DataManager.ts +++ b/packages/shared/sdk-client/src/DataManager.ts @@ -113,18 +113,7 @@ export abstract class BaseDataManager implements DataManager { this.platform.requests, this.platform.encoding!, async (flags) => { - this.logger.debug(`Handling polling result: ${Object.keys(flags)}`); - - // mapping flags to item descriptors - const descriptors = Object.entries(flags).reduce( - (acc: { [k: string]: ItemDescriptor }, [key, flag]) => { - acc[key] = { version: flag.version, flag }; - return acc; - }, - {}, - ); - - await this.flagManager.init(checkedContext, descriptors); + await this.dataSourceEventHandler.handlePut(checkedContext, flags); identifyResolve?.(); }, (err) => {