Skip to content

Commit

Permalink
fixing missed merge logic and fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tanderson-ld committed Sep 26, 2024
1 parent a2787a6 commit ae3f473
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
35 changes: 25 additions & 10 deletions packages/sdk/browser/__tests__/BrowserDataManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((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 } }),
Expand All @@ -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<void>((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 } }),
Expand Down
13 changes: 1 addition & 12 deletions packages/shared/sdk-client/src/DataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit ae3f473

Please sign in to comment.