Skip to content

Commit ef47e9e

Browse files
committed
Remove direct inspector wirings
The wirings have been moved to segment-inspector/packages/webext/src/broker/index.ts
1 parent b07a26a commit ef47e9e

File tree

6 files changed

+50
-666
lines changed

6 files changed

+50
-666
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
Remove direct wirings for Segment Inspector

packages/browser/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"@internal/config": "0.0.0",
6464
"@segment/analytics.js-integration": "^3.3.3",
6565
"@segment/analytics.js-integration-amplitude": "^3.3.3",
66-
"@segment/inspector-webext": "^2.0.3",
6766
"@size-limit/preset-big-lib": "^7.0.8",
6867
"@types/flat": "^5.0.1",
6968
"@types/fs-extra": "^9.0.2",

packages/browser/src/browser/__tests__/inspector.integration.test.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,52 @@ jest
1010
const writeKey = 'foo'
1111

1212
describe('Inspector', () => {
13-
const triggeredSpy = jest.fn()
14-
const attachedSpy = jest.fn()
15-
const deliveredSpy = jest.fn()
1613
beforeEach(() => {
17-
Object.assign((window.__SEGMENT_INSPECTOR__ ??= {}), {
18-
triggered: triggeredSpy,
19-
attach: attachedSpy,
20-
delivered: deliveredSpy,
14+
Object.assign(((window as any)['__SEGMENT_INSPECTOR__'] ??= {}), {
15+
attach: jest.fn(),
2116
})
2217
})
18+
2319
it('attaches to inspector', async () => {
24-
await AnalyticsBrowser.load({
20+
const [analytics] = await AnalyticsBrowser.load({
2521
writeKey,
2622
})
27-
expect(attachedSpy).toBeCalledTimes(1)
23+
24+
expect(
25+
((window as any)['__SEGMENT_INSPECTOR__'] as any).attach
26+
).toHaveBeenCalledWith(analytics)
2827
})
2928

30-
it('calls triggered and delivered when an event is sent', async () => {
29+
it('emits essential message lifecycle events', async () => {
3130
const [analytics] = await AnalyticsBrowser.load({
3231
writeKey,
3332
})
34-
expect(attachedSpy).toBeCalledTimes(1)
35-
expect(triggeredSpy).toBeCalledTimes(0)
36-
expect(deliveredSpy).toBeCalledTimes(0)
3733

38-
await analytics.track('foo', {})
34+
const triggeredFn = jest.fn()
35+
const enrichedFn = jest.fn()
36+
const deliveredFn = jest.fn()
37+
38+
analytics.on('dispatch_start', triggeredFn)
39+
analytics.queue.on('message_enriched', enrichedFn)
40+
analytics.queue.on('message_delivered', deliveredFn)
41+
42+
const deliveryPromise = analytics.track('Test event').catch(() => {})
43+
44+
expect(triggeredFn).toHaveBeenCalledTimes(1)
45+
46+
expect(triggeredFn).toHaveBeenCalledWith(
47+
expect.objectContaining({
48+
id: expect.any(String),
49+
event: expect.objectContaining({
50+
event: 'Test event',
51+
type: 'track',
52+
}),
53+
})
54+
)
55+
56+
await deliveryPromise
3957

40-
expect(triggeredSpy.mock.lastCall[0].event.type).toBe('track')
41-
expect(triggeredSpy).toBeCalledTimes(1)
42-
expect(deliveredSpy).toBeCalledTimes(1)
58+
expect(enrichedFn).toHaveBeenCalledTimes(1)
59+
expect(deliveredFn).toHaveBeenCalledTimes(1)
4360
})
4461
})

packages/browser/src/core/inspector/__tests__/index.test.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
1-
import type { InspectBroker } from '@segment/inspector-webext'
21
import { getGlobal } from '../../lib/get-global'
32
import type { Analytics } from '../analytics'
43

5-
declare global {
6-
interface Window {
7-
__SEGMENT_INSPECTOR__: Partial<InspectBroker>
8-
}
9-
}
10-
114
const env = getGlobal()
125

136
// The code below assumes the inspector extension will use Object.assign
147
// to add the inspect interface on to this object reference (unless the
158
// extension code ran first and has already set up the variable)
16-
const inspectorHost: Partial<InspectBroker> = ((env as any)[
17-
'__SEGMENT_INSPECTOR__'
18-
] ??= {})
9+
const inspectorHost: {
10+
attach: (analytics: Analytics) => void
11+
} = ((env as any)['__SEGMENT_INSPECTOR__'] ??= {})
1912

20-
export const attachInspector = (analytics: Analytics) => {
13+
export const attachInspector = (analytics: Analytics) =>
2114
inspectorHost.attach?.(analytics as any)
22-
23-
analytics.on('dispatch_start', (ctx) => inspectorHost.triggered?.(ctx as any))
24-
25-
analytics.queue.on('message_enriched', (ctx) =>
26-
inspectorHost.enriched?.(ctx as any)
27-
)
28-
29-
analytics.queue.on('message_delivered', (ctx) =>
30-
// FIXME: Resolve browsers destinations that the event was sent to
31-
inspectorHost.delivered?.(ctx as any, ['segment.io'])
32-
)
33-
}

0 commit comments

Comments
 (0)