Skip to content

Commit

Permalink
tests: Fix remaining tests from XHR to fetch migration (#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmi22186 authored and alexs-mparticle committed Oct 11, 2024
1 parent 9526e3a commit 46f5938
Show file tree
Hide file tree
Showing 34 changed files with 4,054 additions and 1,971 deletions.
2 changes: 1 addition & 1 deletion src/aliasRequestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function sendAliasRequest (mpInstance: MParticleWebSDK, aliasReques
// https://go.mparticle.com/work/SQDSDKS-6568
// XHRUploader returns the response as a string that we need to parse
const xhrResponse = response as unknown as XMLHttpRequest;
// debugger;

aliasResponseBody = xhrResponse.responseText
? JSON.parse(xhrResponse.responseText)
: '';
Expand Down
4 changes: 2 additions & 2 deletions src/mp-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -1539,9 +1539,9 @@ function processReadyQueue(readyQueue) {
processPreloadedItem(readyQueueItem);
}
});

return [];
}
// https://go.mparticle.com/work/SQDSDKS-6835
return [];
}

function queueIfNotInitialized(func, self) {
Expand Down
49 changes: 39 additions & 10 deletions test/integrations/requirejs/test-requirejs.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
import fetchMock from 'fetch-mock/esm/client';
import sinon from 'sinon';

// https://go.mparticle.com/work/SQDSDKS-6849
const waitForCondition = function async(
conditionFn,
timeout = 200,
interval = 10
) {
return new Promise((resolve, reject) => {
const startTime = Date.now();

(function poll() {
if (conditionFn()) {
return resolve(undefined);
} else if (Date.now() - startTime > timeout) {
return reject(new Error('Timeout waiting for condition'));
} else {
setTimeout(poll, interval);
}
})();
});
},
fetchMockSuccess = function (url, body) {
fetchMock.post(
url,
{
status: 200,
body: JSON.stringify(body),
},
{ overwriteRoutes: true }
);
},
hasIdentifyReturned = () => {
return window.mParticle.Identity.getCurrentUser()?.getMPID() === 'testMPID';
};

describe('Require.JS Pages', function() {
it('loads mParticle properly', function() {
var mockServer = sinon.createFakeServer();
mockServer.respondImmediately = true;

mockServer.respondWith('https://identity.mparticle.com/v1/identify', [
200,
{},
JSON.stringify({ mpid: 'testMPID', is_logged_in: false }),
]);
fetchMockSuccess('https://identity.mparticle.com/v1/identify', {
mpid: 'testMPID', is_logged_in: false
});

fetchMock.post('https://jssdks.mparticle.com/v3/JS/test_key/events', 200);

Expand All @@ -26,7 +54,7 @@ describe('Require.JS Pages', function() {
};

mParticle.init('test_key', window.mParticle.config);

waitForCondition(hasIdentifyReturned).then(() => {
fetchMock.resetHistory();

mParticle.logEvent('Test Event1');
Expand All @@ -35,5 +63,6 @@ describe('Require.JS Pages', function() {
const testEventName = JSON.parse(testEvent[1].body).events[0].data.event_name;

testEventName.should.equal('Test Event1');
})
});
});
14 changes: 9 additions & 5 deletions test/src/_test.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@
import './config/setup';

// Import each test module
import './tests-core-sdk';
import './tests-identity';
import './tests-batchUploader';
import './tests-core-sdk';
import './tests-beaconUpload';
import './tests-kit-blocking';
import './tests-event-logging';
import './tests-eCommerce';
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-batchUploader_2';
import './tests-mockBatchCreator';
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-batchUploader_3';
import './tests-validators';
import './tests-utils';
import './tests-session-manager';
Expand All @@ -35,4 +37,6 @@ import './tests-feature-flags';
import './tests-user';
import './tests-legacy-alias-requests';
import './tests-aliasRequestApiClient';
import './tests-integration-capture';
import './tests-integration-capture';
import './tests-batchUploader_4';

32 changes: 28 additions & 4 deletions test/src/config/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
workspaceCookieName,
das,
} from './constants';
import fetchMock from 'fetch-mock/esm/client';

var pluses = /\+/g,
getLocalStorageProducts = function getLocalStorageProducts() {
Expand Down Expand Up @@ -273,24 +274,32 @@ var pluses = /\+/g,
fullPath = 'https://identity.mparticle.com/v1/' + path;
if (path !== 'modify') {
requests.forEach(function(item) {
if (!item.url && item[0] === fullPath) {
returnedRequests.push(item);
}
if (item.url === fullPath) {
returnedRequests.push(item);
}
});
} else {
requests.forEach(function(item) {
if (item.url.slice(-6) === 'modify') {
let url;
if (!item.url) {
url = item[0];
} else {
url = item.url;
}
if (url.slice(-6) === 'modify') {
returnedRequests.push(item);
}
});
}

return returnedRequests;
},
getIdentityEvent = function(mockRequests, endpoint) {
var returnedReqs = getIdentityRequests(mockRequests, endpoint);
if (returnedReqs[0] && returnedReqs[0].requestBody) {
return JSON.parse(returnedReqs[0].requestBody);
if (returnedReqs[0] && returnedReqs[0][1].body) {
return JSON.parse(returnedReqs[0][1].body);
}
return null;
},
Expand Down Expand Up @@ -608,6 +617,19 @@ var pluses = /\+/g,
}
})();
});
},
fetchMockSuccess = function (url, body) {
fetchMock.post(
url,
{
status: 200,
body: JSON.stringify(body),
},
{ overwriteRoutes: true }
);
},
hasIdentifyReturned = () => {
return window.mParticle.Identity.getCurrentUser()?.getMPID() === testMPID;
};

