diff --git a/src/__tests__/request-queue.js b/src/__tests__/request-queue.js index 468db91d7..3add271ba 100644 --- a/src/__tests__/request-queue.js +++ b/src/__tests__/request-queue.js @@ -83,4 +83,32 @@ describe('RequestQueue', () => { { transport: 'sendbeacon' } ) }) + + it('handles unload with batchKeys', () => { + given.queue.enqueue('/e', { event: 'foo', timestamp: 1_610_000_000 }, { transport: 'XHR' }) + given.queue.enqueue('/identify', { event: '$identify', timestamp: 1_620_000_000 }) + given.queue.enqueue('/e', { event: 'bar', timestamp: 1_630_000_000 }) + given.queue.enqueue('/e', { event: 'zeta', timestamp: 1_640_000_000 }, { _batchKey: 'sessionRecording' }) + + given.queue.unload() + + expect(given.handlePollRequest).toHaveBeenCalledTimes(3) + expect(given.handlePollRequest).toHaveBeenCalledWith( + '/e', + [ + { event: 'foo', timestamp: 1_610_000_000 }, + { event: 'bar', timestamp: 1_630_000_000 }, + ], + { transport: 'sendbeacon' } + ) + expect(given.handlePollRequest).toHaveBeenCalledWith( + '/identify', + [{ event: '$identify', timestamp: 1_620_000_000 }], + { transport: 'sendbeacon' } + ) + expect(given.handlePollRequest).toHaveBeenCalledWith('/e', [{ event: 'zeta', timestamp: 1_640_000_000 }], { + _batchKey: 'sessionRecording', + transport: 'sendbeacon', + }) + }) }) diff --git a/src/request-queue.js b/src/request-queue.js index f6f86a8f3..200623305 100644 --- a/src/request-queue.js +++ b/src/request-queue.js @@ -87,8 +87,8 @@ export class RequestQueue { clearTimeout(this._poller) const requests = this._event_queue.length > 0 ? this.formatQueue() : {} this._event_queue.length = 0 - for (let url in requests) { - const { data, options } = requests[url] + for (let key in requests) { + const { url, data, options } = requests[key] this.handlePollRequest(url, data, { ...options, transport: 'sendbeacon' }) } }