/** * @jest-environment jsdom */ const ogWarn = console.warn import * as KoalaSDK from '../browser' // Prevent API calls from being made jest.mock('../api/collect', () => ({ collectPages: jest.fn(() => Promise.resolve(undefined)), qualify: jest.fn(() => Promise.resolve({ qualification: {} })) })) describe('auto-installer', () => { beforeEach(() => { window.fetch = jest.fn() console.warn = jest.fn() window.ko?.removeListeners?.() // Pseudo-snippet injected into the browser. // @ts-expect-error dynamic code window.ko = [] ;['identify', 'track', 'removeListeners', 'on', 'off', 'ready', 'qualify'].forEach((method) => { // @ts-expect-error dynamic code ko[method] = function () { const args = Array.from(arguments) args.unshift(method) // @ts-expect-error dynamic code ko.push(args) // @ts-expect-error dynamic code return ko } }) }) afterEach(() => { console.warn = ogWarn }) // TODO actually test the functions in `browser-install.ts` test('should let you use event listeners before loading the SDK', (done) => { expect.assertions(1) ;(window.fetch as jest.Mock).mockResolvedValue({ ok: true, json: () => ({ sdk_settings: {} }) }) const spy = jest.fn() window.ko?.ready(spy) window.ko?.on('profile-update', spy) KoalaSDK.load({ project: 'test' }) setTimeout(() => { window.ko?.emit('profile-update') expect(spy).toHaveBeenCalledTimes(2) done() }, 100) }) })