/** * @jest-environment jsdom */ import * as KoalaSDK from '../browser' const ogWarn = console.warn describe(KoalaSDK.load, () => { beforeEach(() => { window.fetch = jest.fn() console.warn = jest.fn() ;(window.fetch as jest.Mock).mockResolvedValue({ ok: true, json: () => ({ sdk_settings: { authorized_referrers: ['somedomain.com'], geo_allowed: false } }) }) // Pseudo-snippet injected into the browser. // @ts-expect-error dynamic code window.ko = [] }) afterEach(() => { console.warn = ogWarn }) test('should not load if host its not authorized on the settings', async () => { await KoalaSDK.load({ project: 'test' }) expect(console.warn).toHaveBeenCalledWith('[KOALA]', 'Current domain not allowed to load the SDK') }) test('should not emit anything to pagetracker if geo is not allowed on the settings', async () => { const sdk = await KoalaSDK.load({ project: 'test' }) // @ts-ignore private method sdk.pageTracker.emit('page', [ { page: { path: '/pricing' } } ]) expect(sdk?.edge.page.seen('/pricing')).toBe(false) }) test('should warn if you already loaded the SDK', async () => { ;(window.fetch as jest.Mock).mockResolvedValue({ ok: true, json: () => ({ sdk_settings: {} }) }) await KoalaSDK.load({ project: 'test' }) await KoalaSDK.load({ project: 'test' }) expect(console.warn).toHaveBeenCalledWith( '[KOALA]', 'The Koala SDK is already loaded. Calling `load` again will have no effect.' ) }) test('should return the original instance if already loaded', async () => { ;(window.fetch as jest.Mock).mockResolvedValue({ ok: true, json: () => ({ sdk_settings: {} }) }) const ko1 = await KoalaSDK.load({ project: 'test' }) const ko2 = await KoalaSDK.load({ project: 'test' }) expect(ko2).toBe(ko1) }) test('should accept incoming valid profileIds', async () => { ;(window.fetch as jest.Mock).mockResolvedValue({ ok: true, json: () => ({ sdk_settings: {} }) }) const profileId = 'd29d5054-349f-45be-9533-69f3240cce3a' const options = { project: 'test', profileId: profileId } const ko = await KoalaSDK.load(options) expect(ko?.user?.id()).toBe(profileId) }) })