Skip to content

Commit

Permalink
Remove direct inspector wirings
Browse files Browse the repository at this point in the history
The wirings have been moved to
segment-inspector/packages/webext/src/broker/index.ts
  • Loading branch information
zikaari committed Jan 10, 2023
1 parent 521be41 commit 5e4a609
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 666 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-kangaroos-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Remove direct wirings for Segment Inspector
1 change: 0 additions & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"@internal/config": "0.0.0",
"@segment/analytics.js-integration": "^3.3.3",
"@segment/analytics.js-integration-amplitude": "^3.3.3",
"@segment/inspector-webext": "^2.0.3",
"@size-limit/preset-big-lib": "^7.0.8",
"@types/flat": "^5.0.1",
"@types/fs-extra": "^9.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,52 @@ jest
const writeKey = 'foo'

describe('Inspector', () => {
const triggeredSpy = jest.fn()
const attachedSpy = jest.fn()
const deliveredSpy = jest.fn()
beforeEach(() => {
Object.assign((window.__SEGMENT_INSPECTOR__ ??= {}), {
triggered: triggeredSpy,
attach: attachedSpy,
delivered: deliveredSpy,
Object.assign(((window as any)['__SEGMENT_INSPECTOR__'] ??= {}), {
attach: jest.fn(),
})
})

it('attaches to inspector', async () => {
await AnalyticsBrowser.load({
const [analytics] = await AnalyticsBrowser.load({
writeKey,
})
expect(attachedSpy).toBeCalledTimes(1)

expect(
((window as any)['__SEGMENT_INSPECTOR__'] as any).attach
).toHaveBeenCalledWith(analytics)
})

it('calls triggered and delivered when an event is sent', async () => {
it('emits essential message lifecycle events', async () => {
const [analytics] = await AnalyticsBrowser.load({
writeKey,
})
expect(attachedSpy).toBeCalledTimes(1)
expect(triggeredSpy).toBeCalledTimes(0)
expect(deliveredSpy).toBeCalledTimes(0)

await analytics.track('foo', {})
const triggeredFn = jest.fn()
const enrichedFn = jest.fn()
const deliveredFn = jest.fn()

analytics.on('dispatch_start', triggeredFn)
analytics.queue.on('message_enriched', enrichedFn)
analytics.queue.on('message_delivered', deliveredFn)

const deliveryPromise = analytics.track('Test event').catch(() => {})

expect(triggeredFn).toHaveBeenCalledTimes(1)

expect(triggeredFn).toHaveBeenCalledWith(
expect.objectContaining({
id: expect.any(String),
event: expect.objectContaining({
event: 'Test event',
type: 'track',
}),
})
)

await deliveryPromise

expect(triggeredSpy.mock.lastCall[0].event.type).toBe('track')
expect(triggeredSpy).toBeCalledTimes(1)
expect(deliveredSpy).toBeCalledTimes(1)
expect(enrichedFn).toHaveBeenCalledTimes(1)
expect(deliveredFn).toHaveBeenCalledTimes(1)
})
})
40 changes: 0 additions & 40 deletions packages/browser/src/core/inspector/__tests__/index.test.ts

This file was deleted.

27 changes: 4 additions & 23 deletions packages/browser/src/core/inspector/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
import type { InspectBroker } from '@segment/inspector-webext'
import { getGlobal } from '../../lib/get-global'
import type { Analytics } from '../analytics'

declare global {
interface Window {
__SEGMENT_INSPECTOR__: Partial<InspectBroker>
}
}

const env = getGlobal()

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

export const attachInspector = (analytics: Analytics) => {
export const attachInspector = (analytics: Analytics) =>
inspectorHost.attach?.(analytics as any)

analytics.on('dispatch_start', (ctx) => inspectorHost.triggered?.(ctx as any))

analytics.queue.on('message_enriched', (ctx) =>
inspectorHost.enriched?.(ctx as any)
)

analytics.queue.on('message_delivered', (ctx) =>
// FIXME: Resolve browsers destinations that the event was sent to
inspectorHost.delivered?.(ctx as any, ['segment.io'])
)
}
Loading

0 comments on commit 5e4a609

Please sign in to comment.