From 9e32dcefcb2c2c7ddb8658787b9b5e41adeebbb5 Mon Sep 17 00:00:00 2001 From: Robert Ing Date: Fri, 20 Oct 2023 17:05:32 -0400 Subject: [PATCH] tests: Update tests to support latest fetch-mock version --- test/src/tests-apiClient.ts | 1 - test/src/tests-batchUploader.ts | 137 ++++++++------- test/src/tests-beaconUpload.ts | 11 +- test/src/tests-consent.ts | 9 +- test/src/tests-cookie-syncing.js | 3 +- test/src/tests-core-sdk.js | 73 ++++---- test/src/tests-eCommerce.js | 75 ++++---- test/src/tests-event-logging.js | 161 ++++++++--------- test/src/tests-forwarders.js | 60 ++++--- test/src/tests-identities-attributes.js | 175 ++++++++++--------- test/src/tests-identity.js | 43 ++--- test/src/tests-main.js | 56 +++--- test/src/tests-mparticle-instance-manager.js | 37 ++-- test/src/tests-persistence.ts | 9 +- test/src/tests-self-hosting-specific.js | 13 +- test/src/utils.js | 4 +- tsconfig.json | 4 +- 17 files changed, 442 insertions(+), 429 deletions(-) diff --git a/test/src/tests-apiClient.ts b/test/src/tests-apiClient.ts index 576c155a..bf271027 100644 --- a/test/src/tests-apiClient.ts +++ b/test/src/tests-apiClient.ts @@ -7,7 +7,6 @@ import { expect } from 'chai'; declare global { interface Window { mParticle: MParticleWebSDK; - fetchMock: any; } } diff --git a/test/src/tests-batchUploader.ts b/test/src/tests-batchUploader.ts index 1f7273e3..165b0640 100644 --- a/test/src/tests-batchUploader.ts +++ b/test/src/tests-batchUploader.ts @@ -14,10 +14,12 @@ import { expect } from 'chai'; import _BatchValidator from '../../src/mockBatchCreator'; import Logger from '../../src/logger.js'; import { event0, event1, event2, event3 } from '../fixtures/events'; +import fetchMock from 'fetch-mock/esm/client'; + declare global { interface Window { mParticle: MParticleWebSDK; - fetchMock: any; + // fetchMock: any; } } @@ -30,7 +32,7 @@ describe('batch uploader', () => { let clock; beforeEach(() => { - window.fetchMock.restore(); + fetchMock.restore(); mockServer = sinon.createFakeServer(); mockServer.respondImmediately = true; @@ -43,7 +45,7 @@ describe('batch uploader', () => { afterEach(() => { mockServer.reset(); - window.fetchMock.restore(); + fetchMock.restore(); window.localStorage.clear(); }); @@ -129,11 +131,11 @@ describe('batch uploader', () => { window.mParticle.init(apiKey, window.mParticle.config); }); afterEach(() => { - window.fetchMock.restore(); + fetchMock.restore(); }); it('should reject batches without events', async () => { - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); const newLogger = new Logger(window.mParticle.config); const mpInstance = window.mParticle.getInstance(); @@ -157,11 +159,11 @@ describe('batch uploader', () => { [actualBatch, eventlessBatch], false ); - - expect(window.fetchMock.calls().length).to.equal(1); + debugger; + expect(fetchMock.calls().length).to.equal(1); const actualBatchResult = JSON.parse( - window.fetchMock.calls()[0][1].body + fetchMock.calls()[0][1].body as string ); expect(actualBatchResult.events.length).to.equal(1); @@ -169,7 +171,7 @@ describe('batch uploader', () => { }); it('should return batches that fail to upload with 500 errors', async () => { - window.fetchMock.post(urls.events, 500); + fetchMock.post(urls.events, 500); const newLogger = new Logger(window.mParticle.config); const mpInstance = window.mParticle.getInstance(); @@ -216,7 +218,7 @@ describe('batch uploader', () => { }); it('should return batches that fail to upload with 429 errors', async () => { - window.fetchMock.post(urls.events, 429); + fetchMock.post(urls.events, 429); const newLogger = new Logger(window.mParticle.config); const mpInstance = window.mParticle.getInstance(); @@ -263,7 +265,7 @@ describe('batch uploader', () => { }); it('should return null if batches fail to upload with 401 errors', async () => { - window.fetchMock.post(urls.events, 401); + fetchMock.post(urls.events, 401); const newLogger = new Logger(window.mParticle.config); const mpInstance = window.mParticle.getInstance(); @@ -297,7 +299,7 @@ describe('batch uploader', () => { }); it('should return batches that fail to unknown HTTP errors', async () => { - window.fetchMock.post(urls.events, 400); + fetchMock.post(urls.events, 400); const newLogger = new Logger(window.mParticle.config); const mpInstance = window.mParticle.getInstance(); @@ -350,7 +352,7 @@ describe('batch uploader', () => { describe('Offline Storage Feature Flag', () => { beforeEach(() => { sinon.restore(); - window.fetchMock.restore(); + fetchMock.restore(); mockServer.reset(); window.localStorage.clear(); @@ -358,7 +360,7 @@ describe('batch uploader', () => { }); afterEach(() => { sinon.restore(); - window.fetchMock.restore(); + fetchMock.restore(); mockServer.reset(); window.localStorage.clear(); @@ -486,7 +488,7 @@ describe('batch uploader', () => { afterEach(() => { sinon.restore(); window.localStorage.clear(); - window.fetchMock.restore(); + fetchMock.restore(); clock.restore(); }); @@ -540,7 +542,7 @@ describe('batch uploader', () => { clock = sinon.useFakeTimers({ now: new Date().getTime(), }); - window.fetchMock.restore(); + fetchMock.restore(); window.sessionStorage.clear(); window.localStorage.clear(); @@ -548,7 +550,7 @@ describe('batch uploader', () => { afterEach(() => { sinon.restore(); - window.fetchMock.restore(); + fetchMock.restore(); clock.restore(); }); @@ -590,7 +592,7 @@ describe('batch uploader', () => { it('should purge events from Session Storage upon Batch Creation', (done) => { const eventStorageKey = 'mprtcl-v4_abcdef-events'; - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); window.mParticle._resetForTests(MPConfig); window.mParticle.init(apiKey, window.mParticle.config); @@ -639,7 +641,7 @@ describe('batch uploader', () => { it('should save batches in sequence to Local Storage when an HTTP 500 error is encountered', (done) => { const batchStorageKey = 'mprtcl-v4_abcdef-batches'; - window.fetchMock.post(urls.events, 500); + fetchMock.post(urls.events, 500); window.mParticle._resetForTests(MPConfig); window.mParticle.init(apiKey, window.mParticle.config); @@ -696,7 +698,7 @@ describe('batch uploader', () => { it('should save batches in sequence to Local Storage when an HTTP 429 error is encountered', (done) => { const batchStorageKey = 'mprtcl-v4_abcdef-batches'; - window.fetchMock.post(urls.events, 429); + fetchMock.post(urls.events, 429); window.mParticle._resetForTests(MPConfig); window.mParticle.init(apiKey, window.mParticle.config); @@ -751,7 +753,7 @@ describe('batch uploader', () => { // Offline Storage afterwards const batchStorageKey = 'mprtcl-v4_abcdef-batches'; - window.fetchMock.post(urls.events, 401); + fetchMock.post(urls.events, 401); window.mParticle._resetForTests(MPConfig); window.mParticle.init(apiKey, window.mParticle.config); @@ -787,13 +789,13 @@ describe('batch uploader', () => { const batchStorageKey = 'mprtcl-v4_abcdef-batches'; // First upload is successful - window.fetchMock.post(urls.events, 200, { + fetchMock.post(urls.events, 200, { overwriteRoutes: false, repeat: 1, }); // Second upload is rate limited - window.fetchMock.post(urls.events, 429, { + fetchMock.post(urls.events, 429, { overwriteRoutes: false, }); @@ -932,7 +934,7 @@ describe('batch uploader', () => { const batchStorageKey = 'mprtcl-v4_abcdef-batches'; - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); // Set up SDK and Uploader window.mParticle._resetForTests(MPConfig); @@ -999,19 +1001,19 @@ describe('batch uploader', () => { // To verify the sequence, we should look at what has been uploaded // as the upload queue and Offline Storage should be empty - expect(window.fetchMock.calls().length).to.equal(4); + expect(fetchMock.calls().length).to.equal(4); const uploadedBatch1: Batch = JSON.parse( - window.fetchMock.calls()[0][1].body + fetchMock.calls()[0][1].body as string ); const uploadedBatch2: Batch = JSON.parse( - window.fetchMock.calls()[1][1].body + fetchMock.calls()[1][1].body as string ); const uploadedBatch3: Batch = JSON.parse( - window.fetchMock.calls()[2][1].body + fetchMock.calls()[2][1].body as string ); const uploadedBatch4: Batch = JSON.parse( - window.fetchMock.calls()[3][1].body + fetchMock.calls()[3][1].body as string ); // The following assertions should verify the sequence presented below @@ -1079,7 +1081,7 @@ describe('batch uploader', () => { }); afterEach(() => { - window.fetchMock.restore(); + fetchMock.restore(); clock.restore(); }); @@ -1087,8 +1089,8 @@ describe('batch uploader', () => { // Batches should be uploaded in the order they were created to prevent // any potential corruption. - window.fetchMock.post(urls.events, 200); - window.fetchMock.config.overwriteRoutes = true; + fetchMock.post(urls.events, 200); + fetchMock.config.overwriteRoutes = true; window.mParticle.config.flags = { ...enableBatchingConfigFlags, @@ -1103,11 +1105,11 @@ describe('batch uploader', () => { window.mParticle.upload(); expect( - window.fetchMock.called(), + fetchMock.called(), 'FetchMock should have been called' ).to.equal(true); - const batch1 = JSON.parse(window.fetchMock._calls[0][1].body); + const batch1 = JSON.parse(fetchMock.calls()[0][1].body as string); // Batch 1 should contain only session start, AST and a single event // in this exact order @@ -1126,7 +1128,7 @@ describe('batch uploader', () => { // Manually initiate the upload process - turn event into batches and upload the batch window.mParticle.upload(); - const batch2 = JSON.parse(window.fetchMock._calls[1][1].body); + const batch2 = JSON.parse(fetchMock.calls()[1][1].body as string); // Batch 2 should contain three custom events expect(batch2.events.length).to.equal(3); @@ -1141,7 +1143,7 @@ describe('batch uploader', () => { // Manually initiate the upload process - turn event into batches and upload the batch window.mParticle.upload(); - const batch3 = JSON.parse(window.fetchMock._calls[2][1].body); + const batch3 = JSON.parse(fetchMock.calls()[2][1].body as string); // Batch 3 should contain two custom events expect(batch3.events.length).to.equal(2); @@ -1157,7 +1159,7 @@ describe('batch uploader', () => { // If batches cannot upload, they should be added back to the Batch Queue // in the order they were created so they can be retransmitted. - window.fetchMock.post(urls.events, 500); + fetchMock.post(urls.events, 500); window.mParticle.config.flags = { ...enableBatchingConfigFlags, @@ -1174,11 +1176,11 @@ describe('batch uploader', () => { window.mParticle.upload(); expect( - window.fetchMock.called(), + fetchMock.called(), 'FetchMock should have been called' ).to.equal(true); - const batch1 = JSON.parse(window.fetchMock._calls[0][1].body); + const batch1 = JSON.parse(fetchMock.calls()[0][1].body as string); // Batch 1 should contain only session start, AST and a single event // in this exact order @@ -1251,12 +1253,12 @@ describe('batch uploader', () => { // https://go.mparticle.com/work/SQDSDKS-5165 it.skip('should keep and retry batches in sequence if the transmission fails midway', (done) => { // First request is successful, subsequent requests fail - window.fetchMock.post(urls.events, 200, { + fetchMock.post(urls.events, 200, { overwriteRoutes: false, repeat: 1, }); - window.fetchMock.post(urls.events, 429, { + fetchMock.post(urls.events, 429, { overwriteRoutes: false, }); @@ -1296,7 +1298,7 @@ describe('batch uploader', () => { setTimeout(() => { // Batch upload should be triggered 3 times, but only // 2 should be waiting for retry - expect(window.fetchMock.calls().length).to.equal(3); + expect(fetchMock.calls().length).to.equal(3); const batchQueue = window.mParticle.getInstance()._APIClient.uploader @@ -1331,8 +1333,8 @@ describe('batch uploader', () => { describe('batching via window.fetch', () => { beforeEach(() => { - window.fetchMock.post(urls.events, 200); - window.fetchMock.config.overwriteRoutes = true; + fetchMock.post(urls.events, 200); + fetchMock.config.overwriteRoutes = true; clock = sinon.useFakeTimers({now: new Date().getTime()}); window.mParticle.config.flags = { @@ -1341,12 +1343,13 @@ describe('batch uploader', () => { }); afterEach(() => { - window.fetchMock.restore(); + fetchMock.restore(); sinon.restore(); clock.restore(); }); it('should use custom v3 endpoint', function(done) { + // debugger; window.mParticle._resetForTests(MPConfig); window.mParticle.init(apiKey, window.mParticle.config); @@ -1359,9 +1362,9 @@ describe('batch uploader', () => { // Tick forward 1000 ms to hit upload interval and force an upload clock.tick(1000); - const lastCall = window.fetchMock.lastCall(); + const lastCall = fetchMock.lastCall(); const endpoint = lastCall[0]; - const batch = JSON.parse(window.fetchMock.lastCall()[1].body); + const batch = JSON.parse(fetchMock.lastCall()[1].body as string); endpoint.should.equal(urls.events); batch.events[0].event_type.should.equal('session_start'); @@ -1381,9 +1384,9 @@ describe('batch uploader', () => { clock.tick(1000); // Tick forward 1000 ms to hit upload interval and force an upload - const lastCall = window.fetchMock.lastCall(); + const lastCall = fetchMock.lastCall(); const endpoint = lastCall[0]; - const batch = JSON.parse(window.fetchMock.lastCall()[1].body); + const batch = JSON.parse(fetchMock.lastCall()[1].body as string); endpoint.should.equal(urls.events); batch.events[2].data.location.should.have.property('latitude', 100) batch.events[2].data.location.should.have.property('longitude', 100) @@ -1402,14 +1405,14 @@ describe('batch uploader', () => { mockServer.requests.length.should.equal(1); // no calls made to fetch yet - (window.fetchMock.lastCall() === undefined).should.equal(true) + (fetchMock.lastCall() === undefined).should.equal(true) // force upload, triggering window.fetch window.mParticle.upload(); - const lastCall = window.fetchMock.lastCall(); + const lastCall = fetchMock.lastCall(); const endpoint = lastCall[0]; - const batch = JSON.parse(window.fetchMock.lastCall()[1].body); + const batch = JSON.parse(fetchMock.lastCall()[1].body as string); endpoint.should.equal(urls.events); batch.events[0].event_type.should.equal('session_start'); @@ -1432,9 +1435,9 @@ describe('batch uploader', () => { var product1 = window.mParticle.eCommerce.createProduct('iphone', 'iphoneSKU', 999); window.mParticle.eCommerce.logProductAction(SDKProductActionType.AddToCart, product1); - const lastCall = window.fetchMock.lastCall(); + const lastCall = fetchMock.lastCall(); const endpoint = lastCall[0]; - const batch = JSON.parse(window.fetchMock.lastCall()[1].body); + const batch = JSON.parse(fetchMock.lastCall()[1].body as string); endpoint.should.equal(urls.events); batch.events[0].event_type.should.equal('session_start'); @@ -1451,7 +1454,7 @@ describe('batch uploader', () => { window.mParticle._resetForTests(MPConfig); var clock = sinon.useFakeTimers(); - window.fetchMock.post(urls.events, 500); + fetchMock.post(urls.events, 500); window.mParticle.init(apiKey, window.mParticle.config); window.mParticle.logEvent('Test Event'); @@ -1463,15 +1466,15 @@ describe('batch uploader', () => { pendingEvents[1].EventName.should.equal(10); pendingEvents[2].EventName.should.equal('Test Event'); - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); - (window.fetchMock.lastCall() === undefined).should.equal(true); + (fetchMock.lastCall() === undefined).should.equal(true); clock.tick(1000); let nowPendingEvents = window.mParticle.getInstance()._APIClient.uploader.eventsQueuedForProcessing; nowPendingEvents.length.should.equal(0); - const batch = JSON.parse(window.fetchMock.lastCall()[1].body); + const batch = JSON.parse(fetchMock.lastCall()[1].body as string); batch.events[0].event_type.should.equal('session_start'); batch.events[1].event_type.should.equal('application_state_transition'); batch.events[2].event_type.should.equal('custom_event'); @@ -1489,9 +1492,9 @@ describe('batch uploader', () => { // Tick forward 1000 ms to hit upload interval and force an upload clock.tick(1000); - const lastCall = window.fetchMock.lastCall(); + const lastCall = fetchMock.lastCall(); const endpoint = lastCall[0]; - const batch = JSON.parse(window.fetchMock.lastCall()[1].body); + const batch = JSON.parse(fetchMock.lastCall()[1].body as string); endpoint.should.equal(urls.events); batch.events[0].data.should.have.property('source_message_id') @@ -1519,9 +1522,9 @@ describe('batch uploader', () => { // Tick forward 1000 ms to hit upload interval and force an upload clock.tick(1000); - const lastCall = window.fetchMock.lastCall(); + const lastCall = fetchMock.lastCall(); const endpoint = lastCall[0]; - const batch = JSON.parse(window.fetchMock.lastCall()[1].body); + const batch = JSON.parse(fetchMock.lastCall()[1].body as string); endpoint.should.equal(urls.events); // event batch includes session start, ast, then last event is Test Event @@ -1578,11 +1581,11 @@ describe('batch uploader', () => { clock.restore(); setTimeout(function() { - const batch1 = JSON.parse(window.fetchMock._calls[0][1].body); - const batch2 = JSON.parse(window.fetchMock._calls[1][1].body); - const batch3 = JSON.parse(window.fetchMock._calls[2][1].body); - const batch4 = JSON.parse(window.fetchMock._calls[3][1].body); - const batch5 = JSON.parse(window.fetchMock._calls[4][1].body); + const batch1 = JSON.parse(fetchMock.calls()[0][1].body as string); + const batch2 = JSON.parse(fetchMock.calls()[1][1].body as string); + const batch3 = JSON.parse(fetchMock.calls()[2][1].body as string); + const batch4 = JSON.parse(fetchMock.calls()[3][1].body as string); + const batch5 = JSON.parse(fetchMock.calls()[4][1].body as string); // UAC event expect(batch1.events.length, 'Batch 1: UAC event').to.equal(1); diff --git a/test/src/tests-beaconUpload.ts b/test/src/tests-beaconUpload.ts index 7ba097fe..608adba8 100644 --- a/test/src/tests-beaconUpload.ts +++ b/test/src/tests-beaconUpload.ts @@ -1,4 +1,5 @@ import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { expect } from 'chai'; import { urls } from './config'; import { apiKey, MPConfig, testMPID } from './config'; @@ -23,12 +24,12 @@ let clock; describe('Beacon Upload', () => { let mockServer; before(function() { - window.fetchMock.restore(); + fetchMock.restore(); sinon.restore(); }); beforeEach(function() { - window.fetchMock.restore(); + fetchMock.restore(); mockServer = sinon.createFakeServer(); mockServer.respondImmediately = true; @@ -46,7 +47,7 @@ describe('Beacon Upload', () => { afterEach(() => { sinon.restore(); mockServer.reset(); - window.fetchMock.restore(); + fetchMock.restore(); }); it('should trigger beacon on page visibilitychange events', function(done) { @@ -106,7 +107,7 @@ describe('Beacon Upload', () => { clock = sinon.useFakeTimers({ now: new Date().getTime(), }); - window.fetchMock.restore(); + fetchMock.restore(); window.sessionStorage.clear(); window.localStorage.clear(); @@ -114,7 +115,7 @@ describe('Beacon Upload', () => { afterEach(() => { sinon.restore(); - window.fetchMock.restore(); + fetchMock.restore(); clock.restore(); }); diff --git a/test/src/tests-consent.ts b/test/src/tests-consent.ts index ad465be2..a28364ac 100644 --- a/test/src/tests-consent.ts +++ b/test/src/tests-consent.ts @@ -1,5 +1,6 @@ import Utils from './utils'; import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { urls } from './config'; import { apiKey, MPConfig, testMPID } from './config'; import { MParticleWebSDK } from '../../src/sdkRuntimeModels'; @@ -28,7 +29,7 @@ const mParticle = window.mParticle; describe('Consent', function() { beforeEach(function() { - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); mockServer = sinon.createFakeServer(); mockServer.respondImmediately = true; @@ -42,7 +43,7 @@ describe('Consent', function() { afterEach(function() { mockServer.restore(); - window.fetchMock.restore(); + fetchMock.restore(); mParticle._resetForTests(MPConfig); }); @@ -646,7 +647,7 @@ describe('Consent', function() { user.setConsentState(consentState); mParticle.logEvent('Test Event'); - const testEvent = findBatch(window.fetchMock._calls, 'Test Event'); + const testEvent = findBatch(fetchMock.calls(), 'Test Event'); testEvent.should.have.property('consent_state'); testEvent.consent_state.should.have.property('ccpa'); @@ -685,7 +686,7 @@ describe('Consent', function() { mParticle.logEvent('Test Event'); - const testEvent = findBatch(window.fetchMock._calls, 'Test Event'); + const testEvent = findBatch(fetchMock.calls(), 'Test Event'); testEvent.should.have.property('consent_state'); testEvent.consent_state.should.have.property('ccpa'); diff --git a/test/src/tests-cookie-syncing.js b/test/src/tests-cookie-syncing.js index 9601e2fd..6fea0cb7 100644 --- a/test/src/tests-cookie-syncing.js +++ b/test/src/tests-cookie-syncing.js @@ -1,5 +1,6 @@ import Utils from './utils'; import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { urls, testMPID, MPConfig, v4LSKey, apiKey } from './config'; const { setLocalStorage, MockForwarder, getLocalStorage } = Utils; @@ -56,7 +57,7 @@ describe('cookie syncing', function() { JSON.stringify({ mpid: testMPID, is_logged_in: false }), ]); - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); mParticle.init(apiKey, window.mParticle.config); }); diff --git a/test/src/tests-core-sdk.js b/test/src/tests-core-sdk.js index bca6adb9..e667fff6 100644 --- a/test/src/tests-core-sdk.js +++ b/test/src/tests-core-sdk.js @@ -8,7 +8,6 @@ import { apiKey, das, MPConfig, testMPID, workspaceCookieName } from './config'; const DefaultConfig = Constants.DefaultConfig, setLocalStorage = Utils.setLocalStorage, - findRequest = Utils.findRequest, findRequestURL = Utils.findRequestURL, findEventFromRequest = Utils.findEventFromRequest, findBatch = Utils.findBatch; @@ -38,7 +37,7 @@ describe('core SDK', function() { it('starts new session', function(done) { mParticle.startNewSession(); - const sessionStartEvent = findEventFromRequest(fetchMock._calls, 'session_start'); + const sessionStartEvent = findEventFromRequest(fetchMock.calls(), 'session_start'); sessionStartEvent.should.be.ok(); sessionStartEvent.data.should.have.property('session_uuid'); @@ -91,7 +90,7 @@ describe('core SDK', function() { mParticle.startNewSession(); mParticle.endSession(); - const sessionEndEvent = findEventFromRequest(fetchMock._calls, 'session_end'); + const sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); sessionEndEvent.should.be.ok(); @@ -102,14 +101,14 @@ describe('core SDK', function() { it('creates a new dateLastEventSent when logging an event, and retains the previous one when ending session', function(done) { mParticle.logEvent('Test Event1'); - const testEvent1 = findEventFromRequest(fetchMock._calls, 'Test Event1'); + const testEvent1 = findEventFromRequest(fetchMock.calls(), 'Test Event1'); setTimeout(function() { mParticle.logEvent('Test Event2'); - const testEvent2 = findEventFromRequest(fetchMock._calls, 'Test Event2'); + const testEvent2 = findEventFromRequest(fetchMock.calls(), 'Test Event2'); mParticle.endSession(); - const sessionEndEvent = findEventFromRequest(fetchMock._calls, 'session_end'); + const sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); const result1 = testEvent1.data.timestamp_unixtime_ms === testEvent2.data.timestamp_unixtime_ms; const result2 = testEvent2.data.timestamp_unixtime_ms === sessionEndEvent.data.timestamp_unixtime_ms; @@ -140,7 +139,7 @@ describe('core SDK', function() { mParticle.setAppVersion('1.0'); window.mParticle.logEvent('Test Event', mParticle.EventType.Navigation); - const testEventBatch = findBatch(fetchMock._calls, 'Test Event'); + const testEventBatch = findBatch(fetchMock.calls(), 'Test Event'); testEventBatch.application_info.should.have.property('application_version', '1.0'); done(); @@ -248,7 +247,7 @@ describe('core SDK', function() { removeme: new Error(), }); - const sanitizedEvent = findEventFromRequest(fetchMock._calls, 'sanitized event'); + const sanitizedEvent = findEventFromRequest(fetchMock.calls(), 'sanitized event'); sanitizedEvent.data.custom_attributes.should.have.property('key1', 'value1'); sanitizedEvent.data.custom_attributes.should.have.property('mydate'); @@ -281,13 +280,13 @@ describe('core SDK', function() { product.Attributes.should.have.property('valid'); mParticle.eCommerce.logCheckout(1, 'visa', attrs); - const checkoutEvent = findEventFromRequest(fetchMock._calls, 'checkout'); + const checkoutEvent = findEventFromRequest(fetchMock.calls(), 'checkout'); checkoutEvent.data.custom_attributes.should.not.have.property('invalid'); checkoutEvent.data.custom_attributes.should.have.property('valid'); mParticle.eCommerce.logProductAction(mParticle.ProductActionType.AddToCart, product, attrs); - const addToCartEvent = findEventFromRequest(fetchMock._calls, 'add_to_cart'); + const addToCartEvent = findEventFromRequest(fetchMock.calls(), 'add_to_cart'); addToCartEvent.data.custom_attributes.should.not.have.property('invalid'); addToCartEvent.data.custom_attributes.should.have.property('valid'); @@ -308,7 +307,7 @@ describe('core SDK', function() { false, attrs ); - const purchaseEvent = findEventFromRequest(fetchMock._calls, 'purchase'); + const purchaseEvent = findEventFromRequest(fetchMock.calls(), 'purchase'); purchaseEvent.data.custom_attributes.should.not.have.property('invalid'); purchaseEvent.data.custom_attributes.should.have.property('valid'); @@ -321,7 +320,7 @@ describe('core SDK', function() { mockServer.requests = []; mParticle.eCommerce.logPromotion(1, promotion, attrs); - const promotionViewEvent = findEventFromRequest(fetchMock._calls, 'view'); + const promotionViewEvent = findEventFromRequest(fetchMock.calls(), 'view'); promotionViewEvent.data.custom_attributes.should.not.have.property('invalid'); promotionViewEvent.data.custom_attributes.should.have.property('valid'); @@ -332,7 +331,7 @@ describe('core SDK', function() { false, attrs ); - const refundEvent = findEventFromRequest(fetchMock._calls, 'refund'); + const refundEvent = findEventFromRequest(fetchMock.calls(), 'refund'); refundEvent.data.custom_attributes.should.not.have.property('invalid'); refundEvent.data.custom_attributes.should.have.property('valid'); @@ -383,12 +382,12 @@ describe('core SDK', function() { mParticle.init(apiKey, window.mParticle.config); clock.tick(100); mParticle.logEvent('Test Event'); - const testEvent = findEventFromRequest(fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); clock.tick(70000); mParticle.logEvent('Test Event2'); - const testEvent2 = findEventFromRequest(fetchMock._calls, 'Test Event2'); + const testEvent2 = findEventFromRequest(fetchMock.calls(), 'Test Event2'); testEvent.data.session_uuid.should.not.equal(testEvent2.data.session_uuid); mParticle.getInstance()._SessionManager.clearSessionTimeout(); clock.restore(); @@ -411,9 +410,9 @@ describe('core SDK', function() { clock.tick(150000); - const testEvent = findEventFromRequest(fetchMock._calls, 'Test Event'); - const testEvent2 = findEventFromRequest(fetchMock._calls, 'Test Event2'); - const testEvent3 = findEventFromRequest(fetchMock._calls, 'Test Event3'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); + const testEvent2 = findEventFromRequest(fetchMock.calls(), 'Test Event2'); + const testEvent3 = findEventFromRequest(fetchMock.calls(), 'Test Event3'); testEvent2.data.session_uuid.should.equal(testEvent.data.session_uuid); testEvent3.data.session_uuid.should.not.equal(testEvent.data.session_uuid); @@ -435,10 +434,10 @@ describe('core SDK', function() { // This clock tick initiates a session end event that is successful clock.tick(70000); - let sessionEndEvent = findEventFromRequest(fetchMock._calls, 'session_end'); + let sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); Should(sessionEndEvent).be.ok(); - fetchMock._calls = []; + fetchMock.resetHistory(); clock.tick(100); mParticle.logEvent('Test Event2'); @@ -455,14 +454,14 @@ describe('core SDK', function() { setLocalStorage(workspaceCookieName, new_Persistence); // // This clock tick initiates a session end event that is not successful clock.tick(70000); - sessionEndEvent = findEventFromRequest(fetchMock._calls, 'session_end'); + sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); Should(sessionEndEvent).not.be.ok(); - const testEvent2 = findEventFromRequest(fetchMock._calls, 'Test Event2'); + const testEvent2 = findEventFromRequest(fetchMock.calls(), 'Test Event2'); mParticle.logEvent('Test Event3'); - const testEvent3 = findEventFromRequest(fetchMock._calls, 'Test Event3'); + const testEvent3 = findEventFromRequest(fetchMock.calls(), 'Test Event3'); testEvent3.data.session_uuid.should.equal(testEvent2.data.session_uuid); clock.restore(); @@ -471,7 +470,7 @@ describe('core SDK', function() { it('should get sessionId', function(done) { mParticle.logEvent('Test Event'); - const testEvent = findEventFromRequest(fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); const sessionId = mParticle.getInstance()._SessionManager.getSession(); @@ -483,7 +482,7 @@ describe('core SDK', function() { it('should set session start date in dto', function(done) { mParticle.logEvent('Test Event'); - const testEvent = findEventFromRequest(fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); testEvent.data.session_start_unixtime_ms.should.be.above(0); @@ -493,18 +492,18 @@ describe('core SDK', function() { it('should update session start date when manually ending session then starting a new one', function(done) { mParticle.logEvent('Test Event'); - const testEvent = findEventFromRequest(fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); const testEventSessionStartTime = testEvent.data.session_start_unixtime_ms; mParticle.endSession(); - const sessionEndEvent = findEventFromRequest(fetchMock._calls, 'session_end'); + const sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); const sessionEndEventSessionStartDate = sessionEndEvent.data.session_start_unixtime_ms; sessionEndEventSessionStartDate.should.equal(testEventSessionStartTime); mParticle.logEvent('Test Event2'); - const testEvent2 = findEventFromRequest(fetchMock._calls, 'Test Event2'); + const testEvent2 = findEventFromRequest(fetchMock.calls(), 'Test Event2'); const testEvent2SessionStartDate = testEvent2.data.session_start_unixtime_ms; testEvent2SessionStartDate.should.be.above(sessionEndEventSessionStartDate); @@ -522,21 +521,21 @@ describe('core SDK', function() { clock.tick(10); mParticle.logEvent('Test Event'); - const testEvent = findEventFromRequest(fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); const testEventSessionStartDate = testEvent.data.session_start_unixtime_ms; // trigger session timeout which ends session automatically clock.tick(60000); // note to self - session end event not being triggered, could be the same bug - const sessionEndEvent = findEventFromRequest(fetchMock._calls, 'session_end'); + const sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); const sessionEndEventSessionStartDate = sessionEndEvent.data.session_start_unixtime_ms; sessionEndEventSessionStartDate.should.equal(testEventSessionStartDate); clock.tick(100); mParticle.logEvent('Test Event2'); - const testEvent2 = findEventFromRequest(fetchMock._calls, 'Test Event2'); + const testEvent2 = findEventFromRequest(fetchMock.calls(), 'Test Event2'); const testEvent2SessionStartDate = testEvent2.data.session_start_unixtime_ms; testEvent2SessionStartDate.should.be.above(sessionEndEventSessionStartDate); @@ -549,7 +548,7 @@ describe('core SDK', function() { it('should load SDK with the included api on init and not send events to previous apikey in persistence', function(done) { mParticle.logEvent('Test Event1'); - const testEvent1URL = findRequest(fetchMock._calls, 'Test Event1')[0]; + const testEvent1URL = findRequestURL(fetchMock.calls(), 'Test Event1'); testEvent1URL.should.equal(urls.events); fetchMock.post( @@ -560,7 +559,7 @@ describe('core SDK', function() { mParticle.init('new-api-key', window.mParticle.config); mParticle.logEvent('Test Event2'); - const testEvent2URL = findRequestURL(fetchMock._calls, 'Test Event2'); + const testEvent2URL = findRequestURL(fetchMock.calls(), 'Test Event2'); testEvent2URL.should.equal( 'https://jssdks.mparticle.com/v3/JS/new-api-key/events' ); @@ -752,7 +751,7 @@ describe('core SDK', function() { warnMessages.length.should.equal(0); errorMessages.length.should.equal(0); - const testEvent = findEventFromRequest(fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); Should(testEvent).be.ok(); done(); @@ -805,6 +804,8 @@ describe('core SDK', function() { window.mParticle.config.identityUrl = 'custom-identityUrl/'; window.mParticle.config.aliasUrl = 'custom-aliasUrl/'; + fetchMock.post('https://testtesttest-custom-v3secureserviceurl/v3/JS/test_key/events', 200) + mParticle.init(apiKey, window.mParticle.config); mParticle.getInstance()._Store.SDKConfig.v3SecureServiceUrl.should.equal(window.mParticle.config.v3SecureServiceUrl) @@ -815,7 +816,7 @@ describe('core SDK', function() { // test events endpoint mParticle.logEvent('Test Event'); - const testEventURL = findRequestURL(fetchMock._calls, 'Test Event'); + const testEventURL = findRequestURL(fetchMock.calls(), 'Test Event'); testEventURL.should.equal( 'https://' + window.mParticle.config.v3SecureServiceUrl + @@ -978,7 +979,7 @@ describe('core SDK', function() { mParticle.Identity.identify({ userIdentities: { customerid: 'test' } }); mParticle.logEvent('Test Event'); - const testEvent = findEventFromRequest(fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); testEvent.should.be.ok(); diff --git a/test/src/tests-eCommerce.js b/test/src/tests-eCommerce.js index 87dd24e4..9082f213 100644 --- a/test/src/tests-eCommerce.js +++ b/test/src/tests-eCommerce.js @@ -1,5 +1,6 @@ import Utils from './utils'; import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { urls, apiKey, workspaceToken, MPConfig, testMPID, ProductActionType, PromotionActionType } from './config'; const getLocalStorageProducts = Utils.getLocalStorageProducts, @@ -14,7 +15,7 @@ describe('eCommerce', function() { delete mParticle._instances['default_instance']; mockServer = sinon.createFakeServer(); mockServer.respondImmediately = true; - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); mockServer.respondWith(urls.identify, [ 200, @@ -27,7 +28,7 @@ describe('eCommerce', function() { afterEach(function() { mockServer.restore(); - window.fetchMock.restore(); + fetchMock.restore(); mParticle._resetForTests(MPConfig); }); @@ -94,7 +95,7 @@ describe('eCommerce', function() { mParticle.eCommerce.logPurchase(transactionAttributes, product); - const purchaseEvent = findEventFromRequest(window.fetchMock._calls, 'purchase'); + const purchaseEvent = findEventFromRequest(fetchMock.calls(), 'purchase'); purchaseEvent.data.should.have.property('product_action'); purchaseEvent.data.product_action.should.have.property('action', 'purchase'); @@ -125,7 +126,7 @@ describe('eCommerce', function() { it('should not log a ecommerce event if there is a typo in the product action type', function(done) { // fetchMock calls will have session start and AST events, we want to reset so that we can prove the product action type does not go through (length remains 0 after logging) - window.fetchMock._calls = []; + fetchMock.resetHistory(); const product = mParticle.eCommerce.createProduct( 'iPhone', '12345', @@ -138,7 +139,7 @@ describe('eCommerce', function() { mParticle.ProductActionType.Typo, // <------ will result in a null when converting the product action type as this is not a real value [product] ); - window.fetchMock._calls.length.should.equal(0); + fetchMock.calls().length.should.equal(0); done(); }); @@ -167,7 +168,7 @@ describe('eCommerce', function() { mParticle.eCommerce.logPurchase(transactionAttributes, product); - const purchaseEvent = findEventFromRequest(window.fetchMock._calls, 'purchase'); + const purchaseEvent = findEventFromRequest(fetchMock.calls(), 'purchase'); purchaseEvent.data.should.have.property('product_action'); purchaseEvent.data.product_action.should.have.property('action', 'purchase'); @@ -219,12 +220,12 @@ describe('eCommerce', function() { ); mParticle.eCommerce.logPurchase(transactionAttributes, product); - const purchaseEvent1 = findEventFromRequest(window.fetchMock._calls, 'purchase'); + const purchaseEvent1 = findEventFromRequest(fetchMock.calls(), 'purchase'); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.eCommerce.logProductAction(mParticle.ProductActionType.Purchase, product, null, null, transactionAttributes) - const purchaseEvent2 = findEventFromRequest(window.fetchMock._calls, 'purchase'); + const purchaseEvent2 = findEventFromRequest(fetchMock.calls(), 'purchase'); const { product_action: productAction1 } = purchaseEvent1.data; const { product_action: productAction2 } = purchaseEvent2.data @@ -266,7 +267,7 @@ describe('eCommerce', function() { product2, ]); - const purchaseEvent = findEventFromRequest(window.fetchMock._calls, 'purchase'); + const purchaseEvent = findEventFromRequest(fetchMock.calls(), 'purchase'); purchaseEvent.data.should.have.property('product_action'); purchaseEvent.data.product_action.should.have.property('products').with.lengthOf(2); @@ -288,7 +289,7 @@ describe('eCommerce', function() { product2, ]); - const refundEvent = findEventFromRequest(window.fetchMock._calls, 'refund'); + const refundEvent = findEventFromRequest(fetchMock.calls(), 'refund'); refundEvent.data.should.have.property('product_action'); refundEvent.data.product_action.should.have.property('products').with.lengthOf(2); @@ -329,7 +330,7 @@ describe('eCommerce', function() { promotion ); - const promotionEvent = findEventFromRequest(window.fetchMock._calls, 'click'); + const promotionEvent = findEventFromRequest(fetchMock.calls(), 'click'); Should(promotionEvent).be.ok(); @@ -363,7 +364,7 @@ describe('eCommerce', function() { [promotion1, promotion2] ); - const promotionEvent = findEventFromRequest(window.fetchMock._calls, 'click'); + const promotionEvent = findEventFromRequest(fetchMock.calls(), 'click'); Should(promotionEvent).be.ok(); @@ -397,7 +398,7 @@ describe('eCommerce', function() { { shouldUploadEvent: false } ); - const promotionEvent = findEventFromRequest(window.fetchMock._calls, 'click'); + const promotionEvent = findEventFromRequest(fetchMock.calls(), 'click'); Should(promotionEvent).not.be.ok(); done(); @@ -425,7 +426,7 @@ describe('eCommerce', function() { ); mParticle.eCommerce.logImpression(impression); - const impressionEvent = findEventFromRequest(window.fetchMock._calls, 'impression'); + const impressionEvent = findEventFromRequest(fetchMock.calls(), 'impression'); Should(impressionEvent).be.ok(); @@ -447,7 +448,7 @@ describe('eCommerce', function() { mParticle.eCommerce.logImpression(impression, null, null, { shouldUploadEvent: false }); - const impressionEvent = findEventFromRequest(window.fetchMock._calls, 'impression'); + const impressionEvent = findEventFromRequest(fetchMock.calls(), 'impression'); Should(impressionEvent).not.be.ok(); @@ -472,7 +473,7 @@ describe('eCommerce', function() { mParticle.eCommerce.logImpression([impression, impression2]); - const impressionEvent = findEventFromRequest(window.fetchMock._calls, 'impression'); + const impressionEvent = findEventFromRequest(fetchMock.calls(), 'impression'); Should(impressionEvent).be.ok(); @@ -511,7 +512,7 @@ describe('eCommerce', function() { mParticle.eCommerce.logRefund(transactionAttributes, product); - const refundEvent = findEventFromRequest(window.fetchMock._calls, 'refund'); + const refundEvent = findEventFromRequest(fetchMock.calls(), 'refund'); Should(refundEvent).be.ok(); @@ -561,13 +562,13 @@ describe('eCommerce', function() { mParticle.eCommerce.logRefund(transactionAttributes, product); - const refundEvent1 = findEventFromRequest(window.fetchMock._calls, 'refund'); + const refundEvent1 = findEventFromRequest(fetchMock.calls(), 'refund'); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.eCommerce.logProductAction(mParticle.ProductActionType.Refund, product, null, null, transactionAttributes) - const refundEvent2 = findEventFromRequest(window.fetchMock._calls, 'refund'); + const refundEvent2 = findEventFromRequest(fetchMock.calls(), 'refund'); Should(refundEvent1).be.ok(); Should(refundEvent2).be.ok(); @@ -623,7 +624,7 @@ describe('eCommerce', function() { { shouldUploadEvent: false} ); - const event = findEventFromRequest(window.fetchMock._calls, 'purchase'); + const event = findEventFromRequest(fetchMock.calls(), 'purchase'); Should(event).not.be.ok(); done(); @@ -634,7 +635,7 @@ describe('eCommerce', function() { mParticle.eCommerce.Cart.add(product, true); - const addToCartEvent = findEventFromRequest(window.fetchMock._calls, 'add_to_cart'); + const addToCartEvent = findEventFromRequest(fetchMock.calls(), 'add_to_cart'); addToCartEvent.data.should.have.property('product_action'); addToCartEvent.data.product_action.should.have.property('action', 'add_to_cart'); @@ -650,7 +651,7 @@ describe('eCommerce', function() { mParticle.eCommerce.Cart.add(product); mParticle.eCommerce.Cart.remove({ Sku: '12345' }, true); - const removeFromCartEvent = findEventFromRequest(window.fetchMock._calls, 'remove_from_cart'); + const removeFromCartEvent = findEventFromRequest(fetchMock.calls(), 'remove_from_cart'); removeFromCartEvent.data.should.have.property('product_action'); removeFromCartEvent.data.product_action.should.have.property('action', 'remove_from_cart'); @@ -727,7 +728,7 @@ describe('eCommerce', function() { mParticle.eCommerce.logCheckout(1, 'Visa'); - const checkoutEvent = findEventFromRequest(window.fetchMock._calls, 'checkout'); + const checkoutEvent = findEventFromRequest(fetchMock.calls(), 'checkout'); Should(checkoutEvent).be.ok(); @@ -752,7 +753,7 @@ describe('eCommerce', function() { mParticle.eCommerce.logProductAction(mParticle.ProductActionType.Checkout, [product1, product2], null, null, {Step: 4, Option: 'Visa'}); - const checkoutEvent = findEventFromRequest(window.fetchMock._calls, 'checkout'); + const checkoutEvent = findEventFromRequest(fetchMock.calls(), 'checkout'); Should(checkoutEvent).be.ok(); @@ -777,7 +778,7 @@ describe('eCommerce', function() { { color: 'blue' } ); - const checkoutOptionEvent = findEventFromRequest(window.fetchMock._calls, 'checkout_option'); + const checkoutOptionEvent = findEventFromRequest(fetchMock.calls(), 'checkout_option'); Should(checkoutOptionEvent).be.ok(); @@ -801,7 +802,7 @@ describe('eCommerce', function() { product ); - const viewDetailEvent = findEventFromRequest(window.fetchMock._calls, 'view_detail'); + const viewDetailEvent = findEventFromRequest(fetchMock.calls(), 'view_detail'); Should(viewDetailEvent).be.ok(); viewDetailEvent.should.have.property('event_type', 'commerce_event'); @@ -896,7 +897,7 @@ describe('eCommerce', function() { ); mParticle.eCommerce.logPurchase(transactionAttributes, product); - const purchaseEvent = findEventFromRequest(window.fetchMock._calls, 'purchase'); + const purchaseEvent = findEventFromRequest(fetchMock.calls(), 'purchase'); purchaseEvent.data.product_action.products[0].should.not.have.property('position'); @@ -920,7 +921,7 @@ describe('eCommerce', function() { mParticle.eCommerce.Cart.add([product1, product2], true); - const addToCartEvent = findEventFromRequest(window.fetchMock._calls, 'add_to_cart'); + const addToCartEvent = findEventFromRequest(fetchMock.calls(), 'add_to_cart'); Should(addToCartEvent).be.ok(); @@ -947,7 +948,7 @@ describe('eCommerce', function() { mParticle.eCommerce.Cart.add(product1, true); - const addToCartEvent = findEventFromRequest(window.fetchMock._calls, 'add_to_cart'); + const addToCartEvent = findEventFromRequest(fetchMock.calls(), 'add_to_cart'); Should(addToCartEvent).be.ok(); @@ -1421,7 +1422,7 @@ describe('eCommerce', function() { it('should add customFlags to logCheckout events', function(done) { mParticle.eCommerce.logCheckout(1, {}, {}, { interactionEvent: true }); - const checkoutEvent = findEventFromRequest(window.fetchMock._calls, 'checkout'); + const checkoutEvent = findEventFromRequest(fetchMock.calls(), 'checkout'); checkoutEvent.data.custom_flags.interactionEvent.should.equal(true); done(); @@ -1435,7 +1436,7 @@ describe('eCommerce', function() { { price: 5 }, { interactionEvent: true } ); - const unknownEvent = findEventFromRequest(window.fetchMock._calls, 'unknown'); + const unknownEvent = findEventFromRequest(fetchMock.calls(), 'unknown'); unknownEvent.data.custom_flags.interactionEvent.should.equal(true); @@ -1457,7 +1458,7 @@ describe('eCommerce', function() { { interactionEvent: true } ); - const purchaseEvent = findEventFromRequest(window.fetchMock._calls, 'purchase'); + const purchaseEvent = findEventFromRequest(fetchMock.calls(), 'purchase'); purchaseEvent.data.custom_flags.interactionEvent.should.equal(true); @@ -1479,7 +1480,7 @@ describe('eCommerce', function() { ); - const promotionEvent = findEventFromRequest(window.fetchMock._calls, 'click'); + const promotionEvent = findEventFromRequest(fetchMock.calls(), 'click'); promotionEvent.data.custom_flags.interactionEvent.should.equal(true); @@ -1498,7 +1499,7 @@ describe('eCommerce', function() { { interactionEvent: true } ); - const impressionEvent = findEventFromRequest(window.fetchMock._calls, 'impression'); + const impressionEvent = findEventFromRequest(fetchMock.calls(), 'impression'); impressionEvent.data.custom_flags.interactionEvent.should.equal(true); done(); @@ -1518,7 +1519,7 @@ describe('eCommerce', function() { { shipping: 5 }, { interactionEvent: true } ); - const refundEvent = findEventFromRequest(window.fetchMock._calls, 'refund'); + const refundEvent = findEventFromRequest(fetchMock.calls(), 'refund'); refundEvent.data.custom_flags.interactionEvent.should.equal(true); diff --git a/test/src/tests-event-logging.js b/test/src/tests-event-logging.js index e23cb9ee..1aa54847 100644 --- a/test/src/tests-event-logging.js +++ b/test/src/tests-event-logging.js @@ -1,5 +1,6 @@ import Utils from './utils'; import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { urls, apiKey, testMPID, MPConfig, @@ -13,7 +14,7 @@ let mockServer; describe('event logging', function() { beforeEach(function() { mParticle._resetForTests(MPConfig); - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); delete mParticle._instances['default_instance']; mockServer = sinon.createFakeServer(); mockServer.respondImmediately = true; @@ -29,7 +30,7 @@ describe('event logging', function() { afterEach(function() { mockServer.restore(); - window.fetchMock.restore(); + fetchMock.restore(); mParticle._resetForTests(MPConfig); }); @@ -39,8 +40,8 @@ describe('event logging', function() { mParticle.EventType.Navigation, { mykey: 'myvalue' } ); - const testEvent = findEventFromRequest(window.fetchMock._calls, 'Test Event'); - const testEventBatch = findBatch(window.fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); + const testEventBatch = findBatch(fetchMock.calls(), 'Test Event'); testEvent.data.should.have.property('event_name', 'Test Event'); testEvent.data.should.have.property('custom_event_type', 'navigation'); @@ -59,14 +60,14 @@ describe('event logging', function() { { mykey: 'myvalue' } ); - const testEventBatch = findBatch(window.fetchMock._calls, 'Test Event'); + const testEventBatch = findBatch(fetchMock.calls(), 'Test Event'); // this das should be the SDK auto generated one, which is 36 characters long testEventBatch.mp_deviceid.should.have.length(36) mParticle.setDeviceId('foo-guid'); window.mParticle.logEvent('Test Event2',); - const testEvent2Batch = findBatch(window.fetchMock._calls, 'Test Event2'); + const testEvent2Batch = findBatch(fetchMock.calls(), 'Test Event2'); // das should be the one passed to setDeviceId() testEvent2Batch.should.have.property('mp_deviceid', 'foo-guid'); @@ -81,7 +82,7 @@ describe('event logging', function() { mParticle.init(apiKey, window.mParticle.config); window.mParticle.logEvent('Test Event',); - const testEventBatch = findBatch(window.fetchMock._calls, 'Test Event'); + const testEventBatch = findBatch(fetchMock.calls(), 'Test Event'); // this das should be the SDK auto generated one testEventBatch.should.have.property('mp_deviceid', 'foo-guid'); @@ -110,10 +111,10 @@ describe('event logging', function() { } ); - const uploadEvent = findEventFromRequest(window.fetchMock._calls, 'Test Standard Upload'); - const uploadEventBatch = findBatch(window.fetchMock._calls, 'Test Standard Upload'); + const uploadEvent = findEventFromRequest(fetchMock.calls(), 'Test Standard Upload'); + const uploadEventBatch = findBatch(fetchMock.calls(), 'Test Standard Upload'); - const bypassedEvent = findEventFromRequest(window.fetchMock._calls, 'Test Upload Bypass'); + const bypassedEvent = findEventFromRequest(fetchMock.calls(), 'Test Upload Bypass'); uploadEvent.should.be.ok(); uploadEvent.data.should.have.property('event_name', 'Test Standard Upload'); @@ -148,10 +149,10 @@ describe('event logging', function() { shouldUploadEvent: false, }); - const uploadEvent = findEventFromRequest(window.fetchMock._calls, 'Test Standard Upload'); - const uploadEventBatch = findBatch(window.fetchMock._calls, 'Test Standard Upload'); + const uploadEvent = findEventFromRequest(fetchMock.calls(), 'Test Standard Upload'); + const uploadEventBatch = findBatch(fetchMock.calls(), 'Test Standard Upload'); - const bypassedEvent = findEventFromRequest(window.fetchMock._calls, 'Test Upload Bypass'); + const bypassedEvent = findEventFromRequest(fetchMock.calls(), 'Test Upload Bypass'); uploadEvent.should.be.ok(); @@ -169,7 +170,7 @@ describe('event logging', function() { it('should log an error', function(done) { mParticle.logError('my error'); - const errorEvent = findEventFromRequest(window.fetchMock._calls, 'my error'); + const errorEvent = findEventFromRequest(fetchMock.calls(), 'my error'); Should(errorEvent).be.ok(); @@ -186,7 +187,7 @@ describe('event logging', function() { mParticle.logError(error); - const errorEvent = findEventFromRequest(window.fetchMock._calls, 'my error'); + const errorEvent = findEventFromRequest(fetchMock.calls(), 'my error'); Should(errorEvent).be.ok(); @@ -205,7 +206,7 @@ describe('event logging', function() { mParticle.logError(error, { location: 'my path', myData: 'my data' }); - const errorEvent = findEventFromRequest(window.fetchMock._calls, 'my error'); + const errorEvent = findEventFromRequest(fetchMock.calls(), 'my error'); Should(errorEvent).be.ok(); errorEvent.data.should.have.property('message', 'Error'); @@ -223,7 +224,7 @@ describe('event logging', function() { valid: 10, }); - const errorEvent = findEventFromRequest(window.fetchMock._calls, 'my error'); + const errorEvent = findEventFromRequest(fetchMock.calls(), 'my error'); Should(errorEvent).be.ok(); errorEvent.data.should.have.property('message', 'Error'); @@ -242,7 +243,7 @@ describe('event logging', function() { }); it('should log an AST with firstRun = true when first visiting a page, and firstRun = false when reloading the page', function(done) { - const astEvent = findEventFromRequest(window.fetchMock._calls, 'application_state_transition'); + const astEvent = findEventFromRequest(fetchMock.calls(), 'application_state_transition'); astEvent.data.should.have.property('application_transition_type', 'application_initialized'); astEvent.data.should.have.property('is_first_run', true); @@ -252,10 +253,10 @@ describe('event logging', function() { astEvent.data.should.have.property('launch_referral', window.location.href); } - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.init(apiKey, window.mParticle.config) - const astEvent2 = findEventFromRequest(window.fetchMock._calls, 'application_state_transition'); + const astEvent2 = findEventFromRequest(fetchMock.calls(), 'application_state_transition'); astEvent2.data.should.have.property('is_first_run', false); @@ -264,11 +265,11 @@ describe('event logging', function() { it('should log an AST on init with firstRun = false when cookies already exist', function(done) { // cookies currently exist, mParticle.init called from beforeEach - window.fetchMock._calls = []; + fetchMock.resetHistory(); // log second AST mParticle.init(apiKey, window.mParticle.config); - const astEvent = findEventFromRequest(window.fetchMock._calls, 'application_state_transition'); + const astEvent = findEventFromRequest(fetchMock.calls(), 'application_state_transition'); astEvent.data.should.have.property('is_first_run', false); done(); @@ -277,7 +278,7 @@ describe('event logging', function() { it('should log a page view', function(done) { mParticle.logPageView(); - const pageViewEvent = findEventFromRequest(window.fetchMock._calls, 'screen_view'); + const pageViewEvent = findEventFromRequest(fetchMock.calls(), 'screen_view'); Should(pageViewEvent).be.ok(); @@ -297,7 +298,7 @@ describe('event logging', function() { } ); - const pageViewEvent = findEventFromRequest(window.fetchMock._calls, 'screen_view'); + const pageViewEvent = findEventFromRequest(fetchMock.calls(), 'screen_view'); Should(pageViewEvent).be.ok(); @@ -314,7 +315,7 @@ describe('event logging', function() { 'MyCustom.Flag': 'Test', }); - const pageViewEvent = findEventFromRequest(window.fetchMock._calls, 'screen_view'); + const pageViewEvent = findEventFromRequest(fetchMock.calls(), 'screen_view'); Should(pageViewEvent).be.ok(); @@ -332,7 +333,7 @@ describe('event logging', function() { { shouldUploadEvent: false } ); - const pageViewEvent = findEventFromRequest(window.fetchMock._calls, 'screen_view'); + const pageViewEvent = findEventFromRequest(fetchMock.calls(), 'screen_view'); Should(pageViewEvent).not.be.ok(); done(); @@ -340,7 +341,7 @@ describe('event logging', function() { it('should not log a PageView event if there are invalid attrs', function(done) { mParticle.logPageView('test1', 'invalid', null); - const pageViewEvent = findEventFromRequest(window.fetchMock._calls, 'screen_view'); + const pageViewEvent = findEventFromRequest(fetchMock.calls(), 'screen_view'); Should(pageViewEvent).not.be.ok(); @@ -350,30 +351,30 @@ describe('event logging', function() { it('should not log an event that has an invalid customFlags', function(done) { mParticle.logPageView('test', null, 'invalid'); - const pageViewEvent = findEventFromRequest(window.fetchMock._calls, 'screen_view'); + const pageViewEvent = findEventFromRequest(fetchMock.calls(), 'screen_view'); Should(pageViewEvent).not.be.ok(); done(); }); it('should log event with name PageView when an invalid event name is passed', function(done) { - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logPageView(null); - window.fetchMock._calls.length.should.equal(1); - const pageViewEvent = findEventFromRequest(window.fetchMock._calls, 'screen_view'); + fetchMock.calls().length.should.equal(1); + const pageViewEvent = findEventFromRequest(fetchMock.calls(), 'screen_view'); pageViewEvent.data.screen_name.should.equal('PageView'); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logPageView({ test: 'test' }); - window.fetchMock._calls.length.should.equal(1); - const pageViewEvent2 = findEventFromRequest(window.fetchMock._calls, 'screen_view'); + fetchMock.calls().length.should.equal(1); + const pageViewEvent2 = findEventFromRequest(fetchMock.calls(), 'screen_view'); pageViewEvent2.data.screen_name.should.equal('PageView'); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logPageView([1, 2, 3]); - window.fetchMock._calls.length.should.equal(1); - const pageViewEvent3 = findEventFromRequest(window.fetchMock._calls, 'screen_view'); + fetchMock.calls().length.should.equal(1); + const pageViewEvent3 = findEventFromRequest(fetchMock.calls(), 'screen_view'); pageViewEvent3.data.screen_name.should.equal('PageView'); done(); @@ -382,7 +383,7 @@ describe('event logging', function() { it('should log opt out', function(done) { mParticle.setOptOut(true); - const optOutEvent = findEventFromRequest(window.fetchMock._calls, 'opt_out'); + const optOutEvent = findEventFromRequest(fetchMock.calls(), 'opt_out'); optOutEvent.event_type.should.equal('opt_out'); optOutEvent.data.should.have.property('is_opted_out', true); @@ -391,20 +392,20 @@ describe('event logging', function() { }); it('log event requires name', function(done) { - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent(); - window.fetchMock._calls.should.have.lengthOf(0); + fetchMock.calls().should.have.lengthOf(0); done(); }); it('log event requires valid event type', function(done) { - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('test', 100); - window.fetchMock._calls.should.have.lengthOf(0); + fetchMock.calls().should.have.lengthOf(0); done(); }); @@ -412,7 +413,7 @@ describe('event logging', function() { it('event attributes must be object', function(done) { mParticle.logEvent('Test Event', null, 1); - const testEvent = findEventFromRequest(window.fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); testEvent.data.should.have.property('custom_attributes', null); @@ -421,36 +422,36 @@ describe('event logging', function() { it('opting out should prevent events being sent', function(done) { mParticle.setOptOut(true); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('test'); - window.fetchMock._calls.should.have.lengthOf(0); + fetchMock.calls().should.have.lengthOf(0); done(); }); it('after logging optout, and reloading, events still should not be sent until opt out is enabled when using local storage', function(done) { mParticle.setOptOut(true); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('test'); - window.fetchMock._calls.should.have.lengthOf(0); + fetchMock.calls().should.have.lengthOf(0); mParticle.setOptOut(false); mParticle.init(apiKey, window.mParticle.config); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('test'); - window.fetchMock._calls.should.have.lengthOf(1); + fetchMock.calls().should.have.lengthOf(1); mParticle.setOptOut(true); mParticle.init(apiKey, window.mParticle.config); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('test'); - window.fetchMock._calls.should.have.lengthOf(0); + fetchMock.calls().should.have.lengthOf(0); done(); }); @@ -459,23 +460,23 @@ describe('event logging', function() { mParticle.config.useCookieStorage = true; mParticle.init(apiKey, window.mParticle.config); mParticle.setOptOut(true); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('test'); - window.fetchMock._calls.should.have.lengthOf(0); + fetchMock.calls().should.have.lengthOf(0); mParticle.setOptOut(false); mParticle.init(apiKey, window.mParticle.config); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('test'); - window.fetchMock._calls.should.have.lengthOf(1); + fetchMock.calls().should.have.lengthOf(1); mParticle.setOptOut(true); mParticle.init(apiKey, window.mParticle.config); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('test'); - window.fetchMock._calls.should.have.lengthOf(0); + fetchMock.calls().should.have.lengthOf(0); done(); }); @@ -543,7 +544,7 @@ describe('event logging', function() { it('should send das with each event logged', function(done) { window.mParticle.logEvent('Test Event'); - const testEventBatch = findBatch(window.fetchMock._calls, 'Test Event'); + const testEventBatch = findBatch(fetchMock.calls(), 'Test Event'); testEventBatch.should.have.property('mp_deviceid'); testEventBatch.mp_deviceid.length.should.equal(36); @@ -565,7 +566,7 @@ describe('event logging', function() { mParticle.Identity.getCurrentUser().setConsentState(consentState); window.mParticle.logEvent('Test Event'); - const testEvent = findBatch(window.fetchMock._calls, 'Test Event'); + const testEvent = findBatch(fetchMock.calls(), 'Test Event'); testEvent.should.have.property('consent_state'); testEvent.consent_state.should.have.property('gdpr'); @@ -580,7 +581,7 @@ describe('event logging', function() { mParticle.Identity.getCurrentUser().setConsentState(null); window.mParticle.logEvent('Test Event2'); - const testEvent2 = findBatch(window.fetchMock._calls, 'Test Event2'); + const testEvent2 = findBatch(fetchMock.calls(), 'Test Event2'); testEvent2.should.have.property('consent_state', null); @@ -590,7 +591,7 @@ describe('event logging', function() { it('should log integration attributes with each event', function(done) { mParticle.setIntegrationAttribute(128, { MCID: 'abcdefg' }); mParticle.logEvent('Test Event'); - const testEvent = findBatch(window.fetchMock._calls, 'Test Event'); + const testEvent = findBatch(fetchMock.calls(), 'Test Event'); testEvent.should.have.property('integration_attributes'); testEvent.integration_attributes.should.have.property('128'); @@ -616,17 +617,17 @@ describe('event logging', function() { // mock geo will successfully run after 1 second (geomock.js // navigator.geolocation.delay) clock.tick(1000); successCallbackCalled.should.equal(true); - let testEvent = findEventFromRequest(window.fetchMock._calls, 'Test Event'); + let testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); testEvent.data.location.latitude.should.equal(52.5168); testEvent.data.location.longitude.should.equal(13.3889); - window.fetchMock._calls = []; + fetchMock.resetHistory(); //this will hit the watch position again, but won't call the callback again clock.tick(1000); numberTimesCalled.should.equal(1); - testEvent = findEventFromRequest(window.fetchMock._calls, 'Test Event'); + testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); Should(testEvent).not.be.ok(); @@ -655,16 +656,16 @@ describe('event logging', function() { clock.tick(1000); successCallbackCalled.should.equal(true); - let testEvent = findEventFromRequest(window.fetchMock._calls, 'Test Event'); + let testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); testEvent.data.should.have.property('location', null); - window.fetchMock._calls = []; + fetchMock.resetHistory(); //this will hit the watch position again, but won't call the callback again clock.tick(1000); numberTimesCalled.should.equal(1); - testEvent = findEventFromRequest(window.fetchMock._calls, 'Test Event'); + testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); Should(testEvent).not.be.ok(); navigator.geolocation.shouldFail = false; @@ -719,7 +720,7 @@ describe('event logging', function() { // mock geo will successfully run after 1 second (geomock.js // navigator.geolocation.delay) clock.tick(1000); successCallbackCalled.should.equal(true); - const testEvent = findEventFromRequest(window.fetchMock._calls, 'Test Event'); + const testEvent = findEventFromRequest(fetchMock.calls(), 'Test Event'); const latitudeResult = 52.5168; const longitudeResult = 13.3889; @@ -741,7 +742,7 @@ describe('event logging', function() { window.mParticle.logEvent('Test Event'); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.application_info.should.have.property('application_name', 'a name'); @@ -755,11 +756,11 @@ describe('event logging', function() { mParticle.init(apiKey, mParticle.config); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.events[0].data.should.have.property('is_first_run', true); mParticle.init(apiKey, mParticle.config); - const batch2 = JSON.parse(window.fetchMock.lastOptions().body); + const batch2 = JSON.parse(fetchMock.lastOptions().body); batch2.events[0].data.should.have.property('is_first_run', false); delete window.mParticle.config.flags @@ -776,7 +777,7 @@ describe('event logging', function() { mParticle.init(apiKey, mParticle.config); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.events[0].data.should.have.property('launch_referral'); batch.events[0].data.launch_referral.should.startWith('http://localhost') @@ -795,7 +796,7 @@ describe('event logging', function() { window.mParticle.logEvent('Test Event'); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.application_info.should.have.property('application_name', 'another name'); delete window.mParticle.config.flags @@ -817,7 +818,7 @@ describe('event logging', function() { window.mParticle.logEvent('Test Event'); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.should.have.property('context'); batch.context.should.have.property('data_plan'); @@ -841,7 +842,7 @@ describe('event logging', function() { window.mParticle.logEvent('Test Event'); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.should.have.property('context'); batch.context.should.have.property('data_plan'); @@ -865,7 +866,7 @@ describe('event logging', function() { window.mParticle.logEvent('Test Event'); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.should.not.have.property('context'); @@ -898,7 +899,7 @@ describe('event logging', function() { window.mParticle.logEvent('Test Event'); errorMessage.should.equal('Your data plan id must be a string and match the data plan slug format (i.e. under_case_slug)') - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.should.not.have.property('context'); delete window.mParticle.config.flags @@ -942,7 +943,7 @@ describe('event logging', function() { window.mParticle.logEvent('Test Event'); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.should.have.property('consent_state'); batch.consent_state.should.have.properties(['gdpr', 'ccpa']); @@ -991,7 +992,7 @@ describe('event logging', function() { customFlags, transactionAttributes); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); batch.events[0].data.product_action.total_amount.should.equal(0); batch.events[0].data.product_action.shipping_amount.should.equal(0); @@ -1028,7 +1029,7 @@ describe('event logging', function() { customFlags, transactionAttributes); - const batch = JSON.parse(window.fetchMock.lastOptions().body); + const batch = JSON.parse(fetchMock.lastOptions().body); (batch.events[0].data.product_action.products[0].position === null).should.equal(true) batch.events[0].data.product_action.products[0].price.should.equal(0); batch.events[0].data.product_action.products[0].quantity.should.equal(0); diff --git a/test/src/tests-forwarders.js b/test/src/tests-forwarders.js index 19fa1765..73ffdf14 100644 --- a/test/src/tests-forwarders.js +++ b/test/src/tests-forwarders.js @@ -1,5 +1,7 @@ import Utils from './utils'; import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; + import { urls } from './config'; import { apiKey, MPConfig, testMPID, MessageType } from './config'; import { expect } from 'chai'; @@ -16,7 +18,7 @@ describe('forwarders', function() { beforeEach(function() { mParticle._resetForTests(MPConfig); delete mParticle._instances['default_instance']; - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); mockServer = sinon.createFakeServer(); mockServer.respondImmediately = true; @@ -53,7 +55,7 @@ describe('forwarders', function() { }); afterEach(function() { - window.fetchMock.restore(); + fetchMock.restore(); delete window.MockForwarder1; mockServer.restore(); }); @@ -1022,7 +1024,7 @@ describe('forwarders', function() { { 'my-key': 'my-value' } ); - const event = findEventFromRequest(window.fetchMock._calls, 'send this event to forwarder', true); + const event = findEventFromRequest(fetchMock.calls(), 'send this event to forwarder', true); Should(event).should.not.have.property('n'); @@ -2216,19 +2218,19 @@ describe('forwarders', function() { mParticle._setIntegrationDelay(10, true); mParticle.init(apiKey, window.mParticle.config); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('Test Event1'); - window.fetchMock._calls.length.should.equal(0); + fetchMock.calls().length.should.equal(0); mParticle._setIntegrationDelay(10, false); mParticle._setIntegrationDelay(128, false); mParticle.logEvent('Test Event2'); - window.fetchMock._calls.length.should.equal(4); + fetchMock.calls().length.should.equal(4); - const sessionStartEvent = findEventFromRequest(window.fetchMock._calls, 'session_start'); - const ASTEvent = findEventFromRequest(window.fetchMock._calls, 'application_state_transition'); - const testEvent1 = findEventFromRequest(window.fetchMock._calls, 'Test Event1'); - const testEvent2 = findEventFromRequest(window.fetchMock._calls, 'Test Event2'); + const sessionStartEvent = findEventFromRequest(fetchMock.calls(), 'session_start'); + const ASTEvent = findEventFromRequest(fetchMock.calls(), 'application_state_transition'); + const testEvent1 = findEventFromRequest(fetchMock.calls(), 'Test Event1'); + const testEvent2 = findEventFromRequest(fetchMock.calls(), 'Test Event2'); sessionStartEvent.should.be.ok(); ASTEvent.should.be.ok(); @@ -2259,19 +2261,19 @@ describe('forwarders', function() { .length ).equal(3); mParticle.init(apiKey, window.mParticle.config); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('Test Event1'); - window.fetchMock._calls.length.should.equal(0); + fetchMock.calls().length.should.equal(0); clock.tick(5001); mParticle.logEvent('Test Event2'); - window.fetchMock._calls.length.should.equal(4); + fetchMock.calls().length.should.equal(4); - const sessionStartEvent = findEventFromRequest(window.fetchMock._calls, 'session_start'); - const ASTEvent = findEventFromRequest(window.fetchMock._calls, 'application_state_transition'); - const testEvent1 = findEventFromRequest(window.fetchMock._calls, 'Test Event1'); - const testEvent2 = findEventFromRequest(window.fetchMock._calls, 'Test Event2'); + const sessionStartEvent = findEventFromRequest(fetchMock.calls(), 'session_start'); + const ASTEvent = findEventFromRequest(fetchMock.calls(), 'application_state_transition'); + const testEvent1 = findEventFromRequest(fetchMock.calls(), 'Test Event1'); + const testEvent2 = findEventFromRequest(fetchMock.calls(), 'Test Event2'); sessionStartEvent.should.be.ok(); ASTEvent.should.be.ok(); @@ -2287,19 +2289,19 @@ describe('forwarders', function() { mParticle._setIntegrationDelay(24, false); mParticle._setIntegrationDelay(10, true); mParticle.init(apiKey, window.mParticle.config); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('Test Event3'); - window.fetchMock._calls.length.should.equal(0); + fetchMock.calls().length.should.equal(0); clock.tick(1001); mParticle.logEvent('Test Event4'); - window.fetchMock._calls.length.should.equal(4); + fetchMock.calls().length.should.equal(4); - const sessionStartEvent2 = findEventFromRequest(window.fetchMock._calls, 'session_start'); - const ASTEvent2 = findEventFromRequest(window.fetchMock._calls, 'application_state_transition'); - const testEvent3 = findEventFromRequest(window.fetchMock._calls, 'Test Event3'); - const testEvent4 = findEventFromRequest(window.fetchMock._calls, 'Test Event4'); + const sessionStartEvent2 = findEventFromRequest(fetchMock.calls(), 'session_start'); + const ASTEvent2 = findEventFromRequest(fetchMock.calls(), 'application_state_transition'); + const testEvent3 = findEventFromRequest(fetchMock.calls(), 'Test Event3'); + const testEvent4 = findEventFromRequest(fetchMock.calls(), 'Test Event4'); sessionStartEvent2.should.be.ok(); ASTEvent2.should.be.ok(); @@ -2317,15 +2319,15 @@ describe('forwarders', function() { mParticle._setIntegrationDelay(128, true); mParticle._setIntegrationDelay(24, false); mParticle.init(apiKey, window.mParticle.config); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('test1'); - window.fetchMock._calls.length.should.equal(0); + fetchMock.calls().length.should.equal(0); // now that we set all integrations to false, the SDK should process queued events mParticle._setIntegrationDelay(128, false); clock.tick(5001); - window.fetchMock._calls.length.should.equal(3); + fetchMock.calls().length.should.equal(3); clock.restore(); done(); @@ -2714,7 +2716,7 @@ describe('forwarders', function() { mParticle.logEvent('foo', mParticle.EventType.Navigation); - const batch = JSON.parse(window.fetchMock.lastCall()[1].body); + const batch = JSON.parse(fetchMock.lastCall()[1].body); expect(batch).to.have.property('application_info'); expect(batch.application_info).to.have.property( @@ -2731,7 +2733,7 @@ describe('forwarders', function() { mParticle.logEvent('foo', mParticle.EventType.Navigation); - const batch = JSON.parse(window.fetchMock.lastCall()[1].body); + const batch = JSON.parse(fetchMock.lastCall()[1].body); expect(batch).to.have.property('application_info'); expect(batch.application_info).not.to.have.property( diff --git a/test/src/tests-identities-attributes.js b/test/src/tests-identities-attributes.js index 9f2d1197..a1e98b84 100644 --- a/test/src/tests-identities-attributes.js +++ b/test/src/tests-identities-attributes.js @@ -1,5 +1,6 @@ import Utils from './utils'; import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { urls, apiKey, testMPID, MPConfig } from './config'; @@ -15,7 +16,7 @@ describe('identities and attributes', function() { mockServer = sinon.createFakeServer(); mockServer.respondImmediately = true; - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); mockServer.respondWith(urls.identify, [ 200, {}, @@ -26,7 +27,7 @@ describe('identities and attributes', function() { afterEach(function() { mockServer.restore(); - window.fetchMock.restore(); + fetchMock.restore(); }); it('should set user attribute', function(done) { @@ -39,7 +40,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test user attributes'); - const event = findBatch(window.fetchMock._calls, 'test user attributes'); + const event = findBatch(fetchMock.calls(), 'test user attributes'); event.should.have.property('user_attributes'); event.user_attributes.should.have.property('gender', 'male'); @@ -64,7 +65,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test user attributes'); - const event = findBatch(window.fetchMock._calls, 'test user attributes'); + const event = findBatch(fetchMock.calls(), 'test user attributes'); let cookies = getLocalStorage(); cookies[testMPID].ua.should.have.property('gender', 'female'); @@ -76,7 +77,7 @@ describe('identities and attributes', function() { mParticle.Identity.getCurrentUser().setUserAttribute('Gender', 'male'); mParticle.logEvent('test user attributes2'); - const event2 = findBatch(window.fetchMock._calls, 'test user attributes2'); + const event2 = findBatch(fetchMock.calls(), 'test user attributes2'); event2.user_attributes.should.have.property('Gender', 'male'); event2.user_attributes.should.not.have.property('gender'); @@ -99,7 +100,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test user attributes'); - const event = findBatch(window.fetchMock._calls, 'test user attributes'); + const event = findBatch(fetchMock.calls(), 'test user attributes'); const cookies = getLocalStorage(); cookies[testMPID].ua.should.have.property('gender', 'male'); @@ -123,7 +124,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test user attributes'); - const event = findBatch(window.fetchMock._calls, 'test user attributes'); + const event = findBatch(fetchMock.calls(), 'test user attributes'); event.user_attributes.should.not.have.property('gender'); const cookies = getLocalStorage(); @@ -143,7 +144,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test user attributes'); - const event = findBatch(window.fetchMock._calls, 'test user attributes'); + const event = findBatch(fetchMock.calls(), 'test user attributes'); event.user_attributes.should.not.have.property('Gender'); const cookies = getLocalStorage(); @@ -157,12 +158,12 @@ describe('identities and attributes', function() { mParticle.logEvent('test event'); - const event = findEventFromRequest(window.fetchMock._calls, 'test event'); + const event = findEventFromRequest(fetchMock.calls(), 'test event'); Should(event.data.custom_attributes).equal(null); mParticle.endSession(); - const sessionEndEvent = findEventFromRequest(window.fetchMock._calls, 'session_end'); + const sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); sessionEndEvent.data.custom_attributes.should.have.property('name', 'test'); @@ -175,7 +176,7 @@ describe('identities and attributes', function() { mParticle.endSession(); - const sessionEndEvent = findEventFromRequest(window.fetchMock._calls, 'session_end'); + const sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); sessionEndEvent.data.custom_attributes.should.have.property('name', 'test1'); sessionEndEvent.data.custom_attributes.should.not.have.property('Name'); @@ -186,13 +187,13 @@ describe('identities and attributes', function() { it("should not set a session attribute's key as an object or array)", function(done) { mParticle.setSessionAttribute({ key: 'value' }, 'test'); mParticle.endSession(); - const sessionEndEvent1 = findEventFromRequest(window.fetchMock._calls, 'session_end'); + const sessionEndEvent1 = findEventFromRequest(fetchMock.calls(), 'session_end'); mParticle.startNewSession(); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.setSessionAttribute(['test'], 'test'); mParticle.endSession(); - const sessionEndEvent2 = findEventFromRequest(window.fetchMock._calls, 'session_end'); + const sessionEndEvent2 = findEventFromRequest(fetchMock.calls(), 'session_end'); Object.keys(sessionEndEvent1.data.custom_attributes).length.should.equal(0); Object.keys(sessionEndEvent2.data.custom_attributes).length.should.equal(0); @@ -205,11 +206,11 @@ describe('identities and attributes', function() { mParticle.setSessionAttribute('name', 'test'); mParticle.endSession(); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.startNewSession(); mParticle.endSession(); - const sessionEndEvent = findEventFromRequest(window.fetchMock._calls, 'session_end'); + const sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); sessionEndEvent.data.custom_attributes.should.not.have.property('name'); @@ -220,7 +221,7 @@ describe('identities and attributes', function() { mParticle.setPosition(34.134103, -118.321694); mParticle.logEvent('Test Event'); - const event = findEventFromRequest(window.fetchMock._calls, 'Test Event'); + const event = findEventFromRequest(fetchMock.calls(), 'Test Event'); event.data.should.have.property('location'); event.data.location.should.have.property('latitude', 34.134103); @@ -234,7 +235,7 @@ describe('identities and attributes', function() { mParticle.logEvent('Test Event'); - const event = findBatch(window.fetchMock._calls, 'Test Event'); + const event = findBatch(fetchMock.calls(), 'Test Event'); event.should.have.property('user_attributes'); event.user_attributes.should.have.property('test', null); @@ -251,7 +252,7 @@ describe('identities and attributes', function() { mParticle.logEvent('Test Event'); - const event = findBatch(window.fetchMock._calls, 'Test Event'); + const event = findBatch(fetchMock.calls(), 'Test Event'); event.should.have.property('user_attributes'); event.user_attributes.should.not.have.property('Test'); @@ -269,7 +270,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test event'); - const event = findBatch(window.fetchMock._calls, 'test event'); + const event = findBatch(fetchMock.calls(), 'test event'); event.should.have.property('user_attributes'); event.user_attributes.should.not.have.property('test'); @@ -286,7 +287,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test event'); - const event = findBatch(window.fetchMock._calls, 'test event'); + const event = findBatch(fetchMock.calls(), 'test event'); event.should.have.property('user_attributes'); event.user_attributes.should.not.have.property('Test'); @@ -311,7 +312,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test user attributes'); - const event = findBatch(window.fetchMock._calls, 'test user attributes'); + const event = findBatch(fetchMock.calls(), 'test user attributes'); event.should.have.property('user_attributes'); event.user_attributes.should.have.property('numbers', [1, 2, 3, 4, 5]); @@ -344,7 +345,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test user attributes'); - const event = findBatch(window.fetchMock._calls, 'test user attributes'); + const event = findBatch(fetchMock.calls(), 'test user attributes'); const cookies = getLocalStorage(); event.should.have.property('user_attributes'); @@ -361,7 +362,7 @@ describe('identities and attributes', function() { ]); mParticle.logEvent('test user attributes2'); - const event2 = findBatch(window.fetchMock._calls, 'test user attributes2'); + const event2 = findBatch(fetchMock.calls(), 'test user attributes2'); const cookies3 = getLocalStorage(); event2.user_attributes.should.have.property('numbers', [1, 2, 3, 4, 5]); @@ -387,7 +388,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test user attributes'); - const event = findBatch(window.fetchMock._calls, 'test user attributes'); + const event = findBatch(fetchMock.calls(), 'test user attributes'); const cookies = getLocalStorage(); @@ -414,7 +415,7 @@ describe('identities and attributes', function() { mParticle.logEvent('test user attributes'); - const event = findBatch(window.fetchMock._calls, 'test user attributes'); + const event = findBatch(fetchMock.calls(), 'test user attributes'); const cookies = getLocalStorage(); event.should.have.property('user_attributes', {}); @@ -531,7 +532,7 @@ describe('identities and attributes', function() { mParticle.endSession(); - const sessionEndEvent = findEventFromRequest(window.fetchMock._calls, 'session_end'); + const sessionEndEvent = findEventFromRequest(fetchMock.calls(), 'session_end'); sessionEndEvent.data.custom_attributes.should.not.have.property('name'); @@ -543,7 +544,7 @@ describe('identities and attributes', function() { bad: 'bad', }); mParticle.logEvent('test bad user attributes1'); - const event1 = findBatch(window.fetchMock._calls, 'test bad user attributes1'); + const event1 = findBatch(fetchMock.calls(), 'test bad user attributes1'); mParticle.Identity.getCurrentUser().setUserAttribute('gender', [ 'bad', @@ -551,32 +552,32 @@ describe('identities and attributes', function() { 'bad', ]); mParticle.logEvent('test bad user attributes2'); - const event2 = findBatch(window.fetchMock._calls, 'test bad user attributes2'); + const event2 = findBatch(fetchMock.calls(), 'test bad user attributes2'); mParticle.Identity.getCurrentUser().setUserAttribute( { bad: 'bad' }, 'male' ); mParticle.logEvent('test bad user attributes3'); - const event3 = findBatch(window.fetchMock._calls, 'test bad user attributes3'); + const event3 = findBatch(fetchMock.calls(), 'test bad user attributes3'); mParticle.Identity.getCurrentUser().setUserAttribute( ['bad', 'bad', 'bad'], 'female' ); mParticle.logEvent('test bad user attributes4'); - const event4 = findBatch(window.fetchMock._calls, 'test bad user attributes4'); + const event4 = findBatch(fetchMock.calls(), 'test bad user attributes4'); mParticle.Identity.getCurrentUser().setUserAttribute(null, 'female'); mParticle.logEvent('test bad user attributes5'); - const event5 = findBatch(window.fetchMock._calls, 'test bad user attributes5'); + const event5 = findBatch(fetchMock.calls(), 'test bad user attributes5'); mParticle.Identity.getCurrentUser().setUserAttribute( undefined, 'female' ); mParticle.logEvent('test bad user attributes6'); - const event6 = findBatch(window.fetchMock._calls, 'test bad user attributes6'); + const event6 = findBatch(fetchMock.calls(), 'test bad user attributes6'); event1.should.have.property('user_attributes'); event1.user_attributes.should.not.have.property('gender'); @@ -622,10 +623,10 @@ describe('identities and attributes', function() { mParticle.init(apiKey, window.mParticle.config); // set a new attribute, age - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('age', '25'); - let body = JSON.parse(window.fetchMock.lastOptions().body) + let body = JSON.parse(fetchMock.lastOptions().body) body.user_attributes.should.have.property('age', '25') let event = body.events[0]; event.should.be.ok(); @@ -637,9 +638,9 @@ describe('identities and attributes', function() { event.data.is_new_attribute.should.equal(true); // change age attribute - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('age', '30'); - body = JSON.parse(window.fetchMock.lastOptions().body); + body = JSON.parse(fetchMock.lastOptions().body); body.user_attributes.should.have.property('age', '30') event = body.events[0]; event.event_type.should.equal('user_attribute_change'); @@ -650,9 +651,9 @@ describe('identities and attributes', function() { event.data.is_new_attribute.should.equal(false); // removes age attribute - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().removeUserAttribute('age'); - body = JSON.parse(window.fetchMock.lastOptions().body); + body = JSON.parse(fetchMock.lastOptions().body); body.user_attributes.should.not.have.property('age'); event = body.events[0]; event.event_type.should.equal('user_attribute_change'); @@ -663,13 +664,13 @@ describe('identities and attributes', function() { event.data.is_new_attribute.should.equal(false); // set a user attribute list - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttributeList('age', [ 'test1', 'test2', ]); - body = JSON.parse(window.fetchMock.lastOptions().body); + body = JSON.parse(fetchMock.lastOptions().body); body.user_attributes.age[0].should.equal('test1'); body.user_attributes.age[1].should.equal('test2'); @@ -688,17 +689,17 @@ describe('identities and attributes', function() { event.data.is_new_attribute.should.equal(true); // changes ordering of above attribute list - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttributeList('age', [ 'test2', 'test1', ]); - body = JSON.parse(window.fetchMock.lastOptions().body); + body = JSON.parse(fetchMock.lastOptions().body); body.user_attributes.age[0].should.equal('test2'); body.user_attributes.age[1].should.equal('test1'); - event = JSON.parse(window.fetchMock.lastOptions().body).events[0]; + event = JSON.parse(fetchMock.lastOptions().body).events[0]; event.event_type.should.equal('user_attribute_change'); obj = { test1: true, @@ -725,7 +726,7 @@ describe('identities and attributes', function() { mParticle.Identity.getCurrentUser().setUserAttribute('age', '25'); const testMPID = mParticle.Identity.getCurrentUser().getMPID(); - let body = JSON.parse(window.fetchMock.lastOptions().body) + let body = JSON.parse(fetchMock.lastOptions().body) body.mpid.should.equal(testMPID); let event = body.events[0] event.event_type.should.equal('user_attribute_change'); @@ -761,7 +762,7 @@ describe('identities and attributes', function() { testMPIDUser.getMPID().should.equal('testMPID'); anotherMPIDUser.setUserAttribute('age', '30'); - body = JSON.parse(window.fetchMock.lastOptions().body) + body = JSON.parse(fetchMock.lastOptions().body) event = body.events[0]; body.mpid.should.equal(anotherMPIDUser.getMPID()); event.event_type.should.equal('user_attribute_change'); @@ -772,7 +773,7 @@ describe('identities and attributes', function() { event.data.is_new_attribute.should.equal(true); testMPIDUser.setUserAttribute('age', '20'); - body = JSON.parse(window.fetchMock.lastOptions().body) + body = JSON.parse(fetchMock.lastOptions().body) body.mpid.should.equal(testMPIDUser.getMPID()); event = body.events[0]; event.event_type.should.equal('user_attribute_change'); @@ -784,7 +785,7 @@ describe('identities and attributes', function() { // remove user attribute anotherMPIDUser.removeUserAttribute('age'); - body = JSON.parse(window.fetchMock.lastOptions().body) + body = JSON.parse(fetchMock.lastOptions().body) body.mpid.should.equal(anotherMPIDUser.getMPID()); event = body.events[0]; event.event_type.should.equal('user_attribute_change'); @@ -796,7 +797,7 @@ describe('identities and attributes', function() { testMPIDUser.removeUserAttribute('age'); - body = JSON.parse(window.fetchMock.lastOptions().body) + body = JSON.parse(fetchMock.lastOptions().body) body.mpid.should.equal(testMPIDUser.getMPID()); event = body.events[0]; event.event_type.should.equal('user_attribute_change'); @@ -814,7 +815,7 @@ describe('identities and attributes', function() { it('should send user identity change requests when setting new identities on new users', function(done) { mParticle._resetForTests(MPConfig); - window.fetchMock._calls = []; + fetchMock.resetHistory(); window.mParticle.config.identifyRequest = { userIdentities: { @@ -824,16 +825,16 @@ describe('identities and attributes', function() { mParticle.init(apiKey, window.mParticle.config); - JSON.parse(window.fetchMock.lastOptions().body).user_identities.should.have.property( + JSON.parse(fetchMock.lastOptions().body).user_identities.should.have.property( 'email', 'initial@gmail.com' ); mParticle.logEvent('testAfterInit'); - JSON.parse(window.fetchMock.lastOptions().body).user_identities.should.have.property('email', 'initial@gmail.com'); + JSON.parse(fetchMock.lastOptions().body).user_identities.should.have.property('email', 'initial@gmail.com'); - window.fetchMock.calls().forEach(call => { + fetchMock.calls().forEach(call => { JSON.parse(call[1].body).user_identities.should.have.property('email', 'initial@gmail.com') }); @@ -843,7 +844,7 @@ describe('identities and attributes', function() { JSON.stringify({ mpid: 'anotherMPID', is_logged_in: true }), ]); - window.fetchMock._calls = []; + fetchMock.resetHistory(); // anonymous user is in storage, new user logs in const loginUser = { @@ -853,7 +854,7 @@ describe('identities and attributes', function() { }; mParticle.Identity.login(loginUser); - let body = JSON.parse(window.fetchMock.lastOptions().body); + let body = JSON.parse(fetchMock.lastOptions().body); // should be the new MPID body.mpid.should.equal('anotherMPID'); @@ -873,7 +874,7 @@ describe('identities and attributes', function() { event.data.old.created_this_batch.should.equal(false); mParticle.logEvent('testAfterLogin'); - body = JSON.parse(window.fetchMock.lastOptions().body); + body = JSON.parse(fetchMock.lastOptions().body); body.user_identities.should.have.property('customer_id', 'customerid1'); body.user_identities.should.not.have.property('email'); @@ -891,7 +892,7 @@ describe('identities and attributes', function() { ]); mParticle.Identity.modify(modifyUser); - const body2 = JSON.parse(window.fetchMock.lastOptions().body); + const body2 = JSON.parse(fetchMock.lastOptions().body); body2.mpid.should.equal('anotherMPID'); body2.user_identities.should.have.property('customer_id', 'customerid2'); body2.user_identities.should.not.have.property('email'); @@ -916,11 +917,11 @@ describe('identities and attributes', function() { }, }; - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.modify(modifyUser2); - const body3 = JSON.parse(window.fetchMock.lastOptions().body); + const body3 = JSON.parse(fetchMock.lastOptions().body); body3.mpid.should.equal('anotherMPID'); const event3 = body3.events[0]; @@ -942,7 +943,7 @@ describe('identities and attributes', function() { other: 'other1', }, }; - window.fetchMock._calls = []; + fetchMock.resetHistory(); mockServer.respondWith(urls.logout, [ 200, @@ -952,8 +953,8 @@ describe('identities and attributes', function() { mParticle.Identity.logout(logoutUser); //only call is for `other` change event, not for previous ID types of email and customerid - window.fetchMock._calls.length.should.equal(1); - const body4 = JSON.parse(window.fetchMock.lastOptions().body); + fetchMock.calls().length.should.equal(1); + const body4 = JSON.parse(fetchMock.lastOptions().body); body4.mpid.should.equal('mpid2'); const event4 = body4.events[0]; @@ -971,7 +972,7 @@ describe('identities and attributes', function() { mParticle.logEvent('testAfterLogout'); - const body5 = JSON.parse(window.fetchMock.lastOptions().body); + const body5 = JSON.parse(fetchMock.lastOptions().body); body5.mpid.should.equal('mpid2'); Object.keys(body5.user_identities).length.should.equal(1); body5.user_identities.should.have.property('other', 'other1'); @@ -1009,7 +1010,7 @@ describe('identities and attributes', function() { mParticle.Identity.login(loginUser); - let batch = JSON.parse(window.fetchMock.lastOptions().body); + let batch = JSON.parse(fetchMock.lastOptions().body); batch.mpid.should.equal(testMPID); batch.user_identities.should.have.property('email', 'initial@gmail.com'); batch.user_identities.should.have.property('customer_id', 'customerid1'); @@ -1028,13 +1029,13 @@ describe('identities and attributes', function() { mParticle.Identity.logout(logoutUser); - batch = JSON.parse(window.fetchMock.lastOptions().body); + batch = JSON.parse(fetchMock.lastOptions().body); batch.mpid.should.equal('mpid2'); batch.user_identities.should.have.property('other', 'other1'); batch.user_identities.should.not.have.property('email'); batch.user_identities.should.not.have.property('customer_id'); - window.fetchMock._calls = []; + fetchMock.resetHistory(); // log back in with previous MPID, but with only a single UI, all UIs should be on batch mockServer.respondWith(urls.login, [ 200, @@ -1045,10 +1046,10 @@ describe('identities and attributes', function() { mParticle.Identity.login(loginUser); // switching back to logged in user shoudl not result in any UIC events - Should(window.fetchMock.lastOptions()).not.be.ok() + Should(fetchMock.lastOptions()).not.be.ok() mParticle.logEvent('event after logging back in') - batch = JSON.parse(window.fetchMock.lastOptions().body); + batch = JSON.parse(fetchMock.lastOptions().body); batch.mpid.should.equal(testMPID); batch.user_identities.should.have.property('email', 'initial@gmail.com'); batch.user_identities.should.have.property('customer_id', 'customerid1'); @@ -1066,9 +1067,9 @@ describe('identities and attributes', function() { mParticle.init(apiKey, window.mParticle.config); // set a new attribute, age - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('age', '25'); - const body1 = JSON.parse(window.fetchMock.lastOptions().body) + const body1 = JSON.parse(fetchMock.lastOptions().body) body1.user_attributes.should.have.property('age', '25') const event1 = body1.events[0]; event1.should.be.ok(); @@ -1082,9 +1083,9 @@ describe('identities and attributes', function() { // test setting attributes with 'false' values (i.e false, 0 and '') // check for UAC event for testFalse: fasle when set for first time - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('testFalse', false); - const body2 = JSON.parse(window.fetchMock.lastOptions().body) + const body2 = JSON.parse(fetchMock.lastOptions().body) body2.user_attributes.should.have.property('testFalse', false) const event2 = body2.events[0]; event2.should.be.ok(); @@ -1096,9 +1097,9 @@ describe('identities and attributes', function() { event2.data.is_new_attribute.should.equal(true); // check for UAC event for testEmptyString: '' when set for first time - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('testEmptyString', ''); - const body3 = JSON.parse(window.fetchMock.lastOptions().body) + const body3 = JSON.parse(fetchMock.lastOptions().body) body3.user_attributes.should.have.property('testEmptyString', '') const event3 = body3.events[0]; event3.should.be.ok(); @@ -1110,9 +1111,9 @@ describe('identities and attributes', function() { event3.data.is_new_attribute.should.equal(true); // check for UAC event for testZero: 0 when set for first time - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('testZero', 0); - const body4 = JSON.parse(window.fetchMock.lastOptions().body) + const body4 = JSON.parse(fetchMock.lastOptions().body) body4.user_attributes.should.have.property('testZero', 0) const event4 = body4.events[0]; event4.should.be.ok(); @@ -1131,13 +1132,13 @@ describe('identities and attributes', function() { userAttributes.should.have.property('testZero'); // re-set all previous attributes with the same values - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('age', '25'); mParticle.Identity.getCurrentUser().setUserAttribute('testFalse', false); mParticle.Identity.getCurrentUser().setUserAttribute('testEmptyString', ''); mParticle.Identity.getCurrentUser().setUserAttribute('testZero', 0); - (window.fetchMock.lastOptions() === undefined).should.equal(true); - (window.fetchMock._calls.length === 0).should.equal(true); + (fetchMock.lastOptions() === undefined).should.equal(true); + (fetchMock.calls().length === 0).should.equal(true); done() }); @@ -1152,9 +1153,9 @@ describe('identities and attributes', function() { mParticle.init(apiKey, window.mParticle.config); // set initial test attribute with 'falsey' value to 0 - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('testFalsey', 0); - const body1 = JSON.parse(window.fetchMock.lastOptions().body) + const body1 = JSON.parse(fetchMock.lastOptions().body) body1.user_attributes.should.have.property('testFalsey', 0) const event1 = body1.events[0]; event1.should.be.ok(); @@ -1166,9 +1167,9 @@ describe('identities and attributes', function() { event1.data.is_new_attribute.should.equal(true); // re-set same test attribute with 'falsey' value to '' - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('testFalsey', ''); - const body2 = JSON.parse(window.fetchMock.lastOptions().body) + const body2 = JSON.parse(fetchMock.lastOptions().body) body2.user_attributes.should.have.property('testFalsey', '') const event2 = body2.events[0]; event2.should.be.ok(); @@ -1180,9 +1181,9 @@ describe('identities and attributes', function() { event2.data.is_new_attribute.should.equal(false); // re-set same test attribute with 'falsey' value to false - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('testFalsey', false); - const body3 = JSON.parse(window.fetchMock.lastOptions().body) + const body3 = JSON.parse(fetchMock.lastOptions().body) body3.user_attributes.should.have.property('testFalsey', false) const event3 = body3.events[0]; event3.should.be.ok(); @@ -1194,9 +1195,9 @@ describe('identities and attributes', function() { event3.data.is_new_attribute.should.equal(false); // re-set same test attribute with 'falsey' value to original value 0 - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.Identity.getCurrentUser().setUserAttribute('testFalsey', 0); - const body4 = JSON.parse(window.fetchMock.lastOptions().body) + const body4 = JSON.parse(fetchMock.lastOptions().body) body4.user_attributes.should.have.property('testFalsey', 0) const event4 = body4.events[0]; event4.should.be.ok(); diff --git a/test/src/tests-identity.js b/test/src/tests-identity.js index 4117f140..91f54fe4 100644 --- a/test/src/tests-identity.js +++ b/test/src/tests-identity.js @@ -1,6 +1,7 @@ import Constants from '../../src/constants'; import Utils from './utils'; import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { urls, apiKey, testMPID, MPConfig, @@ -22,7 +23,7 @@ let mockServer; describe('identity', function() { beforeEach(function() { delete mParticle.config.useCookieStorage; - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); mockServer = sinon.createFakeServer(); mockServer.respondImmediately = true; @@ -36,7 +37,7 @@ describe('identity', function() { afterEach(function() { mockServer.restore(); - window.fetchMock.restore(); + fetchMock.restore(); mParticle._resetForTests(MPConfig); }); @@ -1011,10 +1012,10 @@ describe('identity', function() { mParticle.init(apiKey, window.mParticle.config); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.logEvent('Test Event1'); - let testEvent1 = findEventFromRequest(window.fetchMock._calls, 'Test Event1'); + let testEvent1 = findEventFromRequest(fetchMock.calls(), 'Test Event1'); Should(testEvent1).not.be.ok(); @@ -1027,12 +1028,12 @@ describe('identity', function() { mParticle.logEvent('Test Event2'); mParticle.Identity.login(); // server requests will have AST, sessionStart, Test1, Test2, and login - testEvent1 = findEventFromRequest(window.fetchMock._calls, 'Test Event1'); - window.fetchMock._calls.length.should.equal(4); + testEvent1 = findEventFromRequest(fetchMock.calls(), 'Test Event1'); + fetchMock.calls().length.should.equal(4); - const testEvent2 = findEventFromRequest(window.fetchMock._calls, 'Test Event2'); - const ASTEvent = findEventFromRequest(window.fetchMock._calls, 'application_state_transition'); - const sessionStartEvent = findEventFromRequest(window.fetchMock._calls, 'session_start'); + const testEvent2 = findEventFromRequest(fetchMock.calls(), 'Test Event2'); + const ASTEvent = findEventFromRequest(fetchMock.calls(), 'application_state_transition'); + const sessionStartEvent = findEventFromRequest(fetchMock.calls(), 'session_start'); const loginEvent = getIdentityEvent(mockServer.requests, 'login'); Should(testEvent1).be.ok(); @@ -1276,7 +1277,7 @@ describe('identity', function() { mParticle.logEvent('Test Event1'); - const testEvent1Batch = findBatch(window.fetchMock._calls, 'Test Event1'); + const testEvent1Batch = findBatch(fetchMock.calls(), 'Test Event1'); testEvent1Batch.user_attributes.should.have.property('foo1', 'bar1'); testEvent1Batch.user_identities.should.have.property('customer_id', 'customerid1'); @@ -1309,7 +1310,7 @@ describe('identity', function() { mParticle.Identity.logout(user2); mParticle.logEvent('Test Event2'); - const testEvent2Batch = findBatch(window.fetchMock._calls, 'Test Event2'); + const testEvent2Batch = findBatch(fetchMock.calls(), 'Test Event2'); Object.keys(testEvent2Batch.user_attributes).length.should.equal(0); testEvent2Batch.user_identities.should.have.property('customer_id', 'customerid2'); @@ -1322,7 +1323,7 @@ describe('identity', function() { mParticle.Identity.login(user1); mParticle.logEvent('Test Event3'); - const testEvent3Batch = findBatch(window.fetchMock._calls, 'Test Event3'); + const testEvent3Batch = findBatch(fetchMock.calls(), 'Test Event3'); testEvent3Batch.user_attributes.should.have.property('foo1', 'bar1'); Object.keys(testEvent3Batch.user_identities).length.should.equal(2); @@ -1688,7 +1689,7 @@ describe('identity', function() { mParticle.eCommerce.logCheckout(1); - const checkoutEvent = findEventFromRequest(window.fetchMock._calls, 'checkout'); + const checkoutEvent = findEventFromRequest(fetchMock.calls(), 'checkout'); checkoutEvent.data.product_action.should.have.property('products', null) @@ -1699,10 +1700,10 @@ describe('identity', function() { ]); mParticle.Identity.login(identityAPIRequest1); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.eCommerce.logCheckout(1); - const checkoutEvent2 = findEventFromRequest(window.fetchMock._calls, 'checkout'); + const checkoutEvent2 = findEventFromRequest(fetchMock.calls(), 'checkout'); checkoutEvent2.data.product_action.should.have.property('products', null); @@ -2023,14 +2024,14 @@ describe('identity', function() { mParticle.Identity.getCurrentUser().setUserAttribute('foo', 'bar'); }; - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.init(apiKey, window.mParticle.config); - (window.fetchMock._calls.length === 0).should.equal.true + (fetchMock.calls().length === 0).should.equal.true clock.tick(1000); - const sessionStartEventBatch = findBatch(window.fetchMock._calls, 'session_start'); - const ASTEventBatch = findBatch(window.fetchMock._calls, 'application_state_transition'); + const sessionStartEventBatch = findBatch(fetchMock.calls(), 'session_start'); + const ASTEventBatch = findBatch(fetchMock.calls(), 'application_state_transition'); sessionStartEventBatch.user_attributes.should.have.property('foo', 'bar'); ASTEventBatch.user_attributes.should.have.property('foo', 'bar'); @@ -2064,10 +2065,10 @@ describe('identity', function() { JSON.stringify({ mpid: 'MPID1', is_logged_in: false }), ]); - window.fetchMock._calls = []; + fetchMock.resetHistory(); mParticle.init(apiKey, window.mParticle.config); //the only server request is the AST, there is no request to Identity - window.fetchMock._calls.length.should.equal(1); + fetchMock.calls().length.should.equal(1); result.should.have.properties('body', 'httpCode', 'getUser'); result.httpCode.should.equal(-3); diff --git a/test/src/tests-main.js b/test/src/tests-main.js index ced4ef28..6f0456f3 100644 --- a/test/src/tests-main.js +++ b/test/src/tests-main.js @@ -27,34 +27,30 @@ beforeEach(function() { delete mParticle._instances['default_instance']; }); -afterEach(function() { - // window.fetchMock.restore(); -}); - import './tests-core-sdk'; -// import './tests-batchUploader'; -// import './tests-beaconUpload'; -// import './tests-kit-blocking'; -// import './tests-persistence'; -// import './tests-forwarders'; -// import './tests-helpers'; -// import './tests-identity'; -// import './tests-event-logging'; -// import './tests-eCommerce'; -// import './tests-cookie-syncing'; -// import './tests-identities-attributes'; -// import './tests-native-sdk'; -// import './tests-consent'; -// import './tests-serverModel'; -// import './tests-mockBatchCreator.ts'; -// import './tests-mParticleUser'; -// import './tests-self-hosting-specific'; -// import './tests-runtimeToBatchEventsDTO'; -// import './tests-apiClient'; -// import './tests-mparticle-instance-manager'; -// import './tests-queue-public-methods'; -// import './tests-validators'; -// import './tests-utils'; -// import './tests-session-manager'; -// import './tests-store'; -// import './tests-config-api-client'; +import './tests-batchUploader'; +import './tests-beaconUpload'; +import './tests-kit-blocking'; +import './tests-persistence'; +import './tests-forwarders'; +import './tests-helpers'; +import './tests-identity'; +import './tests-event-logging'; +import './tests-eCommerce'; +import './tests-cookie-syncing'; +import './tests-identities-attributes'; +import './tests-native-sdk'; +import './tests-consent'; +import './tests-serverModel'; +import './tests-mockBatchCreator.ts'; +import './tests-mParticleUser'; +import './tests-self-hosting-specific'; +import './tests-runtimeToBatchEventsDTO'; +import './tests-apiClient'; +import './tests-mparticle-instance-manager'; +import './tests-queue-public-methods'; +import './tests-validators'; +import './tests-utils'; +import './tests-session-manager'; +import './tests-store'; +import './tests-config-api-client'; diff --git a/test/src/tests-mparticle-instance-manager.js b/test/src/tests-mparticle-instance-manager.js index 0a9579ee..55299dab 100644 --- a/test/src/tests-mparticle-instance-manager.js +++ b/test/src/tests-mparticle-instance-manager.js @@ -1,4 +1,5 @@ import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { urls, MPConfig } from './config'; import Utils from './utils'; const findEventFromRequest = Utils.findEventFromRequest; @@ -205,15 +206,15 @@ describe('mParticle instance manager', function() { ); // default instance event mock - window.fetchMock.post( + fetchMock.post( 'https://jssdks.mparticle.com/v3/JS/apiKey1/events', 200 ); - window.fetchMock.post( + fetchMock.post( 'https://jssdks.mparticle.com/v3/JS/apiKey2/events', 200 ); - window.fetchMock.post( + fetchMock.post( 'https://jssdks.mparticle.com/v3/JS/apiKey3/events', 200 ); @@ -258,63 +259,63 @@ describe('mParticle instance manager', function() { mParticle.getInstance('instance3').logEvent('hi3'); const instance1Event = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey1', 'hi1' ); instance1Event.should.be.ok(); const instance2Event = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey2', 'hi2' ); instance2Event.should.be.ok(); const instance3Event = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey3', 'hi3' ); instance3Event.should.be.ok(); const instance1EventsFail1 = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey1', 'hi2' ); Should(instance1EventsFail1).not.be.ok(); const instance1EventsFail2 = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey1', 'hi3' ); Should(instance1EventsFail2).not.be.ok(); const instance2EventsFail1 = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey2', 'hi1' ); Should(instance2EventsFail1).not.be.ok(); const instance2EventsFail2 = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey2', 'hi3' ); Should(instance2EventsFail2).not.be.ok(); const instance3EventsFail1 = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey3', 'hi1' ); Should(instance3EventsFail1).not.be.ok(); const instance3EventsFail2 = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey3', 'hi2' ); @@ -371,17 +372,17 @@ describe('mParticle instance manager', function() { .eCommerce.logPurchase(ta, [product1, product2]); const instance1Event = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey1', 'purchase' ); let instance2Event = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey2', 'purchase' ); let instance3Event = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey3', 'purchase' ); @@ -394,12 +395,12 @@ describe('mParticle instance manager', function() { .eCommerce.logPurchase(ta, [product1, product2]); instance2Event = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey2', 'purchase' ); instance3Event = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey3', 'purchase' ); @@ -412,7 +413,7 @@ describe('mParticle instance manager', function() { .eCommerce.logPurchase(ta, [product1, product2]); instance3Event = returnEventForMPInstance( - window.fetchMock._calls, + fetchMock.calls(), 'apiKey3', 'purchase' ); diff --git a/test/src/tests-persistence.ts b/test/src/tests-persistence.ts index f20bbabe..b3333bf0 100644 --- a/test/src/tests-persistence.ts +++ b/test/src/tests-persistence.ts @@ -1,5 +1,6 @@ import Utils from './utils'; import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { urls, @@ -33,7 +34,7 @@ let mockServer; describe('migrations and persistence-related', () => { beforeEach(() => { - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); mockServer = sinon.createFakeServer(); mockServer.respondImmediately = true; @@ -47,7 +48,7 @@ describe('migrations and persistence-related', () => { afterEach(() => { mockServer.restore(); - window.fetchMock.restore(); + fetchMock.restore(); }); it('should move new schema from cookies to localStorage with useCookieStorage = false', done => { @@ -431,9 +432,9 @@ describe('migrations and persistence-related', () => { it('should save integration attributes properly on a page refresh', done => { mParticle.setIntegrationAttribute(128, { MCID: 'abcedfg' }); mParticle.init(apiKey, mParticle.config); - + debugger; mParticle.logEvent('Test Event'); - const testEvent = findBatch(window.fetchMock._calls, 'Test Event'); + const testEvent = findBatch(fetchMock.calls(), 'Test Event'); testEvent.integration_attributes.should.have.property('128'); testEvent.integration_attributes['128'].should.have.property('MCID', 'abcedfg'); diff --git a/test/src/tests-self-hosting-specific.js b/test/src/tests-self-hosting-specific.js index dfeb71c9..0262fc0d 100644 --- a/test/src/tests-self-hosting-specific.js +++ b/test/src/tests-self-hosting-specific.js @@ -1,5 +1,6 @@ import Utils from './utils'; import sinon from 'sinon'; +import fetchMock from 'fetch-mock/esm/client'; import { urls, apiKey, MPConfig } from './config'; const { findEventFromRequest, findBatch } = Utils; @@ -9,12 +10,12 @@ let mockServer; // Calls to /config are specific to only the self hosting environment describe('/config self-hosting integration tests', function() { beforeEach(function() { - window.fetchMock.post(urls.events, 200); + fetchMock.post(urls.events, 200); mockServer = sinon.createFakeServer(); }); afterEach(function() { - window.fetchMock.restore(); + fetchMock.restore(); sinon.restore(); }) @@ -47,13 +48,13 @@ describe('/config self-hosting integration tests', function() { // log event before config and identify come back mParticle.logEvent('Test'); - let event = findEventFromRequest(window.fetchMock._calls, 'Test'); + let event = findEventFromRequest(fetchMock.calls(), 'Test'); Should(event).not.be.ok(); // config and identify now get triggered, which runs through the event queue clock.tick(300); - event = findBatch(window.fetchMock._calls, 'Test'); + event = findBatch(fetchMock.calls(), 'Test'); event.should.be.ok(); event.mpid.should.equal('identifyMPID'); @@ -112,7 +113,7 @@ describe('/config self-hosting integration tests', function() { // config triggers, login triggers immediately before identify clock.tick(300); - const event1 = findBatch(window.fetchMock._calls, 'Test'); + const event1 = findBatch(fetchMock.calls(), 'Test'); event1.mpid.should.equal('loginMPID'); messages .indexOf('Parsing "login" identity response from server') @@ -122,7 +123,7 @@ describe('/config self-hosting integration tests', function() { messages .indexOf('Parsing "identify" identity response from server') .should.equal(-1); - const event2 = findBatch(window.fetchMock._calls, 'identify callback event', false, mockServer); + const event2 = findBatch(fetchMock.calls(), 'identify callback event', false, mockServer); event2.mpid.should.equal('loginMPID'); mockServer.restore(); diff --git a/test/src/utils.js b/test/src/utils.js index 6887794b..4dc27e89 100644 --- a/test/src/utils.js +++ b/test/src/utils.js @@ -222,7 +222,7 @@ var pluses = /\+/g, findRequest = function(requests, eventName) { let matchingRequest; requests.forEach(function(request) { - var batch = JSON.parse(request.options.body); + var batch = JSON.parse(request[1].body); for (var i = 0; i