Skip to content

Commit

Permalink
Expose flushed callback
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed Apr 19, 2020
1 parent fa28c86 commit b89082a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/plug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Plug {
readonly tracker: TrackerFacade;
readonly user: UserFacade;
readonly session: SessionFacade;
readonly flushed: Promise<void>;

plug(configuration: Configuration): void;

Expand Down Expand Up @@ -52,6 +53,10 @@ class GlobalPlug implements Plug {
this.facade = SdkFacade.init(configuration);
}

public get flushed(): Promise<void> {
return this.tracker.flushed;
}

private get instance(): SdkFacade {
if (this.facade === undefined) {
throw new Error('Croct is not plugged in.');
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ describe('The Croct plug', () => {
expect(initialize).toBeCalledTimes(1);
});

test('should provide a callback that is called when the current pending events are flushed', async () => {
const config: SdkFacadeConfiguration = {appId: appId};

const sdkFacade = SdkFacade.init(config);

const flushed = jest.fn().mockResolvedValue(undefined);

Object.defineProperty(sdkFacade.tracker, 'flushed', {
get: flushed,
});

jest.spyOn(SdkFacade, 'init').mockReturnValue(sdkFacade);

croct.plug(config);

await expect(croct.flushed).resolves.toBeUndefined();

expect(flushed).toHaveBeenCalledTimes(1);
});

test('should provide a tracker facade', () => {
const config: SdkFacadeConfiguration = {appId: appId};
const sdkFacade = SdkFacade.init(config);
Expand Down

0 comments on commit b89082a

Please sign in to comment.