var TestsCore = {
Expand All @@ -634,6 +656,8 @@ var TestsCore = {
forwarderDefaultConfiguration: forwarderDefaultConfiguration,
deleteAllCookies: deleteAllCookies,
waitForCondition: waitForCondition,
fetchMockSuccess: fetchMockSuccess,
hasIdentifyReturned: hasIdentifyReturned,
};

export default TestsCore;
3 changes: 1 addition & 2 deletions test/src/tests-apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Types from '../../src/types';
import Constants from '../../src/constants';
import { apiKey, MPConfig } from './config/constants';
import { MParticleWebSDK } from '../../src/sdkRuntimeModels';
import { expect } from 'chai';
Expand Down Expand Up @@ -116,4 +115,4 @@ describe('Api Client', () => {

done();
});
});
});
16 changes: 6 additions & 10 deletions test/src/tests-audience-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import AudienceManager, {
IAudienceMemberships, IAudienceMembershipsServerResponse
} from '../../src/audienceManager';
import Logger from '../../src/logger.js';
import Utils from './config/utils';
const { fetchMockSuccess } = Utils;

declare global {
interface Window {
Expand All @@ -19,22 +21,17 @@ declare global {
const userAudienceUrl = `https://${Constants.DefaultBaseUrls.userAudienceUrl}${apiKey}/audience`;

describe('AudienceManager', () => {
let mockServer;
before(function() {
fetchMock.restore();
sinon.restore();
});

beforeEach(function() {
fetchMock.restore();
mockServer = sinon.createFakeServer();
mockServer.respondImmediately = true;

mockServer.respondWith(urls.identify, [
200,
{},
JSON.stringify({ mpid: testMPID, is_logged_in: false }),
]);
fetchMockSuccess(urls.identify, {
mpid: testMPID, is_logged_in: false
});

window.mParticle.config.flags = {
eventBatchingIntervalMillis: 1000,
Expand All @@ -43,7 +40,6 @@ describe('AudienceManager', () => {

afterEach(() => {
sinon.restore();
mockServer.reset();
fetchMock.restore();
});

Expand Down Expand Up @@ -171,4 +167,4 @@ describe('AudienceManager', () => {
);
});
});
});
});
Loading

0 comments on commit 46f5938

Please sign in to comment.