Skip to content

Commit 40d4f1d

Browse files
refactor: Migrate Forwarder Tests to TypeScript (#976)
1 parent b70bd20 commit 40d4f1d

File tree

7 files changed

+392
-777
lines changed

7 files changed

+392
-777
lines changed

src/mp-instance.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ export default function mParticleInstance(this: IMParticleWebSDKInstance, instan
129129

130130
// required for forwarders once they reference the mparticle instance
131131
this.IdentityType = IdentityType;
132-
this.EventType = EventType as unknown as valueof<typeof EventType>;
133-
this.CommerceEventType = CommerceEventType as unknown as valueof<typeof CommerceEventType>;
134-
this.PromotionType = PromotionActionType as unknown as valueof<typeof PromotionActionType>;
135-
this.ProductActionType = ProductActionType as unknown as valueof<typeof ProductActionType>;
132+
this.EventType = EventType;
133+
this.CommerceEventType = CommerceEventType;
134+
this.PromotionType = PromotionActionType;
135+
this.ProductActionType = ProductActionType;
136136

137137

138138
this._Identity = new Identity(this);

src/mparticle-instance-manager.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import Polyfill from './polyfill';
2-
import Types, { CommerceEventType, EventType, ProductActionType, PromotionActionType } from './types';
2+
import { CommerceEventType, EventType, IdentityType, ProductActionType, PromotionActionType } from './types';
33
import Constants from './constants';
44
import mParticleInstance, { IMParticleWebSDKInstance } from './mp-instance.js';
55
import _BatchValidator from './mockBatchCreator';
66
import MPSideloadedKit from './sideloadedKit';
77
import { IMParticleInstanceManager } from './sdkRuntimeModels';
88
import { IStore } from './store';
99
import { Dictionary } from '@mparticle/web-sdk';
10-
import { valueof } from './utils';
1110

1211
if (!Array.prototype.forEach) {
1312
Array.prototype.forEach = Polyfill.forEach;
@@ -32,11 +31,11 @@ function mParticleInstanceManager(this: IMParticleInstanceManager) {
3231
// Only leaving this here in case any clients are trying to access mParticle.Store, to prevent from throwing
3332
this.Store = {} as IStore;
3433
this._instances = {} as Dictionary<IMParticleWebSDKInstance>;
35-
this.IdentityType = Types.IdentityType;
36-
this.EventType = EventType as unknown as valueof<typeof EventType>;
37-
this.CommerceEventType = CommerceEventType as unknown as valueof<typeof CommerceEventType>;
38-
this.PromotionType = PromotionActionType as unknown as valueof<typeof PromotionActionType>;
39-
this.ProductActionType = ProductActionType as unknown as valueof<typeof ProductActionType>;
34+
this.IdentityType = IdentityType;
35+
this.EventType = EventType;
36+
this.CommerceEventType = CommerceEventType;
37+
this.PromotionType = PromotionActionType;
38+
this.ProductActionType = ProductActionType;
4039

4140
this.MPSideloadedKit = MPSideloadedKit;
4241

src/sdkRuntimeModels.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,11 @@ export interface SDKProduct {
164164
// https://go.mparticle.com/work/SQDSDKS-6949
165165
export interface MParticleWebSDK {
166166
addForwarder(mockForwarder: MPForwarder): void;
167-
IdentityType: valueof<typeof IdentityType>;
168-
CommerceEventType: valueof<typeof CommerceEventType>;
169-
EventType: valueof<typeof EventType>;
170-
PromotionType: valueof<typeof PromotionActionType>;
167+
IdentityType: typeof IdentityType;
168+
CommerceEventType: typeof CommerceEventType;
169+
EventType: typeof EventType;
170+
PromotionType: typeof PromotionActionType;
171+
ProductActionType: typeof ProductActionType;
171172
Identity: SDKIdentityApi;
172173
Logger: SDKLoggerApi;
173174
Consent: SDKConsentApi;
@@ -205,13 +206,15 @@ export interface MParticleWebSDK {
205206
logError(error: IErrorLogMessage, attrs?: SDKEventAttrs): void;
206207
logLink(selector: string, eventName: string, eventType: valueof<typeof EventType>, eventInfo: SDKEventAttrs): void;
207208
logForm(selector: string, eventName: string, eventType: valueof<typeof EventType>, eventInfo: SDKEventAttrs): void;
208-
logPageView(eventName: string, attrs?: SDKEventAttrs, customFlags?: SDKEventCustomFlags, eventOptions?: SDKEventOptions): void;
209+
logPageView(eventName?: string, attrs?: SDKEventAttrs, customFlags?: SDKEventCustomFlags, eventOptions?: SDKEventOptions): void;
209210
setOptOut(isOptingOut: boolean): void;
210211
eCommerce: SDKECommerceAPI;
211212
isInitialized(): boolean;
212-
ProductActionType: valueof<typeof ProductActionType>;
213213
ready(f: Function): void;
214-
reset(instance: IMParticleWebSDKInstance): void;
214+
215+
// https://go.mparticle.com/work/SQDSDKS-7072
216+
reset(instance?: IMParticleWebSDKInstance): void;
217+
215218
setAppName(name: string): void;
216219
setAppVersion(version: string): void;
217220
setOptOut(isOptingOut: boolean): void;

test/src/tests-batchUploader_3.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,27 +147,27 @@ describe('batch uploader', () => {
147147
});
148148
});
149149

150-
it('should return pending uploads if a 500 is returned', function(done) {
150+
it('should return pending uploads if a 500 is returned', async function() {
151151
window.mParticle._resetForTests(MPConfig);
152152

153153
fetchMock.post(urls.events, 500);
154154

155155
window.mParticle.init(apiKey, window.mParticle.config);
156-
waitForCondition(hasIdentifyReturned)
157-
.then(() => {
156+
await waitForCondition(hasIdentifyReturned);
157+
158158
window.mParticle.logEvent('Test Event');
159159

160160
let pendingEvents = window.mParticle.getInstance()._APIClient.uploader.eventsQueuedForProcessing;
161161

162-
pendingEvents.length.should.equal(3)
162+
pendingEvents.length.should.equal(3);
163163
pendingEvents[0].EventName.should.equal(1);
164164
pendingEvents[1].EventName.should.equal(10);
165165
pendingEvents[2].EventName.should.equal('Test Event');
166166

167167
fetchMock.post(urls.events, 200);
168168

169169
// First fetch call is an identify call
170-
(fetchMock.lastCall()[0].endsWith('identify')).should.equal(true)
170+
(fetchMock.lastCall()[0].endsWith('identify')).should.equal(true);
171171
window.mParticle.upload();
172172

173173
let nowPendingEvents = window.mParticle.getInstance()._APIClient.uploader.eventsQueuedForProcessing;
@@ -178,9 +178,6 @@ describe('batch uploader', () => {
178178
batch.events[1].event_type.should.equal('application_state_transition');
179179
batch.events[2].event_type.should.equal('custom_event');
180180
batch.events[2].data.event_name.should.equal('Test Event');
181-
182-
done();
183-
})
184181
});
185182

186183
it('should send source_message_id with events to v3 endpoint', function(done) {

0 commit comments

Comments
 (0)