Skip to content

Commit 153d8d6

Browse files
committed
add origin
1 parent 96130f2 commit 153d8d6

File tree

2 files changed

+40
-78
lines changed

2 files changed

+40
-78
lines changed

packages/signals/signals/src/core/processor/__tests__/sandbox-analytics-runtime.test.ts

Lines changed: 40 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ describe('AnalyticsRuntime', () => {
5050

5151
const calls = analyticsRuntime.getCalls()
5252
expect(calls.track).toHaveLength(1)
53-
expect(calls.track[0]).toEqual(['Event Name', undefined, {}])
53+
expect(calls.track[0]).toEqual([
54+
'Event Name',
55+
undefined,
56+
{ context: { __eventOrigin: { type: 'Signal' } } },
57+
])
5458
})
5559

5660
it('should handle multiple track calls', () => {
@@ -62,23 +66,6 @@ describe('AnalyticsRuntime', () => {
6266
expect(calls.track[0][0]).toBe('Event 1')
6367
expect(calls.track[1][0]).toBe('Event 2')
6468
})
65-
66-
it('should handle errors gracefully', () => {
67-
const consoleSpy = jest.spyOn(console, 'error')
68-
69-
// Mock createOptions to throw an error
70-
const originalCreateOptions = (analyticsRuntime as any).createOptions
71-
;(analyticsRuntime as any).createOptions = jest.fn(() => {
72-
throw new Error('Test error')
73-
})
74-
75-
analyticsRuntime.track('Event Name', {}, {})
76-
77-
expect(consoleSpy).toHaveBeenCalledWith(expect.any(Error))
78-
79-
// Restore original method
80-
;(analyticsRuntime as any).createOptions = originalCreateOptions
81-
})
8269
})
8370

