Skip to content

Commit b224785

Browse files
committed
add tests for more ts code patterns
1 parent 3aa015b commit b224785

File tree

5 files changed

+214
-7
lines changed

5 files changed

+214
-7
lines changed

tests/analyzeTypeScript.test.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@ test.describe('analyzeTsFile', () => {
2323
}
2424

2525
test('should correctly analyze TypeScript file with multiple tracking providers', () => {
26-
const customFunction = 'customTrackFunction(userId, EVENT_NAME, PROPERTIES)';
27-
const customFunctionSignatures = [parseCustomFunctionSignature(customFunction)];
26+
const customFunctions = [
27+
'customTrackFunction(userId, EVENT_NAME, PROPERTIES)',
28+
'customTrackFunction5',
29+
'customTrackFunction6(EVENT_NAME, PROPERTIES)',
30+
'customTrackFunction7(EVENT_NAME, PROPERTIES)',
31+
'this.props.customTrackFunction6(EVENT_NAME, PROPERTIES)'
32+
];
33+
const customFunctionSignatures = customFunctions.map(parseCustomFunctionSignature);
2834
const program = createProgram(testFilePath);
2935
const events = analyzeTsFile(testFilePath, program, customFunctionSignatures);
3036

3137
// Sort events by line number for consistent ordering
3238
events.sort((a, b) => a.line - b.line);
3339

34-
assert.strictEqual(events.length, 15);
40+
assert.strictEqual(events.length, 19);
3541

3642
// Test Google Analytics event
3743
const gaEvent = events.find(e => e.eventName === 'order_completed' && e.source === 'googleanalytics');
@@ -360,6 +366,24 @@ test.describe('analyzeTsFile', () => {
360366
items: { type: 'string' }
361367
}
362368
});
369+
370+
// Test InitiatedPayment custom event (nested dispatch)
371+
const initiatedPaymentEvent = events.find(e => e.eventName === 'InitiatedPayment');
372+
assert.ok(initiatedPaymentEvent);
373+
assert.strictEqual(initiatedPaymentEvent.source, 'custom');
374+
assert.deepStrictEqual(initiatedPaymentEvent.properties, {
375+
containerSection: { type: 'string' },
376+
tierCartIntent: { type: 'string' }
377+
});
378+
379+
// Test FailedPayment custom event (variable properties) – from customTrackFunction5
380+
const failedPaymentEvent = events.find(e => e.eventName === 'FailedPayment');
381+
assert.ok(failedPaymentEvent);
382+
assert.strictEqual(failedPaymentEvent.source, 'custom');
383+
assert.deepStrictEqual(failedPaymentEvent.properties, {
384+
containerSection: { type: 'string' },
385+
amount: { type: 'number' }
386+
});
363387
});
364388

