Skip to content

Commit 2048e5f

Browse files
committed
update yaml tests fixtures
1 parent 1d79fca commit 2048e5f

File tree

6 files changed

+417
-21
lines changed

6 files changed

+417
-21
lines changed

tests/analyzeJavaScript.test.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,25 +342,29 @@ test.describe('analyzeJsFile', () => {
342342
});
343343

344344
test('should detect events for all custom function signature variations', () => {
345+
const methodEventFile = path.join(fixturesDir, 'javascript', 'method-event.js');
345346
const variants = [
346-
{ sig: 'customTrackFunction0', event: 'custom_event0' },
347-
{ sig: 'customTrackFunction1(EVENT_NAME, PROPERTIES)', event: 'custom_event1' },
348-
{ sig: 'customTrackFunction2(userId, EVENT_NAME, PROPERTIES)', event: 'custom_event2' },
349-
{ sig: 'customTrackFunction3(EVENT_NAME, PROPERTIES, userEmail)', event: 'custom_event3' },
350-
{ sig: 'customTrackFunction4(userId, EVENT_NAME, userAddress, PROPERTIES, userEmail)', event: 'custom_event4' },
351-
{ sig: 'CustomModule.track(userId, EVENT_NAME, PROPERTIES)', event: 'custom_module_event' },
352-
{ sig: 'getTrackingService().track(EVENT_NAME, PROPERTIES)', event: 'myChainedEvent' },
347+
{ sig: 'customTrackFunction0', event: 'custom_event0', file: testFilePath },
348+
{ sig: 'customTrackFunction1(EVENT_NAME, PROPERTIES)', event: 'custom_event1', file: testFilePath },
349+
{ sig: 'customTrackFunction2(userId, EVENT_NAME, PROPERTIES)', event: 'custom_event2', file: testFilePath },
350+
{ sig: 'customTrackFunction3(EVENT_NAME, PROPERTIES, userEmail)', event: 'custom_event3', file: testFilePath },
351+
{ sig: 'customTrackFunction4(userId, EVENT_NAME, userAddress, PROPERTIES, userEmail)', event: 'custom_event4', file: testFilePath },
352+
{ sig: 'CustomModule.track(userId, EVENT_NAME, PROPERTIES)', event: 'custom_module_event', file: testFilePath },
353+
{ sig: 'getTrackingService().track(EVENT_NAME, PROPERTIES)', event: 'myChainedEvent', file: testFilePath },
354+
// Method-as-event signature
355+
{ sig: 'eventCalls.EVENT_NAME(PROPERTIES)', event: 'viewItemList', file: methodEventFile },
353356
];
354357

355-
variants.forEach(({ sig, event }) => {
358+
variants.forEach(({ sig, event, file }) => {
356359
const customFunctionSignatures = [parseCustomFunctionSignature(sig)];
357-
const events = analyzeJsFile(testFilePath, customFunctionSignatures);
360+
const events = analyzeJsFile(file, customFunctionSignatures);
358361
const found = events.find(e => e.eventName === event && e.source === 'custom');
359362
assert.ok(found, `Should detect ${event} for signature ${sig}`);
360363
});
361364
});
362365

363366
test('should detect events when multiple custom function signatures are provided together', () => {
367+
const methodEventFile = path.join(fixturesDir, 'javascript', 'method-event.js');
364368
const variants = [
365369
'customTrackFunction(userId, EVENT_NAME, PROPERTIES)',
366370
'customTrackFunction0',
@@ -395,6 +399,16 @@ test.describe('analyzeJsFile', () => {
395399
// Sanity check – ensure we did not lose built-in provider events
396400
const builtInProvidersCount = events.filter(e => e.source !== 'custom').length;
397401
assert.ok(builtInProvidersCount >= 10, 'Should still include built-in events');
402+
403+
// Test method-as-event signature separately (different file)
404+
const methodAsEventSignatures = [parseCustomFunctionSignature('eventCalls.EVENT_NAME(PROPERTIES)')];
405+
const methodEvents = analyzeJsFile(methodEventFile, methodAsEventSignatures);
406+
407+
const methodEventNames = ['viewItemList', 'addToCart', 'removeFromCart', 'beginCheckout', 'purchase', 'pageView'];
408+
methodEventNames.forEach(eventName => {
409+
const evt = methodEvents.find(e => e.eventName === eventName && e.source === 'custom');
410+
assert.ok(evt, `Expected to find method-as-event ${eventName}`);
411+
});
398412
});
399413

400414
test('should detect events with no properties for custom function', () => {

tests/analyzeTypeScript.test.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -919,22 +919,25 @@ test.describe('analyzeTsFile', () => {
919919
});
920920

921921
test('should detect events for all custom function signature variations', () => {
922+
const methodEventFile = path.join(fixturesDir, 'typescript', 'method-event.ts');
922923
const variants = [
923-
{ sig: 'customTrackFunction0', event: 'custom_event0' },
924-
{ sig: 'customTrackFunction1(EVENT_NAME, PROPERTIES)', event: 'custom_event1' },
925-
{ sig: 'customTrackFunction2(userId, EVENT_NAME, PROPERTIES)', event: 'custom_event2' },
926-
{ sig: 'customTrackFunction3(EVENT_NAME, PROPERTIES, userEmail)', event: 'custom_event3' },
927-
{ sig: 'customTrackFunction4(userId, EVENT_NAME, userAddress, PROPERTIES, userEmail)', event: 'custom_event4' },
928-
{ sig: 'CustomModule.track(userId, EVENT_NAME, PROPERTIES)', event: 'custom_module_event' },
929-
{ sig: 'customTrackFunction5', event: 'FailedPayment' },
930-
{ sig: 'this.props.customTrackFunction6(EVENT_NAME, PROPERTIES)', event: 'ViewedAttorneyAgreement' },
931-
{ sig: 'customTrackFunction7(EVENT_NAME, PROPERTIES)', event: 'InitiatedPayment' },
924+
{ sig: 'customTrackFunction0', event: 'custom_event0', file: testFilePath },
925+
{ sig: 'customTrackFunction1(EVENT_NAME, PROPERTIES)', event: 'custom_event1', file: testFilePath },
926+
{ sig: 'customTrackFunction2(userId, EVENT_NAME, PROPERTIES)', event: 'custom_event2', file: testFilePath },
927+
{ sig: 'customTrackFunction3(EVENT_NAME, PROPERTIES, userEmail)', event: 'custom_event3', file: testFilePath },
928+
{ sig: 'customTrackFunction4(userId, EVENT_NAME, userAddress, PROPERTIES, userEmail)', event: 'custom_event4', file: testFilePath },
929+
{ sig: 'CustomModule.track(userId, EVENT_NAME, PROPERTIES)', event: 'custom_module_event', file: testFilePath },
930+
{ sig: 'customTrackFunction5', event: 'FailedPayment', file: testFilePath },
931+
{ sig: 'this.props.customTrackFunction6(EVENT_NAME, PROPERTIES)', event: 'ViewedAttorneyAgreement', file: testFilePath },
932+
{ sig: 'customTrackFunction7(EVENT_NAME, PROPERTIES)', event: 'InitiatedPayment', file: testFilePath },
933+
// Method-as-event signature
934+
{ sig: 'eventCalls.EVENT_NAME(PROPERTIES)', event: 'viewItemList', file: methodEventFile },
932935
];
933936

934-
variants.forEach(({ sig, event }) => {
935-
const program = createProgram(testFilePath);
937+
variants.forEach(({ sig, event, file }) => {
938+
const program = createProgram(file);
936939
const customFunctionSignatures = [parseCustomFunctionSignature(sig)];
937-
const events = analyzeTsFile(testFilePath, program, customFunctionSignatures);
940+
const events = analyzeTsFile(file, program, customFunctionSignatures);
938941
const found = events.find(e => e.eventName === event && e.source === 'custom');
939942
assert.ok(found, `Should detect ${event} for signature ${sig}`);
940943
});
@@ -980,6 +983,18 @@ test.describe('analyzeTsFile', () => {
980983
// Ensure built-in provider events remain unaffected
981984
const builtInCount = events.filter(e => e.source !== 'custom').length;
982985
assert.ok(builtInCount >= 10, 'Should still include built-in provider events');
986+
987+
// Test method-as-event signature separately (different file)
988+
const methodEventFile = path.join(fixturesDir, 'typescript', 'method-event.ts');
989+
const methodProgram = createProgram(methodEventFile);
990+
const methodAsEventSignatures = [parseCustomFunctionSignature('eventCalls.EVENT_NAME(PROPERTIES)')];
991+
const methodEvents = analyzeTsFile(methodEventFile, methodProgram, methodAsEventSignatures);
992+
993+
const methodEventNames = ['viewItemList', 'addToCart', 'removeFromCart', 'beginCheckout', 'purchase', 'pageView', 'complexOperation'];
994+
methodEventNames.forEach(eventName => {
995+
const evt = methodEvents.find(e => e.eventName === eventName && e.source === 'custom');
996+
assert.ok(evt, `Expected to find method-as-event ${eventName}`);
997+
});
983998
});
984999

9851000
test('should resolve constants imported via path alias from tsconfig', () => {

tests/cli.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const customFunctionSignatures = [
2222
'this.props.customTrackFunction6(EVENT_NAME, PROPERTIES)',
2323
'customTrackFunction7(EVENT_NAME, PROPERTIES)',
2424
'getTrackingService().track(EVENT_NAME, PROPERTIES)',
25+
// Method-as-event signature
26+
'eventCalls.EVENT_NAME(PROPERTIES)',
2527
];
2628

2729
// Helper function to run CLI and capture output

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ events:
1111
line: 14
1212
function: trackGA4
1313
destination: googleanalytics
14+
- path: method-event.js
15+
line: 34
16+
function: global
17+
destination: custom
1418
properties:
1519
order_id:
1620
type: any
@@ -25,6 +29,32 @@ events:
2529
type: string
2630
state:
2731
type: string
32+
transaction_id:
33+
type: string
34+
value:
35+
type: number
36+
currency:
37+
type: string
38+
items:
39+
type: array
40+
items:
41+
type: object
42+
shipping:
43+
type: object
44+
properties:
45+
method:
46+
type: string
47+
cost:
48+
type: number
49+
address:
50+
type: object
51+
properties:
52+
city:
53+
type: string
54+
state:
55+
type: string
56+
zip:
57+
type: string
2858
newEvent:
2959
implementations:
3060
- path: main.js
@@ -379,3 +409,75 @@ events:
379409
type: string
380410
baz:
381411
type: string
412+
viewItemList:
413+
implementations:
414+
- path: method-event.js
415+
line: 4
416+
function: global
417+
destination: custom
418+
properties:
419+
items:
420+
type: array
421+
items:
422+
type: object
423+
item_list_id:
424+
type: string
425+
item_list_name:
426+
type: string
427+
addToCart:
428+
implementations:
429+
- path: method-event.js
430+
line: 11
431+
function: handleAddToCart
432+
destination: custom
433+
properties:
434+
items:
435+
type: array
436+
items:
437+
type: object
438+
value:
439+
type: number
440+
user:
441+
type: object
442+
properties:
443+
id:
444+
type: string
445+
email:
446+
type: string
447+
name:
448+
type: string
449+
removeFromCart:
450+
implementations:
451+
- path: method-event.js
452+
line: 18
453+
function: global
454+
destination: custom
455+
properties:
456+
items:
457+
type: array
458+
items:
459+
type: object
460+
value:
461+
type: number
462+
beginCheckout:
463+
implementations:
464+
- path: method-event.js
465+
line: 24
466+
function: checkoutHandler
467+
destination: custom
468+
properties:
469+
items:
470+
type: array
471+
items:
472+
type: object
473+
currency:
474+
type: string
475+
value:
476+
type: number
477+
pageView:
478+
implementations:
479+
- path: method-event.js
480+
line: 64
481+
function: global
482+
destination: custom
483+
properties: {}

0 commit comments

Comments
 (0)