Skip to content

Commit

Permalink
tests: Update tests to support latest fetch-mock version
Browse files Browse the repository at this point in the history
  • Loading branch information
rmi22186 committed Oct 20, 2023
1 parent 8e9fcdd commit 9e32dce
Show file tree
Hide file tree
Showing 17 changed files with 442 additions and 429 deletions.
1 change: 0 additions & 1 deletion test/src/tests-apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { expect } from 'chai';
declare global {
interface Window {
mParticle: MParticleWebSDK;
fetchMock: any;
}
}

Expand Down
137 changes: 70 additions & 67 deletions test/src/tests-batchUploader.ts

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions test/src/tests-beaconUpload.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -106,15 +107,15 @@ describe('Beacon Upload', () => {
clock = sinon.useFakeTimers({
now: new Date().getTime(),
});
window.fetchMock.restore();
fetchMock.restore();

window.sessionStorage.clear();
window.localStorage.clear();
});

afterEach(() => {
sinon.restore();
window.fetchMock.restore();
fetchMock.restore();
clock.restore();
});

Expand Down
9 changes: 5 additions & 4 deletions test/src/tests-consent.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand All @@ -42,7 +43,7 @@ describe('Consent', function() {

afterEach(function() {
mockServer.restore();
window.fetchMock.restore();
fetchMock.restore();
mParticle._resetForTests(MPConfig);
});

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion test/src/tests-cookie-syncing.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
});
Expand Down
73 changes: 37 additions & 36 deletions test/src/tests-core-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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();
Expand All @@ -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();

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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(
Expand All @@ -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'
);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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 +
Expand Down Expand Up @@ -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();

Expand Down
Loading

0 comments on commit 9e32dce

Please sign in to comment.