Skip to content

Commit 1673478

Browse files
ahnopologeticskarim
authored andcommitted
refactor: adding react typescript with custom function
1 parent fab00d2 commit 1673478

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

tests/analyzeTypeScript.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ test.describe('analyzeTsFile', () => {
488488
// Sort events by line number for consistent ordering
489489
events.sort((a, b) => a.line - b.line);
490490

491-
assert.strictEqual(events.length, 8);
491+
assert.strictEqual(events.length, 7);
492492

493493
// Test PostHog event
494494
const posthogEvent = events.find(e => e.source === 'posthog');
@@ -595,4 +595,20 @@ test.describe('analyzeTsFile', () => {
595595
checkout_step: { type: 'number' }
596596
});
597597
});
598+
599+
test('should correctly analyze React TypeScript file with custom function', () => {
600+
const reactFilePath = path.join(fixturesDir, 'typescript-react', 'main.tsx');
601+
const program = createProgram(reactFilePath);
602+
const events = analyzeTsFile(reactFilePath, program, 'tracker.track');
603+
604+
console.log({ events });
605+
const trackEvent = events.find(e => e.source === 'custom');
606+
607+
assert.strictEqual(trackEvent.eventName, 'cart_update');
608+
assert.strictEqual(trackEvent.functionName, 'trackCartUpdate');
609+
assert.strictEqual(trackEvent.line, 81);
610+
assert.deepStrictEqual(trackEvent.properties, {
611+
cart_size: { type: 'number' }
612+
});
613+
});
598614
});

tests/fixtures/typescript-react/main.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,10 @@ export const ShoppingCart: React.FC<CartProps> = ({ products }) => {
7878

7979
// Tracking with custom event builder
8080
const trackCartUpdate = () => {
81-
const event = buildStructEvent({
82-
category: 'Cart',
83-
action: 'Update',
84-
label: 'Cart Contents Modified',
85-
property: 'cart_size',
86-
value: cartItems.length
81+
tracker.track('cart_update', {
82+
cart_size: cartItems.length
8783
});
88-
tracker.track(event);
89-
};
84+
}
9085

9186
return (
9287
<div className="shopping-cart">
@@ -95,7 +90,10 @@ export const ShoppingCart: React.FC<CartProps> = ({ products }) => {
9590
{products.map(product => (
9691
<div key={product.id} className="product-item">
9792
<span>{product.name} - ${product.price}</span>
98-
<button onClick={() => handleAddToCart(product)}>Add to Cart</button>
93+
<button onClick={() => {
94+
handleAddToCart(product);
95+
trackCartUpdate();
96+
}}>Add to Cart</button>
9997
</div>
10098
))}
10199

0 commit comments

Comments
 (0)