diff --git a/test/src/config/utils.js b/test/src/config/utils.js index a6582ae84..d94ee8e84 100644 --- a/test/src/config/utils.js +++ b/test/src/config/utils.js @@ -635,7 +635,7 @@ var pluses = /\+/g, return window.mParticle.Identity.getCurrentUser()?.getMPID() === testMPID; }, hasIdentityCallInflightReturned = () => !mParticle.getInstance()?._Store?.identityCallInFlight, - hasConfigLoaded = () => !!mParticle.getInstance()?._Store?.configurationLoaded + hasConfigurationReturned = () => !!mParticle.getInstance()?._Store?.configurationLoaded; var TestsCore = { getLocalStorageProducts: getLocalStorageProducts, @@ -664,7 +664,7 @@ var TestsCore = { fetchMockSuccess: fetchMockSuccess, hasIdentifyReturned: hasIdentifyReturned, hasIdentityCallInflightReturned, - hasConfigLoaded, + hasConfigurationReturned, }; export default TestsCore; \ No newline at end of file diff --git a/test/src/tests-batchUploader_3.ts b/test/src/tests-batchUploader_3.ts index d578be16a..9009250ee 100644 --- a/test/src/tests-batchUploader_3.ts +++ b/test/src/tests-batchUploader_3.ts @@ -229,7 +229,7 @@ describe('batch uploader', () => { }) }); - it('should call the identity callback after a session ends if user is returning to the page after a long period of time', async function() { + it('should call the identity callback after a session ends if user is returning to the page after a long period of time', async () => { // Background of bug that this test fixes: // User navigates away from page and returns after some time // and the session should end. There is a UAC firing inside of diff --git a/test/src/tests-cookie-syncing.ts b/test/src/tests-cookie-syncing.ts index 021943787..6f144795f 100644 --- a/test/src/tests-cookie-syncing.ts +++ b/test/src/tests-cookie-syncing.ts @@ -6,7 +6,12 @@ import { IMParticleUser } from '../../src/identity-user-interfaces'; import { IPixelConfiguration } from '../../src/cookieSyncManager'; import { IConsentRules } from '../../src/consent'; import { IMParticleInstanceManager } from '../../src/sdkRuntimeModels'; -const { fetchMockSuccess, waitForCondition, hasIdentifyReturned } = Utils; +const { + fetchMockSuccess, + waitForCondition, + hasIdentifyReturned, + hasConfigurationReturned +} = Utils; const { setLocalStorage, MockForwarder, getLocalStorage } = Utils; @@ -32,7 +37,6 @@ declare global { const mParticle = window.mParticle; describe('cookie syncing', function() { - const timeout = 100; // Have a reference to createElement function to reset after all cookie sync // tests have run const originalCreateElementFunction = window.document.createElement; @@ -45,11 +49,17 @@ describe('cookie syncing', function() { const element = create.apply(this, arguments as unknown as [string, ElementCreationOptions?]); if (element.tagName === 'IMG') { - setTimeout(() => { - element.onload(new Event('load')); - }, 10); + // Add an `onload` mock that simulates the browser loading the image + Object.defineProperty(element, 'onload', { + set(callback) { + // Automatically invoke the callback to simulate the `load` event + callback(new Event('load')); + }, + }); } + return element; + }; })(document.createElement); }); @@ -74,26 +84,21 @@ describe('cookie syncing', function() { mParticle._resetForTests(MPConfig); }); - it('should sync cookies when there was not a previous cookie-sync', function(done) { + it('should sync cookies when there was not a previous cookie-sync', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.pixelConfigs = [pixelSettings]; mParticle.init(apiKey, window.mParticle.config); - waitForCondition(hasIdentifyReturned) - .then(() => { - setTimeout(function() { - expect( - mParticle.getInstance()._Store.pixelConfigurations.length - ).to.equal(1); - const data = mParticle.getInstance()._Persistence.getLocalStorage(); - data[testMPID].csd.should.have.property('5'); - - done(); - }, timeout); - }) + await waitForCondition(hasIdentifyReturned) + + expect( + mParticle.getInstance()._Store.pixelConfigurations.length + ).to.equal(1); + const data = mParticle.getInstance()._Persistence.getLocalStorage(); + data[testMPID].csd.should.have.property('5'); }); - it('should sync cookies when current date is beyond the frequency cap and the MPID has not changed', function(done) { + it('should sync cookies when current date is beyond the frequency cap and the MPID has not changed', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.pixelConfigs = [pixelSettings]; @@ -106,22 +111,19 @@ describe('cookie syncing', function() { ie: true, }); mParticle.init(apiKey, window.mParticle.config); + await waitForCondition(hasIdentifyReturned) - setTimeout(function() { - expect( - mParticle.getInstance()._Store.pixelConfigurations.length - ).to.equal(1); - - const data = mParticle.getInstance()._Persistence.getLocalStorage(); - const updated = data[testMPID].csd['5'] > 500; + expect( + mParticle.getInstance()._Store.pixelConfigurations.length + ).to.equal(1); - expect(updated).to.be.ok; + const data = mParticle.getInstance()._Persistence.getLocalStorage(); + const updated = data[testMPID].csd['5'] > 500; - done(); - }, timeout); + expect(updated).to.be.ok; }); - it('should not sync cookies when last date is within frequencyCap', function(done) { + it('should not sync cookies when last date is within frequencyCap', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.pixelConfigs = [pixelSettings]; @@ -129,68 +131,48 @@ describe('cookie syncing', function() { setLocalStorage(); mParticle.init(apiKey, window.mParticle.config); - waitForCondition(hasIdentifyReturned) - .then(() => { - setTimeout(function() { - - const data = mParticle.getInstance()._Persistence.getLocalStorage(); - - data[testMPID].csd.should.have.property( - 5, - mParticle.getInstance()._Persistence.getLocalStorage().testMPID - .csd['5'] - ); - expect( - mParticle.getInstance()._Store.pixelConfigurations.length - ).to.equal(1); - - done(); - }, timeout); - }) + await waitForCondition(hasIdentifyReturned) + const data = mParticle.getInstance()._Persistence.getLocalStorage(); + + data[testMPID].csd.should.have.property( + 5, + mParticle.getInstance()._Persistence.getLocalStorage().testMPID + .csd['5'] + ); + expect( + mParticle.getInstance()._Store.pixelConfigurations.length + ).to.equal(1); }); - it('should sync cookies when mpid changes', function(done) { + it('should sync cookies when mpid changes', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.pixelConfigs = [pixelSettings]; mParticle.init(apiKey, window.mParticle.config); - waitForCondition(hasIdentifyReturned) - .then(() => { - setTimeout(function() { - const data1 = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - - fetchMockSuccess(urls.login, { - mpid: 'otherMPID', is_logged_in: false - }); - - mParticle.Identity.login(); - waitForCondition(() => { - return ( - mParticle.Identity.getCurrentUser()?.getMPID() === 'otherMPID' - ); - }) - .then(() => { - setTimeout(function() { - const data2 = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - expect(data1[testMPID].csd[5]).to.be.ok; - expect(data2['otherMPID'].csd[5]).to.be.ok; - expect( - mParticle.getInstance()._Store.pixelConfigurations.length - ).to.equal(1); - - done(); - }, timeout); - }) - }, timeout); - }) + await waitForCondition(hasIdentifyReturned) + + const data1 = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + + fetchMockSuccess(urls.login, { + mpid: 'otherMPID', is_logged_in: false + }); + + mParticle.Identity.login(); + await waitForCondition(() => mParticle.Identity.getCurrentUser()?.getMPID() === 'otherMPID') + const data2 = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + expect(data1[testMPID].csd[5]).to.be.ok; + expect(data2['otherMPID'].csd[5]).to.be.ok; + expect( + mParticle.getInstance()._Store.pixelConfigurations.length + ).to.equal(1); }); - it('should not sync cookies when pixelSettings.isDebug is false, pixelSettings.isProduction is true, and mParticle.config.isDevelopmentMode is true', function(done) { + it('should not sync cookies when pixelSettings.isDebug is false, pixelSettings.isProduction is true, and mParticle.config.isDevelopmentMode is true', async () => { const pixelSettings = { name: 'TestPixel', moduleId: 5, @@ -208,22 +190,19 @@ describe('cookie syncing', function() { window.mParticle.config.pixelConfigs = [pixelSettings]; mParticle.init(apiKey, window.mParticle.config); + await waitForCondition(hasIdentifyReturned) - setTimeout(function() { - const data1 = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - - Object.keys(data1[testMPID]).should.not.have.property('csd'); - expect( - mParticle.getInstance()._Store.pixelConfigurations.length - ).to.equal(0); + const data1 = mParticle + .getInstance() + ._Persistence.getLocalStorage(); - done(); - }, 500); + Object.keys(data1[testMPID]).should.not.have.property('csd'); + expect( + mParticle.getInstance()._Store.pixelConfigurations.length + ).to.equal(0); }); - it('should not sync cookies when pixelSettings.isDebug is true, pixelSettings.isProduction is false, and mParticle.config.isDevelopmentMode is false', function(done) { + it('should not sync cookies when pixelSettings.isDebug is true, pixelSettings.isProduction is false, and mParticle.config.isDevelopmentMode is false', async () => { const pixelSettings = { name: 'TestPixel', moduleId: 5, @@ -241,21 +220,18 @@ describe('cookie syncing', function() { window.mParticle.config.pixelConfigs = [pixelSettings]; mParticle.init(apiKey, window.mParticle.config); + await waitForCondition(hasIdentifyReturned) - setTimeout(function() { - const data1 = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - data1[testMPID].should.not.have.property('csd'); - expect( - mParticle.getInstance()._Store.pixelConfigurations.length - ).to.equal(0); - - done(); - }, timeout); - }); - - it('parse and capture pixel settings properly from backend', function(done) { + const data1 = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + data1[testMPID].should.not.have.property('csd'); + expect( + mParticle.getInstance()._Store.pixelConfigurations.length + ).to.equal(0); + }) + + it('parse and capture pixel settings properly from backend', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.requestConfig = true; // create some cookies @@ -288,45 +264,25 @@ describe('cookie syncing', function() { body: JSON.stringify(forwarderConfigurationResult), }); - waitForCondition(hasIdentifyReturned) - .then(() => { + await waitForCondition(hasIdentifyReturned) // add pixels to preInitConfig mParticle.init(apiKey, window.mParticle.config); - waitForCondition(() => { - return ( - mParticle.getInstance()._Store.configurationLoaded === true - ); - }) - .then(() => { - setTimeout(function() { - mParticle - .getInstance() - ._Store.pixelConfigurations.length.should.equal(1); - - fetchMockSuccess(urls.login, { - mpid: 'MPID1', is_logged_in: false - }); - // force the preInit cookie configurations to fire - mParticle.Identity.login({ - userIdentities: { customerid: 'abc' }, - }); - waitForCondition(() => { - return ( - mParticle.Identity.getCurrentUser()?.getMPID() === 'MPID1' - ); - }) - .then(() => { - setTimeout(function() { - const cookies = getLocalStorage(); - Object.keys(cookies['MPID1'].csd).length.should.equal(1); - - done(); - }, timeout); - }) - }, timeout); - }) - }) + await waitForCondition(hasConfigurationReturned); + mParticle + .getInstance() + ._Store.pixelConfigurations.length.should.equal(1); + + fetchMockSuccess(urls.login, { + mpid: 'MPID1', is_logged_in: false + }); + // force the preInit cookie configurations to fire + mParticle.Identity.login({ + userIdentities: { customerid: 'abc' }, + }); + await waitForCondition(() => mParticle.Identity.getCurrentUser()?.getMPID() === 'MPID1'); + const cookies = getLocalStorage(); + Object.keys(cookies['MPID1'].csd).length.should.equal(1); }); const MockUser = function() { @@ -341,7 +297,7 @@ describe('cookie syncing', function() { }; }; - it('should perform a cookiesync when consent is not configured on the cookiesync setting', function(done) { + it('should perform a cookiesync when consent is not configured on the cookiesync setting', async () => { mParticle._resetForTests(MPConfig); pixelSettings.filteringConsentRuleValues = {} as unknown as IConsentRules; @@ -349,18 +305,16 @@ describe('cookie syncing', function() { mParticle.init(apiKey, window.mParticle.config); - setTimeout(function() { - expect( - mParticle.getInstance()._Store.pixelConfigurations.length - ).to.equal(1); - const data = mParticle.getInstance()._Persistence.getLocalStorage(); - data[testMPID].csd.should.have.property('5'); + await waitForCondition(hasIdentifyReturned); - done(); - }, timeout); + expect( + mParticle.getInstance()._Store.pixelConfigurations.length + ).to.equal(1); + const data = mParticle.getInstance()._Persistence.getLocalStorage(); + data[testMPID].csd.should.have.property('5'); }); - it('should return false for isEnabledForUserConsent when consent is configured but no user is passed', function(done) { + it('should return false for isEnabledForUserConsent when consent is configured but no user is passed', async () => { mParticle._resetForTests(MPConfig); const enableCookieSync = true; @@ -381,11 +335,9 @@ describe('cookie syncing', function() { ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, null); expect(enabled).to.not.be.ok; - - done(); }); - it("should disable cookie sync if 'Do Not Forward' when 'Consent Rejected' is selected and user consent is rejected", function(done) { + it("should disable cookie sync if 'Do Not Forward' when 'Consent Rejected' is selected and user consent is rejected", function() { const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = false; const userConsent = false; @@ -417,10 +369,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.not.be.ok; - done(); }); - it("should disable cookie sync if 'Do Not Forward' when 'Consent Accepted' is selected and user consent is given", function(done) { + it("should disable cookie sync if 'Do Not Forward' when 'Consent Accepted' is selected and user consent is given", function() { const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = true; const userConsent = true; @@ -452,11 +403,9 @@ describe('cookie syncing', function() { ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.not.be.ok; - - done(); }); - it("should enable cookie sync if 'Only Forward' when 'Consent Rejected' is selected and user consent is rejected", function(done) { + it("should enable cookie sync if 'Only Forward' when 'Consent Rejected' is selected and user consent is rejected", function() { const includeOnMatch = true; // 'Only Forward' chosen in UI, 'includeOnMatch' in config const consented = false; const userConsent = false; @@ -489,11 +438,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.be.ok; - - done(); }); - it("should enable cookie sync if 'Only Forward' when 'Consent Given'is selected and user consent is given", function(done) { + it("should enable cookie sync if 'Only Forward' when 'Consent Given'is selected and user consent is given", function() { const includeOnMatch = true; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = true; const userConsent = true; @@ -525,11 +472,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.be.ok; - - done(); }); - it("should disable cookie sync if 'Only Forward' on 'Consent Given' is selected and user consent is not given", function(done) { + it("should disable cookie sync if 'Only Forward' on 'Consent Given' is selected and user consent is not given", function() { const includeOnMatch = true; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = true; const userConsented = false; @@ -561,11 +506,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.not.be.ok; - - done(); }); - it("should perform a cookie sync if 'Do Not Forward' when 'Consent Rejected' is selected and user consent is given", function(done) { + it("should perform a cookie sync if 'Do Not Forward' when 'Consent Rejected' is selected and user consent is given", function() { const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = false; const userConsented = true; @@ -597,11 +540,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.be.ok; - - done(); }); - it("should perform a cookie sync if 'Do Not Forward' when 'Consent Given' is selected and user consent is rejected", function(done) { + it("should perform a cookie sync if 'Do Not Forward' when 'Consent Given' is selected and user consent is rejected", function() { const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = true; const userConsented = false; @@ -633,11 +574,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.be.ok; - - done(); }); - it("should perform a cookie sync if 'Do Not Forward' when 'Consent Rejected' is selected and user consent is given", function(done) { + it("should perform a cookie sync if 'Do Not Forward' when 'Consent Rejected' is selected and user consent is given", function() { const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = false; const userConsented = true; @@ -669,11 +608,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.be.ok; - - done(); }); - it("should not perform a cookie sync if 'Do Not Forward' if CCPA is 'Not Present' is selected and user CCPA is not present", function(done) { + it("should not perform a cookie sync if 'Do Not Forward' if CCPA is 'Not Present' is selected and user CCPA is not present", function() { const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = false; const ccpaPresent = false; @@ -704,11 +641,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.not.be.ok; - - done(); }); - it("should not perform a cookie sync if 'Do Not Forward' if CCPA is 'Present' is selected and user CCPA is present", function(done) { + it("should not perform a cookie sync if 'Do Not Forward' if CCPA is 'Present' is selected and user CCPA is present", function() { const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = true; const ccpaPresent = true; @@ -739,11 +674,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.not.be.ok; - - done(); }); - it("should perform a cookie sync if 'Only Forward' if CCPA is 'Not Present' is selected and user CCPA is not present", function(done) { + it("should perform a cookie sync if 'Only Forward' if CCPA is 'Not Present' is selected and user CCPA is not present", function() { const includeOnMatch = true; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = false; const ccpaPresent = false; @@ -774,11 +707,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.be.ok; - - done(); }); - it("should perform a cookie sync if 'Only Forward' when CCPA is 'Present' is selected and user CCPA is present", function(done) { + it("should perform a cookie sync if 'Only Forward' when CCPA is 'Present' is selected and user CCPA is present", function() { const includeOnMatch = true; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = true; const ccpaPresent = true; @@ -809,11 +740,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.be.ok; - - done(); }); - it("should not perform a cookie sync if 'Only Forward' when CCPA is 'Present' is selected and CCPA is not present", function(done) { + it("should not perform a cookie sync if 'Only Forward' when CCPA is 'Present' is selected and CCPA is not present", function() { const includeOnMatch = true; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = true; const ccpaPresent = false; @@ -844,11 +773,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.not.be.ok; - - done(); }); - it("should perform a cookie sync if 'Do Not Forward' if CCPA is 'Present' is selected and user CCPA is not present", function(done) { + it("should perform a cookie sync if 'Do Not Forward' if CCPA is 'Present' is selected and user CCPA is not present", function() { const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = true; const ccpaPresent = false; @@ -879,11 +806,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.be.ok; - - done(); }); - it("should perform a cookie sync if 'Do Not Forward' if CCPA is 'Not Present' is selected and user CCPA is present", function(done) { + it("should perform a cookie sync if 'Do Not Forward' if CCPA is 'Not Present' is selected and user CCPA is present", function() { const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = false; const ccpaPresent = true; @@ -914,11 +839,9 @@ describe('cookie syncing', function() { .getInstance() ._Consent.isEnabledForUserConsent(filteringConsentRuleValues, user); expect(enabled).to.be.ok; - - done(); }); - it('should perform a cookie sync only after GDPR consent is given when consent is required - perform a cookie sync when accepting consent is required', function(done) { + it('should perform a cookie sync only after GDPR consent is given when consent is required - perform a cookie sync when accepting consent is required', async () => { const includeOnMatch = true; // 'Only Forward' chosen in UI, 'includeOnMatch' in config const consented = true; mParticle._resetForTests(MPConfig); @@ -938,58 +861,50 @@ describe('cookie syncing', function() { window.mParticle.config.pixelConfigs = [pixelSettings]; mParticle.init(apiKey, window.mParticle.config); + await waitForCondition(hasIdentifyReturned); - setTimeout(function() { - const localStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - localStorage.testMPID.should.not.have.property('csd'); - - const falseConsentState = mParticle - .getInstance() - .Consent.createConsentState() - .addGDPRConsentState( - 'foo purpose 1', - mParticle.getInstance().Consent.createGDPRConsent(false) - ); - - mParticle.Identity.getCurrentUser().setConsentState( - falseConsentState + const localStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + localStorage.testMPID.should.not.have.property('csd'); + + const falseConsentState = mParticle + .getInstance() + .Consent.createConsentState() + .addGDPRConsentState( + 'foo purpose 1', + mParticle.getInstance().Consent.createGDPRConsent(false) ); - setTimeout(function() { - const noCookieSyncLS = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - noCookieSyncLS.testMPID.should.not.have.property('csd'); - - const trueConsentState = mParticle - .getInstance() - .Consent.createConsentState() - .addGDPRConsentState( - 'foo purpose 1', - mParticle.getInstance().Consent.createGDPRConsent(true) - ); - - mParticle.Identity.getCurrentUser().setConsentState( - trueConsentState - ); - - setTimeout(function() { - const cookieSyncLS = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - cookieSyncLS.testMPID.should.have.property('csd'); - cookieSyncLS.testMPID.csd.should.have.property(5); - - done(); - }, timeout); - }, timeout); - }, timeout); + + mParticle.Identity.getCurrentUser().setConsentState( + falseConsentState + ); + const noCookieSyncLS = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + noCookieSyncLS.testMPID.should.not.have.property('csd'); + + const trueConsentState = mParticle + .getInstance() + .Consent.createConsentState() + .addGDPRConsentState( + 'foo purpose 1', + mParticle.getInstance().Consent.createGDPRConsent(true) + ); + + mParticle.Identity.getCurrentUser().setConsentState( + trueConsentState + ); + + const cookieSyncLS = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + cookieSyncLS.testMPID.should.have.property('csd'); + cookieSyncLS.testMPID.csd.should.have.property(5); }); - it('should perform a cookie sync only after GDPR consent is given when consent is required - perform a cookie sync when consent is rejected', function(done) { - waitForCondition(hasIdentifyReturned) - .then(() => { + it('should perform a cookie sync only after GDPR consent is given when consent is required - perform a cookie sync when consent is rejected', async () => { + await waitForCondition(hasIdentifyReturned); const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = false; @@ -1011,57 +926,48 @@ describe('cookie syncing', function() { mParticle.init(apiKey, window.mParticle.config); - waitForCondition(hasIdentifyReturned) - .then(() => { - setTimeout(function() { - const localStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - localStorage.testMPID.should.not.have.property('csd'); - const falseConsentState = mParticle - .getInstance() - .Consent.createConsentState() - .addGDPRConsentState( - 'foo purpose 1', - mParticle.getInstance().Consent.createGDPRConsent(false) - ); - - mParticle.Identity.getCurrentUser().setConsentState( - falseConsentState + await waitForCondition(hasIdentifyReturned); + const localStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + localStorage.testMPID.should.not.have.property('csd'); + const falseConsentState = mParticle + .getInstance() + .Consent.createConsentState() + .addGDPRConsentState( + 'foo purpose 1', + mParticle.getInstance().Consent.createGDPRConsent(false) + ); + + mParticle.Identity.getCurrentUser().setConsentState( + falseConsentState + ); + + let newLocalStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + newLocalStorage.testMPID.should.not.have.property('csd'); + + const trueConsentState = mParticle + .getInstance() + .Consent.createConsentState() + .addGDPRConsentState( + 'foo purpose 1', + mParticle.getInstance().Consent.createGDPRConsent(true) ); - setTimeout(function() { - let newLocalStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - newLocalStorage.testMPID.should.not.have.property('csd'); - - const trueConsentState = mParticle - .getInstance() - .Consent.createConsentState() - .addGDPRConsentState( - 'foo purpose 1', - mParticle.getInstance().Consent.createGDPRConsent(true) - ); - - mParticle.Identity.getCurrentUser().setConsentState( - trueConsentState - ); - setTimeout(function() { - newLocalStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - newLocalStorage.testMPID.should.have.property('csd'); - newLocalStorage.testMPID.csd.should.have.property(5); - done(); - }, timeout); - }, timeout); - }, timeout); - }) - }) + mParticle.Identity.getCurrentUser().setConsentState( + trueConsentState + ); + + newLocalStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + newLocalStorage.testMPID.should.have.property('csd'); + newLocalStorage.testMPID.csd.should.have.property(5); }); - it('should perform a cookie sync only after CCPA consent is given when consent is required - perform a cookie sync when accepting consent is required', function(done) { + it('should perform a cookie sync only after CCPA consent is given when consent is required - perform a cookie sync when accepting consent is required', async () => { const includeOnMatch = true; // 'Only Forward' chosen in UI, 'includeOnMatch' in config const consented = true; mParticle._resetForTests(MPConfig); @@ -1082,57 +988,50 @@ describe('cookie syncing', function() { mParticle.init(apiKey, window.mParticle.config); - setTimeout(function() { - const localStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - localStorage.testMPID.should.not.have.property('csd'); - const falseConsentState = mParticle - .getInstance() - .Consent.createConsentState() - .setCCPAConsentState( - // false to show that it doesn't perform a cookie sync - mParticle.getInstance().Consent.createCCPAConsent(false) - ); - - mParticle.Identity.getCurrentUser().setConsentState( - falseConsentState + await waitForCondition(hasIdentifyReturned); + + const localStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + localStorage.testMPID.should.not.have.property('csd'); + const falseConsentState = mParticle + .getInstance() + .Consent.createConsentState() + .setCCPAConsentState( + // false to show that it doesn't perform a cookie sync + mParticle.getInstance().Consent.createCCPAConsent(false) + ); + + mParticle.Identity.getCurrentUser().setConsentState( + falseConsentState + ); + + const noCookieSyncLS = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + noCookieSyncLS.testMPID.should.not.have.property('csd'); + + const trueConsentState = mParticle + .getInstance() + .Consent.createConsentState() + .setCCPAConsentState( + // false to show that it doesn't perform a cookie sync + mParticle.getInstance().Consent.createCCPAConsent(true) ); - setTimeout(function() { - const noCookieSyncLS = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - noCookieSyncLS.testMPID.should.not.have.property('csd'); - - const trueConsentState = mParticle - .getInstance() - .Consent.createConsentState() - .setCCPAConsentState( - // false to show that it doesn't perform a cookie sync - mParticle.getInstance().Consent.createCCPAConsent(true) - ); - - mParticle.Identity.getCurrentUser().setConsentState( - trueConsentState - ); - - setTimeout(function() { - const cookieSyncLS = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - cookieSyncLS.testMPID.should.have.property('csd'); - cookieSyncLS.testMPID.csd.should.have.property(5); - - done(); - }, timeout); - }, timeout); - }, timeout); + mParticle.Identity.getCurrentUser().setConsentState( + trueConsentState + ); + + const cookieSyncLS = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + cookieSyncLS.testMPID.should.have.property('csd'); + cookieSyncLS.testMPID.csd.should.have.property(5); }); - it('should perform a cookie sync only after CCPA consent is given when consent is required - perform a cookie sync when consent is rejected', function(done) { - waitForCondition(hasIdentifyReturned) - .then(() => { + it('should perform a cookie sync only after CCPA consent is given when consent is required - perform a cookie sync when consent is rejected', async () => { + await waitForCondition(hasIdentifyReturned); const includeOnMatch = false; // 'Do Not Forward' chosen in UI, 'includeOnMatch' in config const consented = false; @@ -1154,57 +1053,46 @@ describe('cookie syncing', function() { mParticle.init(apiKey, window.mParticle.config); - waitForCondition(hasIdentifyReturned) - .then(() => { - setTimeout(function() { - const localStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - localStorage.testMPID.should.not.have.property('csd'); - const falseConsentState = mParticle - .getInstance() - .Consent.createConsentState() - .setCCPAConsentState( - mParticle.getInstance().Consent.createCCPAConsent(false) - ); - - mParticle.Identity.getCurrentUser().setConsentState( - falseConsentState + await waitForCondition(hasIdentifyReturned); + const localStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + localStorage.testMPID.should.not.have.property('csd'); + const falseConsentState = mParticle + .getInstance() + .Consent.createConsentState() + .setCCPAConsentState( + mParticle.getInstance().Consent.createCCPAConsent(false) ); - setTimeout(function() { - let newLocalStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - newLocalStorage.testMPID.should.not.have.property('csd'); - - const trueConsentState = mParticle - .getInstance() - .Consent.createConsentState() - .setCCPAConsentState( - mParticle.getInstance().Consent.createCCPAConsent(true) - ); - - mParticle.Identity.getCurrentUser().setConsentState( - trueConsentState - ); - - setTimeout(function() { - newLocalStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - newLocalStorage.testMPID.should.have.property('csd'); - newLocalStorage.testMPID.csd.should.have.property(5); - - done(); - }, timeout); - }, timeout); - }, timeout); - }) - }) + mParticle.Identity.getCurrentUser().setConsentState( + falseConsentState + ); + + let newLocalStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + newLocalStorage.testMPID.should.not.have.property('csd'); + + const trueConsentState = mParticle + .getInstance() + .Consent.createConsentState() + .setCCPAConsentState( + mParticle.getInstance().Consent.createCCPAConsent(true) + ); + + mParticle.Identity.getCurrentUser().setConsentState( + trueConsentState + ); + + newLocalStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + newLocalStorage.testMPID.should.have.property('csd'); + newLocalStorage.testMPID.csd.should.have.property(5); }); - it('should allow some cookie syncs to occur and others to not occur if there are multiple pixels with varying consent levels', function(done) { + it('should allow some cookie syncs to occur and others to not occur if there are multiple pixels with varying consent levels', async () => { // This test has 2 pixelSettings. pixelSettings1 requires consent pixelSettings2 does not. When mparticle initializes, the pixelSettings2 should fire and pixelSettings1 shouldn't. // After the appropriate consent is saved to the huser, pixelSettings1 will fire. @@ -1255,37 +1143,33 @@ describe('cookie syncing', function() { mParticle.init(apiKey, window.mParticle.config); - setTimeout(function() { - const localStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - localStorage.testMPID.should.have.property('csd'); - - // Performs a cookie sync for 2 but not 1 - localStorage.testMPID.csd.should.not.have.property(1); - localStorage.testMPID.csd.should.have.property(2); - - const trueConsentState = mParticle - .getInstance() - .Consent.createConsentState() - .setCCPAConsentState( - mParticle.getInstance().Consent.createCCPAConsent(true) - ); - - mParticle.Identity.getCurrentUser().setConsentState( - trueConsentState + await waitForCondition(hasIdentifyReturned); + + const localStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + localStorage.testMPID.should.have.property('csd'); + + // Performs a cookie sync for 2 but not 1 + localStorage.testMPID.csd.should.not.have.property(1); + localStorage.testMPID.csd.should.have.property(2); + + const trueConsentState = mParticle + .getInstance() + .Consent.createConsentState() + .setCCPAConsentState( + mParticle.getInstance().Consent.createCCPAConsent(true) ); - setTimeout(function() { - const newLocalStorage = mParticle - .getInstance() - ._Persistence.getLocalStorage(); - newLocalStorage.testMPID.should.have.property('csd'); - // Now has both cookie syncs because the appropriate consent was added - newLocalStorage.testMPID.csd.should.have.property(2); - newLocalStorage.testMPID.csd.should.have.property(1); - - done(); - }, timeout); - }, timeout); + + mParticle.Identity.getCurrentUser().setConsentState( + trueConsentState + ); + const newLocalStorage = mParticle + .getInstance() + ._Persistence.getLocalStorage(); + newLocalStorage.testMPID.should.have.property('csd'); + // Now has both cookie syncs because the appropriate consent was added + newLocalStorage.testMPID.csd.should.have.property(2); + newLocalStorage.testMPID.csd.should.have.property(1); }); }); \ No newline at end of file diff --git a/test/src/tests-core-sdk.js b/test/src/tests-core-sdk.js index bfd551ad1..b2a8e0531 100644 --- a/test/src/tests-core-sdk.js +++ b/test/src/tests-core-sdk.js @@ -17,7 +17,7 @@ const { fetchMockSuccess, hasIdentifyReturned, hasIdentityCallInflightReturned, - hasConfigLoaded, + hasConfigurationReturned } = Utils; describe('core SDK', function() { @@ -128,7 +128,7 @@ describe('core SDK', function() { }); }); - it('should process ready queue when initialized', async function() { + it('should process ready queue when initialized', async () => { let readyFuncCalled = false; mParticle._resetForTests(MPConfig); @@ -156,7 +156,7 @@ describe('core SDK', function() { }) }); - it('should get app version', async function() { + it('should get app version', async () => { await waitForCondition(hasIdentityCallInflightReturned); mParticle.setAppVersion('2.0'); @@ -1100,7 +1100,7 @@ describe('core SDK', function() { }); // TODO - there are no actual tests here....what's going on? - it('should fetch from /config and keep everything properly on the store', function(done) { + it('should fetch from /config and keep everything properly on the store', async () => { mParticle._resetForTests(MPConfig); const config = { appName: 'appNameTest', @@ -1116,23 +1116,15 @@ describe('core SDK', function() { window.mParticle.config.requestConfig = true; mParticle.init(apiKey, window.mParticle.config); - waitForCondition(() => { - return ( - mParticle.getInstance()._Store.configurationLoaded === true - ); - }) - .then(() => { + await waitForCondition(hasConfigurationReturned); mParticle.getInstance()._Store.SDKConfig.appName = config.appName; mParticle.getInstance()._Store.SDKConfig.minWebviewBridgeVersion = config.minWebviewBridgeVersion; mParticle.getInstance()._Store.SDKConfig.workspaceToken = config.workspaceToken; localStorage.removeItem(config.workspaceToken); - - done(); - }) }); - it('should initialize and log events even with a failed /config fetch and empty config', async () => { + it('should initialize and log events even with a failed /config fetch and empty config', async () => { // this instance occurs when self hosting and the user only passes an object into init mParticle._resetForTests(MPConfig); @@ -1158,7 +1150,7 @@ describe('core SDK', function() { mParticle.init(apiKey, window.mParticle.config); - await waitForCondition(hasConfigLoaded); + await waitForCondition(hasConfigurationReturned); // fetching the config is async and we need to wait for it to finish mParticle.getInstance()._Store.isInitialized.should.equal(true); @@ -1183,7 +1175,7 @@ describe('core SDK', function() { testEvent.should.be.ok(); }); - it('should initialize without a config object passed to init', async function() { + it('should initialize without a config object passed to init', async () => { // this instance occurs when self hosting and the user only passes an object into init mParticle._resetForTests(MPConfig); @@ -1253,7 +1245,7 @@ describe('core SDK', function() { done(); }); - it('should set a device id when calling setDeviceId', async function() { + it('should set a device id when calling setDeviceId', async () => { mParticle._resetForTests(MPConfig); mParticle.init(apiKey, window.mParticle.config); @@ -1300,7 +1292,7 @@ describe('core SDK', function() { done(); }); - it('should set the wrapper sdk info in Store when mParticle._setWrapperSDKInfo() method is called after init is called', async function() { + it('should set the wrapper sdk info in Store when mParticle._setWrapperSDKInfo() method is called after init is called', async () => { mParticle._resetForTests(MPConfig); mParticle._setWrapperSDKInfo('flutter', '1.0.3'); @@ -1313,7 +1305,7 @@ describe('core SDK', function() { mParticle.getInstance()._Store.wrapperSDKInfo.isInfoSet.should.equal(true); }); - it('should not set the wrapper sdk info in Store after it has previously been set', async function() { + it('should not set the wrapper sdk info in Store after it has previously been set', async () => { mParticle._resetForTests(MPConfig); mParticle._setWrapperSDKInfo('flutter', '1.0.3'); diff --git a/test/src/tests-forwarders.ts b/test/src/tests-forwarders.ts index 175ba0454..11156d7cf 100644 --- a/test/src/tests-forwarders.ts +++ b/test/src/tests-forwarders.ts @@ -1162,7 +1162,7 @@ describe('forwarders', function() { done(); }); - it('should invoke forwarder opt out', async function() { + it('should invoke forwarder opt out', async () => { mParticle._resetForTests(MPConfig); const mockForwarder = new MockForwarder(); @@ -2442,7 +2442,7 @@ describe('forwarders', function() { expect(adobeIntegrationAttributes).to.eqls({}); }); - it('should set integration attributes on forwarders', async function() { + it('should set integration attributes on forwarders', async () => { mParticle.init(apiKey, window.mParticle.config) await waitForCondition(hasIdentityCallInflightReturned); @@ -2454,7 +2454,7 @@ describe('forwarders', function() { adobeIntegrationAttributes.MCID.should.equal('abcdefg'); }); - it('should clear integration attributes when an empty object or a null is passed', async function() { + it('should clear integration attributes when an empty object or a null is passed', async () => { mParticle.init(apiKey, window.mParticle.config) await waitForCondition(hasIdentityCallInflightReturned); @@ -2776,7 +2776,7 @@ describe('forwarders', function() { // This will pass when we add mpInstance._Store.isInitialized = true; to mp-instance before `processIdentityCallback` - it('configures forwarders before events are logged via identify callback', async function() { + it('configures forwarders before events are logged via identify callback', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.identifyRequest = { userIdentities: { diff --git a/test/src/tests-helpers.js b/test/src/tests-helpers.js index 632ffa2b0..968cc8c12 100644 --- a/test/src/tests-helpers.js +++ b/test/src/tests-helpers.js @@ -56,7 +56,7 @@ describe('helpers', function() { done(); }); - it('should return event name in warning when sanitizing invalid attributes', async function() { + it('should return event name in warning when sanitizing invalid attributes', async () => { await waitForCondition(hasIdentityCallInflightReturned); const bond = sinon.spy(mParticle.getInstance().Logger, 'warning'); mParticle.logEvent('eventName', mParticle.EventType.Location, {invalidValue: {}}); @@ -93,7 +93,7 @@ describe('helpers', function() { done(); }); - it('should return commerce event name in warning when sanitizing invalid attributes', async function() { + it('should return commerce event name in warning when sanitizing invalid attributes', async () => { await waitForCondition(hasIdentityCallInflightReturned); const bond = sinon.spy(mParticle.getInstance().Logger, 'warning'); diff --git a/test/src/tests-kit-blocking.ts b/test/src/tests-kit-blocking.ts index 0485089c9..147afc008 100644 --- a/test/src/tests-kit-blocking.ts +++ b/test/src/tests-kit-blocking.ts @@ -1406,7 +1406,7 @@ describe('kit blocking', () => { fetchMock.restore(); }); - it('should create a proper kitblocker on a self hosted set up', function(done) { + it('should create a proper kitblocker on a self hosted set up', async () => { fetchMock.get(`${urls.config}&plan_id=robs_plan&plan_version=1`, { status: 200, body: JSON.stringify({ dataPlanResult: dataPlan }), @@ -1427,21 +1427,17 @@ describe('kit blocking', () => { window.mParticle.config.kitConfigs.push(forwarderDefaultConfiguration('MockForwarder')); window.mParticle.init(apiKey, window.mParticle.config); - window.mParticle.logEvent('Blocked event'); + await waitForCondition(hasIdentifyReturned); - setTimeout(() => { - let event = window.MockForwarder1.instance.receivedEvent; - (event === null).should.equal(true); - - window.mParticle.config.requestConfig = false; - - done(); - - }, 50); + window.mParticle.logEvent('Blocked event'); + let event = window.MockForwarder1.instance.receivedEvent; + (event === null).should.equal(true); + + window.mParticle.config.requestConfig = false; }); - it('should log an error if the data plan has an error on it', function(done) { + it('should log an error if the data plan has an error on it', async () => { const errorMessage = 'This is an error'; fetchMock.get(`${urls.config}&plan_id=robs_plan&plan_version=1`, { @@ -1473,12 +1469,10 @@ describe('kit blocking', () => { } window.mParticle.init(apiKey, window.mParticle.config); - setTimeout(() => { - errorMessages[0].should.equal(errorMessage); - window.mParticle.config.requestConfig = false; - - done(); - }, 50); + await waitForCondition(hasIdentifyReturned) + + errorMessages[0].should.equal(errorMessage); + window.mParticle.config.requestConfig = false; }); }) }); diff --git a/test/src/tests-mparticle-instance-manager.ts b/test/src/tests-mparticle-instance-manager.ts index e56dd2e3c..d9a3b7935 100644 --- a/test/src/tests-mparticle-instance-manager.ts +++ b/test/src/tests-mparticle-instance-manager.ts @@ -4,10 +4,11 @@ import { expect } from 'chai'; import { urls, MPConfig } from './config/constants'; import Utils from './config/utils'; import { IMParticleInstanceManager } from '../../src/sdkRuntimeModels'; -const { +const { findEventFromRequest, waitForCondition, fetchMockSuccess, + hasConfigurationReturned } = Utils; declare global { @@ -303,29 +304,18 @@ describe('mParticle instance manager', () => { expect(mParticle.getInstance('instance3')._instanceName).to.equal('instance3'); }); - it('creates multiple instances with their own cookies', done => { - // setTimeout to allow config to come back from the beforeEach initialization - setTimeout(() => { - const cookies1 = window.localStorage.getItem( - 'mprtcl-v4_wtTest1' - ); - const cookies2 = window.localStorage.getItem( - 'mprtcl-v4_wtTest2' - ); - const cookies3 = window.localStorage.getItem( - 'mprtcl-v4_wtTest3' - ); + it('creates multiple instances with their own cookies', async () => { + await waitForCondition(hasConfigurationReturned); + const cookies1 = window.localStorage.getItem('mprtcl-v4_wtTest1'); + const cookies2 = window.localStorage.getItem('mprtcl-v4_wtTest2'); + const cookies3 = window.localStorage.getItem('mprtcl-v4_wtTest3'); - cookies1.includes('apiKey1').should.equal(true); - cookies2.includes('apiKey2').should.equal(true); - cookies3.includes('apiKey3').should.equal(true); - - done(); - }, 50); + cookies1.includes('apiKey1').should.equal(true); + cookies2.includes('apiKey2').should.equal(true); + cookies3.includes('apiKey3').should.equal(true); }); it('logs events to their own instances', async () => { - // setTimeout to allow config to come back from the beforeEach initialization await waitForCondition(() => { return ( mParticle.getInstance('default_instance')._Store @@ -335,31 +325,31 @@ describe('mParticle instance manager', () => { mParticle.getInstance('instance3')._Store .configurationLoaded === true ); - }); + }) mParticle.getInstance('default_instance').logEvent('hi1'); mParticle.getInstance('instance2').logEvent('hi2'); mParticle.getInstance('instance3').logEvent('hi3'); - + const instance1Event = returnEventForMPInstance( fetchMock.calls(), 'apiKey1', 'hi1' ); - instance1Event.should.be.ok(); + expect(instance1Event).to.be.ok; const instance2Event = returnEventForMPInstance( fetchMock.calls(), 'apiKey2', 'hi2' ); - instance2Event.should.be.ok(); + expect(instance2Event).to.be.ok; const instance3Event = returnEventForMPInstance( fetchMock.calls(), 'apiKey3', 'hi3' ); - instance3Event.should.be.ok(); + expect(instance3Event).to.be.ok; const instance1EventsFail1 = returnEventForMPInstance( fetchMock.calls(), @@ -367,45 +357,45 @@ describe('mParticle instance manager', () => { 'hi2' ); - expect(instance1EventsFail1).not.be.ok; + expect(instance1EventsFail1).to.not.be.ok; const instance1EventsFail2 = returnEventForMPInstance( fetchMock.calls(), 'apiKey1', 'hi3' ); - expect(instance1EventsFail2).not.be.ok; + expect(instance1EventsFail2).to.not.be.ok; const instance2EventsFail1 = returnEventForMPInstance( fetchMock.calls(), 'apiKey2', 'hi1' ); - expect(instance2EventsFail1).not.be.ok; + expect(instance2EventsFail1).to.not.be.ok; const instance2EventsFail2 = returnEventForMPInstance( fetchMock.calls(), 'apiKey2', 'hi3' ); - expect(instance2EventsFail2).not.be.ok; + expect(instance2EventsFail2).to.not.be.ok; const instance3EventsFail1 = returnEventForMPInstance( fetchMock.calls(), 'apiKey3', 'hi1' ); - expect(instance3EventsFail1).not.be.ok; + expect(instance3EventsFail1).to.not.be.ok; const instance3EventsFail2 = returnEventForMPInstance( fetchMock.calls(), 'apiKey3', 'hi2' ); - expect(instance3EventsFail2).not.be.ok; + expect(instance3EventsFail2).to.not.be.ok; }); - it('logs purchase events to their own instances', done => { + it('logs purchase events to their own instances', async () => { const prodattr1 = { journeyType: 'testjourneytype1', eventMetric1: 'metric2', @@ -449,74 +439,59 @@ describe('mParticle instance manager', () => { 5 ); - // setTimeout to allow config to come back from the beforeEach initialization + await waitForCondition(hasConfigurationReturned); mParticle .getInstance() .eCommerce.logPurchase(ta, [product1, product2]); - setTimeout(() => { - const instance1Event = returnEventForMPInstance( - fetchMock.calls(), - 'apiKey1', - 'purchase' - ); - let instance2Event = returnEventForMPInstance( - fetchMock.calls(), - 'apiKey2', - 'purchase' - ); - let instance3Event = returnEventForMPInstance( - fetchMock.calls(), - 'apiKey3', - 'purchase' - ); - expect(instance1Event).be.ok; - expect(instance2Event).not.be.ok; - expect(instance3Event).not.be.ok; - - mParticle - .getInstance('instance2') - .eCommerce.logPurchase( - ta, - [product1, product2], - false, - {}, - {} - ); - - instance2Event = returnEventForMPInstance( - fetchMock.calls(), - 'apiKey2', - 'purchase' - ); - instance3Event = returnEventForMPInstance( - fetchMock.calls(), - 'apiKey3', - 'purchase' - ); - instance2Event.should.be.ok(); - expect(instance3Event).not.be.ok; - - mParticle - .getInstance('instance3') - .eCommerce.logPurchase( - ta, - [product1, product2], - false, - {}, - {} - ); - - instance3Event = returnEventForMPInstance( - fetchMock.calls(), - 'apiKey3', - 'purchase' - ); + const instance1Event = returnEventForMPInstance( + fetchMock.calls(), + 'apiKey1', + 'purchase' + ); + let instance2Event = returnEventForMPInstance( + fetchMock.calls(), + 'apiKey2', + 'purchase' + ); + let instance3Event = returnEventForMPInstance( + fetchMock.calls(), + 'apiKey3', + 'purchase' + ); + instance1Event.should.be.ok(); + expect(instance2Event).to.not.be.ok; + expect(instance3Event).to.not.be.ok; + + mParticle + .getInstance('instance2') + .eCommerce.logPurchase(ta, [product1, product2]); + + instance2Event = returnEventForMPInstance( + fetchMock.calls(), + 'apiKey2', + 'purchase' + ); + instance3Event = returnEventForMPInstance( + fetchMock.calls(), + 'apiKey3', + 'purchase' + ); + + expect(instance2Event).to.be.ok; + expect(instance3Event).to.not.be.ok; + + mParticle + .getInstance('instance3') + .eCommerce.logPurchase(ta, [product1, product2]); - instance3Event.should.be.ok(); + instance3Event = returnEventForMPInstance( + fetchMock.calls(), + 'apiKey3', + 'purchase' + ); - done(); - }, 50); + expect(instance3Event).to.be.ok; }); }); }); diff --git a/test/src/tests-self-hosting-specific.js b/test/src/tests-self-hosting-specific.js index c9df252b8..8c7675a6f 100644 --- a/test/src/tests-self-hosting-specific.js +++ b/test/src/tests-self-hosting-specific.js @@ -3,8 +3,13 @@ import sinon from 'sinon'; import fetchMock from 'fetch-mock/esm/client'; import { urls, apiKey, MPConfig } from './config/constants'; -const { findEventFromRequest, findBatch, waitForCondition, fetchMockSuccess } = Utils; - +const { + findEventFromRequest, + findBatch, + waitForCondition, + fetchMockSuccess, + hasConfigurationReturned +} = Utils; // Calls to /config are specific to only the self hosting environment describe('/config self-hosting integration tests', function() { @@ -18,7 +23,7 @@ describe('/config self-hosting integration tests', function() { window.mParticle.config.requestConfig = false; }) - it('queues events in the eventQueue while /config is in flight, then processes them afterwards with correct MPID', function(done) { + it('queues events in the eventQueue while /config is in flight, then processes them afterwards with correct MPID', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.requestConfig = true; window.mParticle.config.flags.eventBatchingIntervalMillis = 0; // trigger event uploads immediately @@ -57,22 +62,13 @@ describe('/config self-hosting integration tests', function() { let event = findEventFromRequest(fetchMock.calls(), 'Test'); Should(event).not.be.ok(); - waitForCondition(() => { - return ( - mParticle.getInstance()._Store.configurationLoaded === true - ); - }) - .then(() => { - setTimeout(() => { - event = findBatch(fetchMock.calls(), 'Test'); - - event.should.be.ok(); - event.mpid.should.equal('identifyMPID'); - - window.mParticle.config.requestConfig = false; - done(); - }, 75); - }) + await waitForCondition(hasConfigurationReturned); + event = findBatch(fetchMock.calls(), 'Test'); + + event.should.be.ok(); + event.mpid.should.equal('identifyMPID'); + + window.mParticle.config.requestConfig = false; }); // https://go.mparticle.com/work/SQDSDKS-6852 @@ -175,13 +171,12 @@ describe('/config self-hosting integration tests', function() { }); }); - it('cookie name has workspace token in it in self hosting mode after config fetch', function(done) { + it('cookie name has workspace token in it in self hosting mode after config fetch', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.requestConfig = true; window.mParticle.config.logLevel = 'verbose'; delete window.mParticle.config.workspaceToken; - fetchMock.get(urls.config, { status: 200, body: JSON.stringify({ @@ -191,17 +186,15 @@ describe('/config self-hosting integration tests', function() { mParticle.init(apiKey, window.mParticle.config); - setTimeout(() => { - const data = window.localStorage.getItem('mprtcl-v4_wtTest'); - (typeof data === 'string').should.equal(true); - window.mParticle.config.requestConfig = false; - - done(); - }, 75); + await waitForCondition(hasConfigurationReturned); + + const data = window.localStorage.getItem('mprtcl-v4_wtTest'); + (typeof data === 'string').should.equal(true); + window.mParticle.config.requestConfig = false; }); describe('/config self-hosting with direct url routing', function () { - it('should return direct urls when no baseUrls are passed and directURLRouting is true', (done) => { + it('should return direct urls when no baseUrls are passed and directURLRouting is true', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.requestConfig = true; delete window.mParticle.config.workspaceToken; @@ -223,12 +216,7 @@ describe('/config self-hosting integration tests', function() { mParticle.init(apiKey, window.mParticle.config); - waitForCondition(() => { - return ( - mParticle.getInstance()._Store.configurationLoaded === true - ); - }) - .then(() => { + await waitForCondition(hasConfigurationReturned); const { aliasUrl, configUrl, @@ -240,18 +228,11 @@ describe('/config self-hosting integration tests', function() { aliasUrl.should.equal('jssdks.us1.mparticle.com/v1/identity/'); configUrl.should.equal('jssdkcdns.mparticle.com/JS/v2/'); identityUrl.should.equal('identity.us1.mparticle.com/v1/'); - v1SecureServiceUrl.should.equal( - 'jssdks.us1.mparticle.com/v1/JS/' - ); - v3SecureServiceUrl.should.equal( - 'jssdks.us1.mparticle.com/v3/JS/' - ); - - done(); - }) + v1SecureServiceUrl.should.equal('jssdks.us1.mparticle.com/v1/JS/'); + v3SecureServiceUrl.should.equal('jssdks.us1.mparticle.com/v3/JS/'); }); - it('should prioritize passed in baseUrls over direct urls', (done) => { + it('should prioritize passed in baseUrls over direct urls', async () => { mParticle._resetForTests(MPConfig); window.mParticle.config.requestConfig = true; window.mParticle.config.aliasUrl = @@ -279,12 +260,7 @@ describe('/config self-hosting integration tests', function() { mParticle.init(apiKey, window.mParticle.config); - waitForCondition(() => { - return ( - mParticle.getInstance()._Store.configurationLoaded === true - ); - }) - .then(() => { + waitForCondition(hasConfigurationReturned); const { aliasUrl, configUrl, @@ -296,15 +272,8 @@ describe('/config self-hosting integration tests', function() { configUrl.should.equal('jssdkcdns.mparticle.com/JS/v2/'); aliasUrl.should.equal('jssdks.foo.mparticle.com/v1/identity/'); identityUrl.should.equal('identity.foo.mparticle.com/v1/'); - v1SecureServiceUrl.should.equal( - 'jssdks.foo.mparticle.com/v1/JS/' - ); - v3SecureServiceUrl.should.equal( - 'jssdks.foo.mparticle.com/v3/JS/' - ); - - done(); - }) + v1SecureServiceUrl.should.equal('jssdks.foo.mparticle.com/v1/JS/'); + v3SecureServiceUrl.should.equal('jssdks.foo.mparticle.com/v3/JS/'); }); }); }); \ No newline at end of file