Skip to content

Commit

Permalink
Add extra test for integrations object on ajs (#1221)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Feb 3, 2025
1 parent 4828c79 commit c9f81ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import { getGlobalAnalytics } from '../../lib/global-analytics-helper'
import { NullAnalytics } from '../../core/analytics'
import { recordIntegrationMetric } from '../../core/stats/metric-helpers'
import { waitForCondition } from '../../test-helpers/helpers'

let fetchCalls: ReturnType<typeof parseFetchCall>[] = []

Expand Down Expand Up @@ -386,6 +387,38 @@ describe('Initialization', () => {
describe('options.integrations permutations', () => {
const settings = { writeKey }

it('proxies integration options', async () => {
const analytics = AnalyticsBrowser.load(settings)
analytics.track(
'foo',
{},
{
integrations: {
Warehouses: {
warehouseIds: ['3dasf42dd'],
all: true,
},
},
}
)
await waitForCondition(() => {
return fetchCalls.some((el) => el.url.toString().includes('/v1/t'))
})
const trackCall = fetchCalls.find((el) =>
el.url.toString().includes('/v1/t')
)
expect(trackCall?.body.integrations).toMatchInlineSnapshot(`
{
"Warehouses": {
"all": true,
"warehouseIds": [
"3dasf42dd",
],
},
}
`)
})

it('does not load Segment.io if integrations.All is false and Segment.io is not listed', async () => {
const options: { integrations: { [key: string]: boolean } } = {
integrations: { All: false },
Expand Down
14 changes: 14 additions & 0 deletions packages/browser/src/test-helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { sleep } from '@segment/analytics-core'

export const waitForCondition = async (
condition: () => boolean,
timeout = 1000
): Promise<void> => {
const start = Date.now()
while (!condition()) {
if (Date.now() - start > timeout) {
throw new Error(`Timeout of ${timeout}ms exceeded!`)
}
await sleep(10)
}
}

0 comments on commit c9f81ac

Please sign in to comment.