8471
describe('identify method', () => {
@@ -103,7 +90,11 @@ describe('AnalyticsRuntime', () => {
10390

10491
const calls = analyticsRuntime.getCalls()
10592
expect(calls.identify).toHaveLength(1)
106-
expect(calls.identify[0]).toEqual([undefined, undefined, {}])
93+
expect(calls.identify[0]).toEqual([
94+
undefined,
95+
undefined,
96+
{ context: { __eventOrigin: { type: 'Signal' } } },
97+
])
10798
})
10899

109100
it('should handle errors gracefully', () => {
@@ -146,7 +137,11 @@ describe('AnalyticsRuntime', () => {
146137

147138
const calls = analyticsRuntime.getCalls()
148139
expect(calls.alias).toHaveLength(1)
149-
expect(calls.alias[0]).toEqual(['new-user-id', undefined, {}])
140+
expect(calls.alias[0]).toEqual([
141+
'new-user-id',
142+
undefined,
143+
{ context: { __eventOrigin: { type: 'Signal' } } },
144+
])
150145
})
151146

152147
it('should handle errors gracefully', () => {
@@ -189,7 +184,11 @@ describe('AnalyticsRuntime', () => {
189184

190185
const calls = analyticsRuntime.getCalls()
191186
expect(calls.group).toHaveLength(1)
192-
expect(calls.group[0]).toEqual([undefined, undefined, {}])
187+
expect(calls.group[0]).toEqual([
188+
undefined,
189+
undefined,
190+
{ context: { __eventOrigin: { type: 'Signal' } } },
191+
])
193192
})
194193

195194
it('should handle errors gracefully', () => {
@@ -241,22 +240,32 @@ describe('AnalyticsRuntime', () => {
241240
category,
242241
'', // name defaults to empty string
243242
properties,
244-
{},
243+
{ context: { __eventOrigin: { type: 'Signal' } } },
245244
])
246245
})
247246

248247
it('should preserve name when both name and category are provided', () => {
249248
analyticsRuntime.page('Page Name', 'Category', {}, undefined)
250249

251250
const calls = analyticsRuntime.getCalls()
252-
expect(calls.page[0][1]).toBe('Page Name')
251+
expect(calls.page[0]).toEqual([
252+
'Category',
253+
'Page Name',
254+
{},
255+
{ context: { __eventOrigin: { type: 'Signal' } } },
256+
])
253257
})
254258

255259
it('should preserve undefined name when category is also undefined', () => {
256260
analyticsRuntime.page(undefined, undefined, {}, undefined)
257261

258262
const calls = analyticsRuntime.getCalls()
259-
expect(calls.page[0][1]).toBeUndefined()
263+
expect(calls.page[0]).toEqual([
264+
undefined,
265+
undefined,
266+
{},
267+
{ context: { __eventOrigin: { type: 'Signal' } } },
268+
])
260269
})
261270

262271
it('should handle errors gracefully', () => {
@@ -308,15 +317,20 @@ describe('AnalyticsRuntime', () => {
308317
category,
309318
'', // name defaults to empty string
310319
properties,
311-
{},
320+
{ context: { __eventOrigin: { type: 'Signal' } } },
312321
])
313322
})
314323

315324
it('should preserve name when both name and category are provided', () => {
316325
analyticsRuntime.screen('Screen Name', 'Category', {}, undefined)
317326

318327
const calls = analyticsRuntime.getCalls()
319-
expect(calls.screen[0][1]).toBe('Screen Name')
328+
expect(calls.screen[0]).toEqual([
329+
'Category',
330+
'Screen Name',
331+
{},
332+
{ context: { __eventOrigin: { type: 'Signal' } } },
333+
])
320334
})
321335

322336
it('should handle errors gracefully', () => {
@@ -355,55 +369,6 @@ describe('AnalyticsRuntime', () => {
355369
})
356370
})
357371

358-
describe('createOptions method', () => {
359-
it('should return empty object when context is undefined', () => {
360-
const result = (analyticsRuntime as any).createOptions(undefined)
361-
expect(result).toEqual({})
362-
})
363-
364-
it('should return empty object when context is null', () => {
365-
const result = (analyticsRuntime as any).createOptions(null)
366-
expect(result).toEqual({})
367-
})
368-
369-
it('should add __eventOrigin to context', () => {
370-
const context = { userId: '123', custom: 'value' }
371-
const result = (analyticsRuntime as any).createOptions(context)
372-
373-
expect(result).toEqual({
374-
context: {
375-
userId: '123',
376-
custom: 'value',
377-
__eventOrigin: { type: 'Signal' },
378-
},
379-
})
380-
})
381-
382-
it('should preserve existing context properties', () => {
383-
const context = {
384-
ip: '127.0.0.1',
385-
userAgent: 'Chrome',
386-
nested: { prop: 'value' },
387-
}
388-
const result = (analyticsRuntime as any).createOptions(context)
389-
390-
expect(result.context.ip).toBe('127.0.0.1')
391-
expect(result.context.userAgent).toBe('Chrome')
392-
expect(result.context.nested).toEqual({ prop: 'value' })
393-
expect(result.context.__eventOrigin).toEqual({ type: 'Signal' })
394-
})
395-
396-
it('should not mutate the original context object', () => {
397-
const originalContext = { userId: '123' }
398-
const contextCopy = { ...originalContext }
399-
400-
;(analyticsRuntime as any).createOptions(originalContext)
401-
402-
expect(originalContext).toEqual(contextCopy)
403-
expect((originalContext as any).__eventOrigin).toBeUndefined()
404-
})
405-
})
406-
407372
describe('method calls isolation', () => {
408373
it('should maintain separate call arrays for each method', () => {
409374
analyticsRuntime.track('event', {}, {})

packages/signals/signals/src/core/processor/sandbox-analytics-runtime.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ export class AnalyticsRuntime implements AnalyticsRuntimePublicApi {
4343
* Stamp the context with the event origin to prevent infinite signal-event loops.
4444
*/
4545
private createOptions(context?: Record<string, any>): Record<string, any> {
46-
if (!context) {
47-
return {}
48-
}
4946
return {
5047
context: { ...context, __eventOrigin: { type: 'Signal' } },
5148
}

0 commit comments

Comments
 (0)