From e0ffcc64d36345da48ba3f73ddc72abfac2c3574 Mon Sep 17 00:00:00 2001 From: Marcos Passos Date: Sun, 19 Apr 2020 16:44:56 -0300 Subject: [PATCH] Renames `batch` callback to `flushed` and exposes it in tracker facade --- src/container.ts | 2 +- src/facade/trackerFacade.ts | 4 ++++ src/tracker.ts | 2 +- test/facade/trackerFacade.test.ts | 15 +++++++++++++++ test/tracker.test.ts | 6 +++--- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/container.ts b/src/container.ts index 57f0fe72..d8f5fe89 100644 --- a/src/container.ts +++ b/src/container.ts @@ -230,7 +230,7 @@ export class Container { this.tracker.suspend(); - await this.tracker.batch; + await this.tracker.flushed; } delete this.context; diff --git a/src/facade/trackerFacade.ts b/src/facade/trackerFacade.ts index e3c3483a..bfd2909a 100644 --- a/src/facade/trackerFacade.ts +++ b/src/facade/trackerFacade.ts @@ -58,6 +58,10 @@ export default class TrackerFacade { this.tracker = tracker; } + public get flushed(): Promise { + return this.tracker.flushed; + } + public enable(): void { this.tracker.enable(); } diff --git a/src/tracker.ts b/src/tracker.ts index af999203..fba6790a 100644 --- a/src/tracker.ts +++ b/src/tracker.ts @@ -75,7 +75,7 @@ export default class Tracker { this.trackInactivity = this.trackInactivity.bind(this); } - public get batch(): Promise { + public get flushed(): Promise { const suppress = (): void => { // suppress errors }; diff --git a/test/facade/trackerFacade.test.ts b/test/facade/trackerFacade.test.ts index 3685124a..eaacb751 100644 --- a/test/facade/trackerFacade.test.ts +++ b/test/facade/trackerFacade.test.ts @@ -27,6 +27,21 @@ describe('A tracker facade', () => { expect(tracker.disable).toHaveBeenCalledTimes(1); }); + test('should provide a callback that is called when the current pending events are flushed', async () => { + const tracker = jest.genMockFromModule('../../src/tracker'); + const batch = jest.fn().mockResolvedValue(undefined); + + Object.defineProperty(tracker, 'flushed', { + get: batch, + }); + + const trackerFacade = new TrackerFacade(tracker); + + await expect(trackerFacade.flushed).resolves.toBeUndefined(); + + expect(batch).toHaveBeenCalledTimes(1); + }); + test.each([ [ { diff --git a/test/tracker.test.ts b/test/tracker.test.ts index d79e77df..907a7930 100644 --- a/test/tracker.test.ts +++ b/test/tracker.test.ts @@ -1071,7 +1071,7 @@ describe('A tracker', () => { expect(channel.publish).toHaveBeenCalledTimes(1); }); - test('should provide a callback that is called when the current event batch is processed', async () => { + test('should provide a callback that is called when the current pending events are flushed', async () => { const publish = jest.fn(event => new Promise(resolve => setTimeout(() => resolve(event), 10))); const channel: OutputChannel = { @@ -1087,7 +1087,7 @@ describe('A tracker', () => { channel: channel, }); - await expect(tracker.batch).resolves.toBeUndefined(); + await expect(tracker.flushed).resolves.toBeUndefined(); const event: Event = { type: 'nothingChanged', @@ -1096,7 +1096,7 @@ describe('A tracker', () => { const promise = tracker.track(event); - await expect(tracker.batch).resolves.toBeUndefined(); + await expect(tracker.flushed).resolves.toBeUndefined(); expect(publish).toBeCalledWith({ timestamp: now,