365389
test('should handle files without tracking events', () => {
@@ -892,6 +916,9 @@ test.describe('analyzeTsFile', () => {
892916
{ sig: 'customTrackFunction3(EVENT_NAME, PROPERTIES, userEmail)', event: 'custom_event3' },
893917
{ sig: 'customTrackFunction4(userId, EVENT_NAME, userAddress, PROPERTIES, userEmail)', event: 'custom_event4' },
894918
{ sig: 'CustomModule.track(userId, EVENT_NAME, PROPERTIES)', event: 'custom_module_event' },
919+
{ sig: 'customTrackFunction5', event: 'FailedPayment' },
920+
{ sig: 'this.props.customTrackFunction6(EVENT_NAME, PROPERTIES)', event: 'ViewedAttorneyAgreement' },
921+
{ sig: 'customTrackFunction7(EVENT_NAME, PROPERTIES)', event: 'InitiatedPayment' },
895922
];
896923

897924
variants.forEach(({ sig, event }) => {
@@ -911,7 +938,10 @@ test.describe('analyzeTsFile', () => {
911938
'customTrackFunction2(userId, EVENT_NAME, PROPERTIES)',
912939
'customTrackFunction3(EVENT_NAME, PROPERTIES, userEmail)',
913940
'customTrackFunction4(userId, EVENT_NAME, userAddress, PROPERTIES, userEmail)',
914-
'CustomModule.track(userId, EVENT_NAME, PROPERTIES)'
941+
'CustomModule.track(userId, EVENT_NAME, PROPERTIES)',
942+
'customTrackFunction5',
943+
'this.props.customTrackFunction6(EVENT_NAME, PROPERTIES)',
944+
'customTrackFunction7(EVENT_NAME, PROPERTIES)',
915945
];
916946

917947
const customFunctionSignatures = variants.map(parseCustomFunctionSignature);
@@ -926,7 +956,10 @@ test.describe('analyzeTsFile', () => {
926956
'custom_event2',
927957
'custom_event3',
928958
'custom_event4',
929-
'custom_module_event'
959+
'custom_module_event',
960+
'FailedPayment',
961+
'ViewedAttorneyAgreement',
962+
'InitiatedPayment'
930963
];
931964

932965
expectedEventNames.forEach(eventName => {
@@ -936,6 +969,6 @@ test.describe('analyzeTsFile', () => {
936969

937970
// Ensure built-in provider events remain unaffected
938971
const builtInCount = events.filter(e => e.source !== 'custom').length;
939-
assert.ok(builtInCount >= 12, 'Should still include built-in provider events');
972+
assert.ok(builtInCount >= 10, 'Should still include built-in provider events');
940973
});
941974
});

tests/cli.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ const customFunctionSignatures = [
1616
'customTrackFunction2(userId, EVENT_NAME, PROPERTIES)',
1717
'customTrackFunction3(EVENT_NAME, PROPERTIES, userEmail)',
1818
'customTrackFunction4(userId, EVENT_NAME, userAddress, PROPERTIES, userEmail)',
19-
'CustomModule.track(userId, EVENT_NAME, PROPERTIES)'
19+
'CustomModule.track(userId, EVENT_NAME, PROPERTIES)',
20+
'customTrackFunction5',
21+
'customTrackFunction6(EVENT_NAME, PROPERTIES)',
22+
'this.props.customTrackFunction6(EVENT_NAME, PROPERTIES)',
23+
'customTrackFunction7(EVENT_NAME, PROPERTIES)',
2024
];
2125

2226
// Helper function to run CLI and capture output

tests/fixtures/tracking-schema-all.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,3 +1102,36 @@ events:
11021102
type: string
11031103
checkout_step:
11041104
type: number
1105+
FailedPayment:
1106+
implementations:
1107+
- path: typescript/main.ts
1108+
line: 361
1109+
function: global
1110+
destination: custom
1111+
properties:
1112+
containerSection:
1113+
type: string
1114+
amount:
1115+
type: number
1116+
InitiatedPayment:
1117+
implementations:
1118+
- path: typescript/main.ts
1119+
line: 349
1120+
function: global
1121+
destination: custom
1122+
- path: typescript/main.ts
1123+
line: 422
1124+
function: trackInitiatedPayment
1125+
destination: custom
1126+
properties:
1127+
containerSection:
1128+
type: string
1129+
tierCartIntent:
1130+
type: string
1131+
ViewedAttorneyAgreement:
1132+
implementations:
1133+
- path: typescript/main.ts
1134+
line: 375
1135+
function: handleView
1136+
destination: custom
1137+
properties: {}

tests/fixtures/typescript/main.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,107 @@ CustomModule.track('user333', 'custom_module_event', {
323323
order_id: 'order_xyz',
324324
foo: 'bar'
325325
});
326+
327+
// -----------------------------------------------------------------------------
328+
// Additional Object.freeze constant and customTrackFunction6 patterns (new tests)
329+
// -----------------------------------------------------------------------------
330+
331+
export const TELEMETRY_EVENTS = Object.freeze({
332+
VIEWED_TRANSITION: 'ViewedTransition',
333+
INITIATED_PAYMENT: 'InitiatedPayment',
334+
FAILED_PAYMENT: 'FailedPayment',
335+
VIEWED_ATTORNEY_AGREEMENT: 'ViewedAttorneyAgreement',
336+
ACCEPTED_ATTORNEY_AGREEMENT: 'AcceptedAttorneyAgreement',
337+
DECLINED_ATTORNEY_AGREEMENT_FOR_REFUND: 'DeclinedAttorneyAgreementForRefund',
338+
SUCCEEDED_PAYMENT: 'SucceededPayment',
339+
});
340+
341+
declare function customTrackFunction5(EVENT_NAME: string, PROPERTIES: Record<string, any>): void;
342+
declare function customTrackFunction6(EVENT_NAME: string, PROPERTIES: Record<string, any>): void;
343+
declare function customTrackFunction7(EVENT_NAME: string, PROPERTIES: Record<string, any>): void;
344+
345+
declare function dispatch(action: any): void;
346+
347+
// Nested inside another function call (e.g., Redux dispatch pattern)
348+
dispatch(
349+
customTrackFunction7(TELEMETRY_EVENTS.INITIATED_PAYMENT, {
350+
containerSection: 'PaymentPage',
351+
tierCartIntent: 'Gold',
352+
})
353+
);
354+
355+
// Variable reference as properties argument
356+
const paymentArgs = {
357+
containerSection: 'Checkout',
358+
amount: 99.99,
359+
};
360+
dispatch(
361+
customTrackFunction5(TELEMETRY_EVENTS.FAILED_PAYMENT, paymentArgs)
362+
);
363+
364+
// Member expression chain: this.props.customTrackFunction6
365+
class ExampleComponent {
366+
props: { customTrackFunction6: (evt: string, props: Record<string, any>) => void };
367+
368+
constructor() {
369+
this.props = {
370+
customTrackFunction6: () => {},
371+
};
372+
}
373+
374+
handleView() {
375+
this.props.customTrackFunction6(TELEMETRY_EVENTS.VIEWED_ATTORNEY_AGREEMENT, {});
376+
}
377+
}
378+
379+
// -----------------------------------------------------------------------------
380+
// Redux-style mapDispatchToActions with nested customTrackFunction6 patterns
381+
// -----------------------------------------------------------------------------
382+
383+
interface ExplicitPropsRedux {
384+
tier: string;
385+
containerSection: string;
386+
applicationFeeInCents: number;
387+
}
388+
389+
interface MappedProps {}
390+
391+
type GlobalErrorObject = { type: string; payload: any };
392+
393+
type ActionProps = Record<string, any>;
394+
395+
declare function closeModal(...args: any[]): void;
396+
declare function postTelemetryWithConversion(
397+
eventName: string,
398+
props: Record<string, any>,
399+
conversionEvent: string,
400+
extra: Record<string, any>,
401+
urls: string[],
402+
destinations: string[]
403+
): void;
404+
405+
declare const CONVERSION_TRACKING_EVENTS: { PAYMENT: string };
406+
declare const CONVERSION_TRACKING_DESTINATIONS: { FACEBOOK: string; GOOGLE: string };
407+
408+
declare function trackUserEvent(eventName: string, props: Record<string, any>): void; // alias to customTrackFunction6
409+
410+
function mapDispatchToActions(dispatch: Function, ownProps: ExplicitPropsRedux & MappedProps): ActionProps {
411+
return {
412+
closeModal: (...args: any[]) => dispatch(closeModal(...args)),
413+
setGlobalError: ({ type, payload }: GlobalErrorObject) => dispatch({ type, payload }),
414+
415+
// Variable-only properties argument
416+
trackFailedPayment: (args: Record<string, any>) => dispatch(
417+
customTrackFunction6(TELEMETRY_EVENTS.FAILED_PAYMENT, args)
418+
),
419+
420+
// Object literal with spread + additional props
421+
trackInitiatedPayment: (args: Record<string, any>) => dispatch(
422+
customTrackFunction7(TELEMETRY_EVENTS.INITIATED_PAYMENT, {
423+
...args,
424+
containerSection: ownProps.containerSection,
425+
tierCartIntent: ownProps.tier,
426+
})
427+
),
428+
};
429+
}

tests/fixtures/typescript/tracking-schema-typescript.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,36 @@ events:
435435
type: string
436436
userId:
437437
type: string
438+
FailedPayment:
439+
implementations:
440+
- path: main.ts
441+
line: 361
442+
function: global
443+
destination: custom
444+
properties:
445+
containerSection:
446+
type: string
447+
amount:
448+
type: number
449+
InitiatedPayment:
450+
implementations:
451+
- path: main.ts
452+
line: 349
453+
function: global
454+
destination: custom
455+
- path: main.ts
456+
line: 422
457+
function: trackInitiatedPayment
458+
destination: custom
459+
properties:
460+
containerSection:
461+
type: string
462+
tierCartIntent:
463+
type: string
464+
ViewedAttorneyAgreement:
465+
implementations:
466+
- path: main.ts
467+
line: 375
468+
function: handleView
469+
destination: custom
470+
properties: {}

0 commit comments

Comments
 (0)