Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oscb committed Aug 15, 2023
1 parent f03b2f2 commit db9da4f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/browser/src/browser/__tests__/cdn.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnalyticsBrowser } from '../..'
import { AnalyticsBrowser, getGlobalAnalytics } from '../..'
import unfetch from 'unfetch'
import { createSuccess } from '../../test-helpers/factories'
import { setGlobalCDNUrl } from '../../lib/parse-cdn'
Expand Down Expand Up @@ -45,5 +45,5 @@ it('if CDN is overridden, sets the overridden CDN global variable', async () =>
writeKey,
cdnURL: mockCdn,
})
expect(window.analytics._cdn).toBe(mockCdn)
expect(getGlobalAnalytics()?._cdn).toBe(mockCdn)
})
3 changes: 2 additions & 1 deletion packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
highEntropyTestData,
lowEntropyTestData,
} from '../../test-helpers/fixtures/client-hints'
import { getGlobalAnalytics } from '../..'

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

Expand Down Expand Up @@ -200,7 +201,7 @@ describe('Initialization', () => {
{
...xt,
load: async () => {
expect(window.analytics).toBeUndefined()
expect(getGlobalAnalytics()).toBeUndefined()
expect(getCDN()).toContain(overriddenCDNUrl)
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import jsdom, { JSDOM } from 'jsdom'
import { InitOptions } from '../../'
import { InitOptions, getGlobalAnalytics } from '../../'
import { AnalyticsBrowser, loadLegacySettings } from '../../browser'
import { snippet } from '../../tester/__fixtures__/segment-snippet'
import { install, AnalyticsStandalone } from '../standalone-analytics'
import { install } from '../standalone-analytics'
import unfetch from 'unfetch'
import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
import { sleep } from '../../lib/sleep'
import * as Factory from '../../test-helpers/factories'
import { EventQueue } from '../../core/queue/event-queue'
import { AnalyticsStandalone } from '../standalone-interface'

const track = jest.fn()
const identify = jest.fn()
Expand Down Expand Up @@ -142,7 +143,7 @@ describe('standalone bundle', () => {
.mockImplementation((): Promise<Response> => fetchSettings)
const mockCdn = 'http://my-overridden-cdn.com'

window.analytics._cdn = mockCdn
getGlobalAnalytics()!._cdn = mockCdn
await loadLegacySettings(segmentDotCom)

expect(unfetch).toHaveBeenCalledWith(expect.stringContaining(mockCdn))
Expand Down Expand Up @@ -262,7 +263,7 @@ describe('standalone bundle', () => {

// register is called after flushPreBuffer in `loadAnalytics`
register.mockImplementationOnce(() =>
window.analytics.track('race conditions', { foo: 'bar' })
getGlobalAnalytics()?.track('race conditions', { foo: 'bar' })
)

await install()
Expand Down
7 changes: 4 additions & 3 deletions packages/browser/src/browser/__tests__/standalone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LegacySettings } from '..'
import { pWhile } from '../../lib/p-while'
import { snippet } from '../../tester/__fixtures__/segment-snippet'
import * as Factory from '../../test-helpers/factories'
import { getGlobalAnalytics } from '../..'

const cdnResponse: LegacySettings = {
integrations: {
Expand Down Expand Up @@ -86,11 +87,11 @@ describe('standalone bundle', () => {
await import('../standalone')

await pWhile(
() => window.analytics?.initialized !== true,
() => getGlobalAnalytics()?.initialized !== true,
() => {}
)

expect(window.analytics).not.toBeUndefined()
expect(window.analytics.initialized).toBe(true)
expect(getGlobalAnalytics()).not.toBeUndefined()
expect(getGlobalAnalytics()?.initialized).toBe(true)
})
})
2 changes: 2 additions & 0 deletions packages/browser/src/lib/__tests__/parse-cdn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ it('should return the overridden cdn if window.analytics._cdn is mutated', () =>
withTag(`
<script src="https://cdn.segment.com/analytics.js/v1/gA5MBlJXrtZaB5sMMZvCF6czfBcfzNO6/analytics.min.js" />
`)
// @ts-ignore
;(window.analytics as any) = {
_cdn: 'http://foo.cdn.com',
}
Expand All @@ -59,6 +60,7 @@ it('should return the overridden cdn if window.analytics._cdn is mutated', () =>

it('if analytics is not loaded yet, should still return cdn', () => {
// is this an impossible state?
// @ts-ignore
window.analytics = undefined as any
withTag(`
<script src="https://cdn.segment.com/analytics.js/v1/gA5MBlJXrtZaB5sMMZvCF6czfBcfzNO6/analytics.min.js" />
Expand Down

0 comments on commit db9da4f

Please sign in to comment.