Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Refactor test set up for readability #810

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ test/stub/test-stub-bundle.js
test/integrations/requirejs/test-requirejs-bundle.js
browserstack.err
local.log
test/cross-browser-testing/CBT-tests-es5.js
test/cross-browser-testing/CBT-tests-es5.js
test/cross-browser-testing/CBT-tests.js
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"repository": "https://github.com/mParticle/mparticle-web-sdk",
"scripts": {
"build": "cross-env ENVIRONMENT=prod BUILDALL=true rollup --config rollup.config.js",
"build:cbt": "rollup --config test/cross-browser-testing/rollup.CBT.config.js && rm test/cross-browser-testing/CBT-tests.js",
"build:cbt": "rollup --config test/cross-browser-testing/rollup.CBT.config.js -w",
"build:dev": "cross-env ENVIRONMENT=dev BUILD=iife rollup --config rollup.config.js",
"build:iife": "cross-env ENVIRONMENT=prod BUILD=iife rollup --config rollup.config.js",
"build:npm": "cross-env ENVIRONMENT=prod BUILD=cjs rollup --config rollup.config.js",
Expand Down
2 changes: 1 addition & 1 deletion rollup.test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const extensions = ['.js', '.ts'];

const builds = {
main: {
input: 'test/src/tests-main.js',
input: 'test/src/_test.index.ts',
output: {
file: 'test/test-bundle.js',
format: 'iife',
Expand Down
11 changes: 8 additions & 3 deletions src/sdkRuntimeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export interface MParticleWebSDK {
_NativeSdkHelpers: any; // TODO: Set up API
_Persistence: IPersistence;
_preInit: any; // TODO: Set up API
_instances?: Dictionary<MParticleWebSDK>;
_isTestEnv?: boolean;
_resetForTests(
MPConfig?: SDKInitConfig,
keepPersistence?: boolean,
Expand Down Expand Up @@ -205,22 +207,25 @@ export interface SDKInitConfig
dataPlanOptions?: KitBlockerOptions;
flags?: Dictionary;

aliasMaxWindow: number;
aliasMaxWindow?: number;
deviceId?: string;
forceHttps?: boolean;
aliasUrl?: string;
configUrl?: string;
identityUrl?: string;
integrationDelayTimeout: number;
integrationDelayTimeout?: number;
isIOS?: boolean;
maxProducts: number;
maxProducts?: number;
requestConfig?: boolean;
sessionTimeout?: number;
useNativeSdk?: boolean;
useCookieStorage?: boolean;
v1SecureServiceUrl?: string;
v2SecureServiceUrl?: string;
v3SecureServiceUrl?: string;

workspaceToken?: string;
isDevelopmentMode?: boolean;
}

export interface DataPlanConfig {
Expand Down
2 changes: 1 addition & 1 deletion test/cross-browser-testing/rollup.CBT.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import json from '@rollup/plugin-json';

export default [
{
input: 'test/src/tests-main.js',
input: 'test/src/_test.index.ts',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per our conversation this morning, I thought we were just going to call this index.ts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I thought we were just removing the underscore. If the idea is to eventually rename things to module.spec.ts like you have for vault.spec.ts, then once we have things higher alphabetically, it's better to keep it the underscore to keep it at the top of the folder.

output: {
file: 'test/cross-browser-testing/CBT-tests.js',
format: 'umd',
Expand Down
2 changes: 1 addition & 1 deletion test/jest/sideloadedKit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IMPSideloadedKitConstructor } from "../../src/sideloadedKit";
import { EventTypeEnum, IdentityType } from "../../src/types.interfaces";
import { UnregisteredKit } from '../../src/forwarders.interfaces';
import { IKitFilterSettings } from '../../src/configAPIClient';
import { mParticle } from "../src/config";
import { mParticle } from "../src/config/constants";

const mockKitInstance: UnregisteredKit = {
register: function() {}
Expand Down
31 changes: 31 additions & 0 deletions test/src/_test.index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Import set up logic
import './config/setup';

// Import each test module
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';
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';
2 changes: 1 addition & 1 deletion test/src/config.ts → test/src/config/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SDKInitConfig } from "../../src/sdkRuntimeModels";
import { SDKInitConfig } from "../../../src/sdkRuntimeModels";

export const urls = {
events: 'https://jssdks.mparticle.com/v3/JS/test_key/events',
Expand Down
38 changes: 38 additions & 0 deletions test/src/config/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MPConfig, workspaceToken } from './constants';
import { MParticleWebSDK } from '../../../src/sdkRuntimeModels';

declare global {
interface Window {
mParticle: MParticleWebSDK;
}
}

var userApi = null;
rmi22186 marked this conversation as resolved.
Show resolved Hide resolved

window.mParticle._isTestEnv = true;

beforeEach(function() {
// mocha can't clean up after itself, so this lets
// tests mock the current user and restores in between runs.
if (!userApi) {
userApi = window.mParticle.getInstance().Identity.getCurrentUser;
} else {
window.mParticle.getInstance().Identity.getCurrentUser = userApi;
}

window.mParticle.config = {
workspaceToken: workspaceToken,
logLevel: 'none',
kitConfigs: [],
requestConfig: false,
isDevelopmentMode: false,
flags: {
eventBatchingIntervalMillis: 0,
}
};

// This is to tell the resetPersistence method that we are in a test environment
// It should probably be refactored to be included as an argument
window.mParticle._resetForTests(MPConfig);
delete window.mParticle._instances['default_instance'];
});
4 changes: 2 additions & 2 deletions test/src/utils.js → test/src/config/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Utils from '../../src/utils';
import * as Utils from '../../../src/utils';
import {
apiKey,
testMPID,
Expand All @@ -8,7 +8,7 @@ import {
workspaceToken,
workspaceCookieName,
das,
} from './config';
} from './constants';

var pluses = /\+/g,
getLocalStorageProducts = function getLocalStorageProducts() {
Expand Down
2 changes: 1 addition & 1 deletion test/src/tests-apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Types from '../../src/types';
import Constants from '../../src/constants';
import { apiKey, MPConfig } from './config';
import { apiKey, MPConfig } from './config/constants';
import { MParticleWebSDK } from '../../src/sdkRuntimeModels';
import { expect } from 'chai';

Expand Down
6 changes: 3 additions & 3 deletions test/src/tests-batchUploader.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import sinon from 'sinon';
import { urls } from './config';
import { apiKey, MPConfig, testMPID } from './config';
import { urls } from './config/constants';
import { apiKey, MPConfig, testMPID } from './config/constants';
rmi22186 marked this conversation as resolved.
Show resolved Hide resolved
import {
BaseEvent,
MParticleWebSDK,
SDKEvent,
SDKProductActionType,
} from '../../src/sdkRuntimeModels';
import { Batch, CustomEventData } from '@mparticle/event-models';
import Utils from './utils';
import Utils from './config/utils';
import { BatchUploader } from '../../src/batchUploader';
import { expect } from 'chai';
import _BatchValidator from '../../src/mockBatchCreator';
Expand Down
4 changes: 2 additions & 2 deletions test/src/tests-beaconUpload.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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';
import { urls } from './config/constants';
import { apiKey, MPConfig, testMPID } from './config/constants';
rmi22186 marked this conversation as resolved.
Show resolved Hide resolved
import { MParticleWebSDK } from '../../src/sdkRuntimeModels';
import { event0 } from '../fixtures/events';
import { batch1, batch2, batch3 } from '../fixtures/batches';
Expand Down
4 changes: 2 additions & 2 deletions test/src/tests-config-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sinon from 'sinon';
import { urls } from './config';
import { apiKey, MPConfig } from './config';
import { urls } from './config/constants';
import { apiKey, MPConfig } from './config/constants';
rmi22186 marked this conversation as resolved.
Show resolved Hide resolved
import { expect } from 'chai';
import ConfigAPIClient, {
IConfigAPIClient,
Expand Down
6 changes: 3 additions & 3 deletions test/src/tests-consent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Utils from './utils';
import Utils from './config/utils';
import sinon from 'sinon';
import fetchMock from 'fetch-mock/esm/client';
import { urls } from './config';
import { apiKey, MPConfig, testMPID } from './config';
import { urls } from './config/constants';
import { apiKey, MPConfig, testMPID } from './config/constants';
import { MParticleWebSDK } from '../../src/sdkRuntimeModels';
rmi22186 marked this conversation as resolved.
Show resolved Hide resolved
import { expect } from 'chai';
import { GDPRConsentState, PrivacyConsentState } from '@mparticle/web-sdk';
Expand Down
4 changes: 2 additions & 2 deletions test/src/tests-cookie-syncing.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Utils from './utils';
import Utils from './config/utils';
import sinon from 'sinon';
import fetchMock from 'fetch-mock/esm/client';
import { urls, testMPID, MPConfig, v4LSKey, apiKey } from './config';
import { urls, testMPID, MPConfig, v4LSKey, apiKey } from './config/constants';

const { setLocalStorage, MockForwarder, getLocalStorage } = Utils;

Expand Down
5 changes: 2 additions & 3 deletions test/src/tests-core-sdk.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Utils from './utils';
import Utils from './config/utils';
import Store from '../../src/store';
import Constants from '../../src/constants';
import sinon from 'sinon';
import fetchMock from 'fetch-mock/esm/client';
import { urls } from './config';
import { apiKey, das, MPConfig, testMPID, workspaceCookieName } from './config';
import { urls, apiKey, das, MPConfig, testMPID, workspaceCookieName } from './config/constants';

const DefaultConfig = Constants.DefaultConfig,
setLocalStorage = Utils.setLocalStorage,
Expand Down
4 changes: 2 additions & 2 deletions test/src/tests-eCommerce.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Utils from './utils';
import Utils from './config/utils';
import sinon from 'sinon';
import fetchMock from 'fetch-mock/esm/client';
import { urls, apiKey, workspaceToken, MPConfig, testMPID, ProductActionType, PromotionActionType } from './config';
import { urls, apiKey, workspaceToken, MPConfig, testMPID, ProductActionType, PromotionActionType } from './config/constants';

const getLocalStorageProducts = Utils.getLocalStorageProducts,
forwarderDefaultConfiguration = Utils.forwarderDefaultConfiguration,
Expand Down
4 changes: 2 additions & 2 deletions test/src/tests-event-logging.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Utils from './utils';
import Utils from './config/utils';
import sinon from 'sinon';
import fetchMock from 'fetch-mock/esm/client';
import { urls, apiKey,
testMPID,
MPConfig,
MessageType } from './config';
MessageType } from './config/constants';

const getIdentityEvent = Utils.getIdentityEvent,
findEventFromRequest = Utils.findEventFromRequest,
Expand Down
5 changes: 2 additions & 3 deletions test/src/tests-forwarders.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Utils from './utils';
import Utils from './config/utils';
import sinon from 'sinon';
import fetchMock from 'fetch-mock/esm/client';

import { urls } from './config';
import { apiKey, MPConfig, testMPID, MessageType } from './config';
import { urls, apiKey, MPConfig, testMPID, MessageType } from './config/constants';
import { expect } from 'chai';

const findEventFromRequest = Utils.findEventFromRequest,
Expand Down
2 changes: 1 addition & 1 deletion test/src/tests-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiKey } from './config';
import { apiKey } from './config/constants';
import sinon from 'sinon';

describe('helpers', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/src/tests-identities-attributes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Utils from './utils';
import Utils from './config/utils';
import sinon from 'sinon';
import fetchMock from 'fetch-mock/esm/client';
import { urls, apiKey,
testMPID,
MPConfig } from './config';
MPConfig } from './config/constants';

const findEventFromRequest = Utils.findEventFromRequest,
findBatch = Utils.findBatch,
Expand Down
4 changes: 2 additions & 2 deletions test/src/tests-identity.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Constants from '../../src/constants';
import Utils from './utils';
import Utils from './config/utils';
import sinon from 'sinon';
import fetchMock from 'fetch-mock/esm/client';
import { urls, apiKey,
testMPID,
MPConfig,
workspaceCookieName } from './config';
workspaceCookieName } from './config/constants';

const getLocalStorage = Utils.getLocalStorage,
setLocalStorage = Utils.setLocalStorage,
Expand Down
6 changes: 3 additions & 3 deletions test/src/tests-kit-blocking.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sinon from 'sinon';
import { urls } from './config';
import { apiKey, MPConfig, testMPID } from './config';
import { urls } from './config/constants';
import { apiKey, MPConfig, testMPID } from './config/constants';
rmi22186 marked this conversation as resolved.
Show resolved Hide resolved
import { MParticleWebSDK, SDKEvent, SDKProductActionType, DataPlanResult, KitBlockerDataPlan } from '../../src/sdkRuntimeModels';
import * as dataPlan from './dataPlan.json';
import Utils from './utils';
import Utils from './config/utils';
import KitBlocker from '../../src/kitBlocking';
import Types from '../../src/types';
import { DataPlanVersion } from '@mparticle/data-planning-models';
Expand Down
4 changes: 2 additions & 2 deletions test/src/tests-mParticleUser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Utils from './utils';
import Utils from './config/utils';
import sinon from 'sinon';
import { urls, apiKey, MPConfig } from './config';
import { urls, apiKey, MPConfig } from './config/constants';

const forwarderDefaultConfiguration = Utils.forwarderDefaultConfiguration,
MockForwarder = Utils.MockForwarder;
Expand Down
Loading
Loading