Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 15 additions & 36 deletions test/src/tests-beaconUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,63 +45,52 @@ describe('Beacon Upload', () => {
fetchMock.restore();
});

it('should trigger beacon on page visibilitychange events', function(done) {
// http://go/j-SDKE-301
it('should trigger beacon on page visibilitychange events', async () => {
window.mParticle._resetForTests(MPConfig);

const bond = sinon.spy(navigator, 'sendBeacon');
window.mParticle.init(apiKey, window.mParticle.config);

waitForCondition(hasIdentifyReturned)
.then(() => {
await waitForCondition(hasIdentifyReturned);

// visibility change is a document property, not window
document.dispatchEvent(new Event('visibilitychange'));

bond.called.should.eql(true);
bond.lastCall.args[0].should.eql(urls.events);

done();
})
});

it('should trigger beacon on page beforeunload events', function(done) {
it('should trigger beacon on page beforeunload events', async () => {
window.mParticle._resetForTests(MPConfig);

const bond = sinon.spy(navigator, 'sendBeacon');
window.mParticle.init(apiKey, window.mParticle.config);

waitForCondition(hasIdentifyReturned)
.then(() => {
await waitForCondition(hasIdentifyReturned);

// karma fails if onbeforeunload is not set to null
window.onbeforeunload = null;
window.dispatchEvent(new Event('beforeunload'));

bond.called.should.eql(true);
bond.getCalls()[0].args[0].should.eql(urls.events);

done();
});
});

it('should trigger beacon on pagehide events', function(done) {
it('should trigger beacon on pagehide events', async () => {
window.mParticle._resetForTests(MPConfig);

const bond = sinon.spy(navigator, 'sendBeacon');
window.mParticle.init(apiKey, window.mParticle.config);

waitForCondition(hasIdentifyReturned)
.then(() => {
await waitForCondition(hasIdentifyReturned);

window.dispatchEvent(new Event('pagehide'));

bond.called.should.eql(true);
bond.getCalls()[0].args[0].should.eql(urls.events);

(typeof bond.getCalls()[0].args[1]).should.eql('object');

done();
});
});

describe('Offline Storage Enabled', () => {
Expand All @@ -124,13 +113,14 @@ describe('Beacon Upload', () => {
fetchMock.restore();
});

it('`visibilitychange` should purge events and batches from Offline Storage after dispatch', function(done) {
// http://go/j-SDKE-301
it('`visibilitychange` should purge events and batches from Offline Storage after dispatch', async () => {
const eventStorageKey = 'mprtcl-v4_abcdef-events';
const batchStorageKey = 'mprtcl-v4_abcdef-batches';
window.mParticle._resetForTests(MPConfig);
window.mParticle.init(apiKey, window.mParticle.config);
waitForCondition(hasIdentifyReturned)
.then(() => {
await waitForCondition(hasIdentifyReturned);

const mpInstance = window.mParticle.getInstance();
const uploader = mpInstance._APIClient.uploader;

Expand Down Expand Up @@ -174,19 +164,15 @@ describe('Beacon Upload', () => {
uploader.batchesQueuedForProcessing.length,
'Batch Queue should be empty after dispatch'
).to.equal(0);

done();
});
});

it('`beforeunload` should purge events and batches from Offline Storage after dispatch', function(done) {
it('`beforeunload` should purge events and batches from Offline Storage after dispatch', async () => {
const eventStorageKey = 'mprtcl-v4_abcdef-events';
const batchStorageKey = 'mprtcl-v4_abcdef-batches';

window.mParticle._resetForTests(MPConfig);
window.mParticle.init(apiKey, window.mParticle.config);
waitForCondition(hasIdentifyReturned)
.then(() => {
await waitForCondition(hasIdentifyReturned);

const mpInstance = window.mParticle.getInstance();
const uploader = mpInstance._APIClient.uploader;
Expand Down Expand Up @@ -231,19 +217,15 @@ describe('Beacon Upload', () => {
uploader.batchesQueuedForProcessing.length,
'Batch Queue should be empty after dispatch'
).to.equal(0);

done();
});
});

it('`pagehide` should purge events and batches from Offline Storage after dispatch', function(done) {
it('`pagehide` should purge events and batches from Offline Storage after dispatch', async () => {
const eventStorageKey = 'mprtcl-v4_abcdef-events';
const batchStorageKey = 'mprtcl-v4_abcdef-batches';

window.mParticle._resetForTests(MPConfig);
window.mParticle.init(apiKey, window.mParticle.config);
waitForCondition(hasIdentifyReturned)
.then(() => {
await waitForCondition(hasIdentifyReturned);

const mpInstance = window.mParticle.getInstance();
const uploader = mpInstance._APIClient.uploader;
Expand Down Expand Up @@ -288,9 +270,6 @@ describe('Beacon Upload', () => {
uploader.batchesQueuedForProcessing.length,
'Batch Queue should be empty after dispatch'
).to.equal(0);

done();
});
});
});
});
Loading