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

refactor: Migrate Instance Manager Tests to TypeScript #952

Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ export default function APIClient(
this.queueEventForBatchUpload(event);
}

if (event.EventName !== Types.MessageType.AppStateTransition) {
// https://go.mparticle.com/work/SQDSDKS-6935
// While Event Name is 'usually' a string, there are some cases where it is a number
// in that it could be a type of MessageType Enum
if (event.EventName as unknown as number !== Types.MessageType.AppStateTransition) {
if (kitBlocker && kitBlocker.kitBlockingEnabled) {
event = kitBlocker.createBlockedEvent(event);
}
Expand Down
46 changes: 26 additions & 20 deletions src/batchUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Batch } from '@mparticle/event-models';
import Constants from './constants';
import { SDKEvent, MParticleWebSDK, SDKLoggerApi } from './sdkRuntimeModels';
import { convertEvents } from './sdkToEventsApiConverter';
import Types from './types';
import { MessageType } from './types';
import { getRampNumber, isEmpty } from './utils';
import { SessionStorageVault, LocalStorageVault } from './vault';
import {
Expand Down Expand Up @@ -167,29 +167,35 @@ export class BatchUploader {
* @param event event that should be queued
*/
public queueEvent(event: SDKEvent): void {
if (!isEmpty(event)) {
this.eventsQueuedForProcessing.push(event);
if (this.offlineStorageEnabled && this.eventVault) {
this.eventVault.store(this.eventsQueuedForProcessing);
}
this.mpInstance.Logger.verbose(
`Queuing event: ${JSON.stringify(event)}`
);
this.mpInstance.Logger.verbose(
`Queued event count: ${this.eventsQueuedForProcessing.length}`
);
if (isEmpty(event)) {
return;
}

// TODO: Remove this check once the v2 code path is removed
// https://go.mparticle.com/work/SQDSDKS-3720
if (
!this.batchingEnabled ||
Types.TriggerUploadType[event.EventDataType]
) {
this.prepareAndUpload(false, false);
}
const { verbose } = this.mpInstance.Logger;

this.eventsQueuedForProcessing.push(event);
if (this.offlineStorageEnabled && this.eventVault) {
this.eventVault.store(this.eventsQueuedForProcessing);
}

verbose(`Queuing event: ${JSON.stringify(event)}`);
verbose(`Queued event count: ${this.eventsQueuedForProcessing.length}`);

if (this.shouldTriggerImmediateUpload(event.EventDataType)) {
this.prepareAndUpload(false, false);
}
}

// https://go.mparticle.com/work/SQDSDKS-3720
private shouldTriggerImmediateUpload (eventDataType: number): boolean {
const priorityEvents = [
MessageType.Commerce,
MessageType.UserIdentityChange,
] as const;

return !this.batchingEnabled || priorityEvents.includes(eventDataType as typeof priorityEvents[number]);
};

/**
* This implements crucial logic to:
* - bucket pending events by MPID, and then by Session, and upload individual batches for each bucket.
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ const Constants = {
Login: 'login',
Identify: 'identify',
},

Environment: {
Development: 'development',
Production: 'production',
},
} as const;

export default Constants;
Expand Down
6 changes: 3 additions & 3 deletions src/identity-user-interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AllUserAttributes, MPID, Product, User } from '@mparticle/web-sdk';
import { SDKIdentityTypeEnum } from './identity.interfaces';
import { MessageType } from './types.interfaces';
import { MessageType } from './types';
import { BaseEvent } from './sdkRuntimeModels';
import Constants from './constants';
const { HTTPCodes } = Constants;
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface ISDKUserIdentityChangeData {
}

export interface IUserIdentityChangeEvent extends BaseEvent {
messageType: MessageType.UserIdentityChange;
messageType: typeof MessageType.UserIdentityChange;
userIdentityChanges: ISDKUserIdentityChanges;
}

Expand All @@ -75,7 +75,7 @@ export interface ISDKUserAttributeChangeData {
}

export interface IUserAttributeChangeEvent extends BaseEvent {
messageType: MessageType.UserAttributeChange;
messageType: typeof MessageType.UserAttributeChange;
userAttributeChanges: ISDKUserAttributeChangeData;
}

Expand Down
5 changes: 2 additions & 3 deletions src/identity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Constants, { HTTP_OK } from './constants';
import Types from './types';
import Types, { IdentityType } from './types';
import {
cacheOrClearIdCache,
createKnownIdentities,
Expand All @@ -17,7 +17,6 @@ import {
isObject,
} from './utils';
import { hasMPIDAndUserLoginChanged, hasMPIDChanged } from './user-utils';
import { getNewIdentitiesByName } from './type-utils';
import { processReadyQueue } from './pre-init-utils';

export default function Identity(mpInstance) {
Expand Down Expand Up @@ -1623,7 +1622,7 @@ export default function Identity(mpInstance) {
self.setForwarderCallbacks(newUser, method);
}

const newIdentitiesByName = getNewIdentitiesByName(
const newIdentitiesByName = IdentityType.getNewIdentitiesByName(
newIdentitiesByType
);

Expand Down
13 changes: 6 additions & 7 deletions src/kitFilterHelper.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { generateHash } from "./utils";
import { generateHash, valueof } from "./utils";
// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5381
import { EventTypeEnum, IdentityType } from "./types.interfaces";
import Constants from './constants';
import { EventType, IdentityType } from "./types";

export default class KitFilterHelper {
static hashEventType(eventType: EventTypeEnum): number {
static hashEventType(eventType: valueof<typeof EventType>): number {
return generateHash(eventType as unknown as string);
};

static hashEventName(eventName: string, eventType: EventTypeEnum): number {
static hashEventName(eventName: string, eventType: valueof<typeof EventType>): number {
return generateHash(eventType + eventName);
};

static hashEventAttributeKey(eventType: EventTypeEnum, eventName: string, customAttributeName: string): number {
static hashEventAttributeKey(eventType: valueof<typeof EventType>, eventName: string, customAttributeName: string): number {
return generateHash(eventType + eventName + customAttributeName);
}

Expand All @@ -22,7 +21,7 @@ export default class KitFilterHelper {

// User Identities are not actually hashed, this method is named this way to
// be consistent with the filter class. UserIdentityType is also a number
static hashUserIdentity(userIdentity: IdentityType): IdentityType {
static hashUserIdentity(userIdentity: typeof IdentityType): typeof IdentityType {
return userIdentity;
}

Expand Down
4 changes: 2 additions & 2 deletions src/mp-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ export default function mParticleInstance(instanceName) {
*/
this.getEnvironment = function() {
return self._Store.SDKConfig.isDevelopmentMode
? Types.Environment.Development
: Types.Environment.Production;
? Constants.Environment.Development
: Constants.Environment.Production;
};
/**
* Returns the mParticle SDK version number
Expand Down
17 changes: 13 additions & 4 deletions src/sdkRuntimeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@mparticle/web-sdk';
import { IStore } from './store';
import Validators from './validators';
import { Dictionary } from './utils';
import { Dictionary, valueof } from './utils';
import { IServerModel } from './serverModel';
import { IKitConfigs } from './configAPIClient';
import { SDKConsentApi, SDKConsentState } from './consent';
Expand All @@ -29,7 +29,12 @@ import {
IdentityCallback,
ISDKUserAttributes,
} from './identity-user-interfaces';
import { IIdentityType } from './types.interfaces';
import {
CommerceEventType,
EventType,
IdentityType,
PromotionActionType,
} from './types';
import IntegrationCapture from './integrationCapture';
import { INativeSdkHelpers } from './nativeSdkHelpers.interfaces';
import { ICookieSyncManager, IPixelConfiguration } from './cookieSyncManager.interfaces';
Expand Down Expand Up @@ -154,7 +159,10 @@ interface IEvents {
export interface MParticleWebSDK {
addForwarder(mockForwarder: MPForwarder): void;
_IntegrationCapture: IntegrationCapture;
IdentityType: IIdentityType;
IdentityType: valueof<typeof IdentityType>;
CommerceEventType: valueof<typeof CommerceEventType>;
EventType: valueof<typeof EventType>;
PromotionType: valueof<typeof PromotionActionType>;
_Identity: IIdentity;
Identity: SDKIdentityApi;
Logger: SDKLoggerApi;
Expand Down Expand Up @@ -189,7 +197,7 @@ export interface MParticleWebSDK {
getDeviceId(): string;
setDeviceId(deviceId: string): void;
setSessionAttribute(key: string, value: string): void;
getInstance(): MParticleWebSDK; // TODO: Create a new type for MParticleWebSDKInstance
getInstance(instanceName?: string): MParticleWebSDK; // TODO: Create a new type for MParticleWebSDKInstance
ServerModel();
upload();
setLogLevel(logLevel: LogLevelType): void;
Expand All @@ -207,6 +215,7 @@ export interface MParticleWebSDK {
ProductActionType: SDKProductActionType;
generateHash(value: string): string;
isIOS?: boolean;
sessionManager: Pick<ISessionManager, 'getSession'>;
}

// Used in cases where server requires booleans as strings
Expand Down
23 changes: 12 additions & 11 deletions src/sideloadedKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ import {
IKitFilterSettings,
} from './configAPIClient';
import { UnregisteredKit } from './forwarders.interfaces';
import { EventTypeEnum, IdentityType } from './types.interfaces';
import { EventType, IdentityType } from './types';
import { valueof } from './utils';

export interface IMPSideloadedKit {
kitInstance: UnregisteredKit;
filterDictionary: IKitFilterSettings;

addEventTypeFilter(eventType: EventTypeEnum): void;
addEventNameFilter(eventType: EventTypeEnum, eventName: string): void;
addEventTypeFilter(eventType: valueof<typeof EventType>): void;
addEventNameFilter(eventType: valueof<typeof EventType>, eventName: string): void;
addEventAttributeFilter(
eventType: EventTypeEnum,
eventType: valueof<typeof EventType>,
eventName: string,
customAttributeKey: string
): void;
addScreenNameFilter(screenName: string): void;
addScreenAttributeFilter(screenName: string, screenAttribute: string): void;
addUserIdentityFilter(userIdentity: IdentityType): void;
addUserIdentityFilter(userIdentity: typeof IdentityType): void;
addUserAttributeFilter(userAttributeKey: string): void;
}

Expand Down Expand Up @@ -57,13 +58,13 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
this.kitInstance = unregisteredKitInstance;
}

public addEventTypeFilter(eventType: EventTypeEnum): void {
public addEventTypeFilter(eventType: valueof<typeof EventType>): void {
const hashedEventType = KitFilterHelper.hashEventType(eventType);
this.filterDictionary.eventTypeFilters.push(hashedEventType);
}

public addEventNameFilter(
eventType: EventTypeEnum,
eventType: valueof<typeof EventType>,
eventName: string
): void {
const hashedEventName = KitFilterHelper.hashEventName(
Expand All @@ -74,7 +75,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
}

public addEventAttributeFilter(
eventType: EventTypeEnum,
eventType: valueof<typeof EventType>,
eventName: string,
customAttributeKey: string
): void {
Expand All @@ -89,7 +90,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
public addScreenNameFilter(screenName: string): void {
const hashedScreenName = KitFilterHelper.hashEventName(
screenName,
EventTypeEnum.Unknown
EventType.Unknown,
);
this.filterDictionary.screenNameFilters.push(hashedScreenName);
}
Expand All @@ -99,7 +100,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
screenAttribute: string
): void {
const hashedScreenAttribute = KitFilterHelper.hashEventAttributeKey(
EventTypeEnum.Unknown,
EventType.Unknown,
screenName,
screenAttribute
);
Expand All @@ -108,7 +109,7 @@ export default class MPSideloadedKit implements IMPSideloadedKit{
);
}

public addUserIdentityFilter(userIdentity: IdentityType): void {
public addUserIdentityFilter(userIdentity: typeof IdentityType): void {
const hashedIdentityType = KitFilterHelper.hashUserIdentity(
userIdentity
);
Expand Down
66 changes: 0 additions & 66 deletions src/type-utils.ts

This file was deleted.

Loading
Loading