import Tracktor from './index' import { VISITOR_ID, SESSION_STAMP, DEVICE_ID } from './storage' const userConfig = { hostname: 'www.domain.com', userAgent: 'jest-unit-tester', } const storage = { list: {}, get: key => storage.list[key], set: (key, name, options) => { storage.list[key] = name }, } const visitorId = 'visitorId' const sessionStamp = 12345 const deviceId = 'w_deviceId' describe('Tracktor', () => { describe('eventBuffer not configured', () => { it('Should throw an error if the eventBuffer is not initialized', () => { expect(() => new Tracktor(userConfig, storage)).toThrow() }) }) describe('eventBuffer configured', () => { beforeAll(() => { Tracktor.initializeEventBuffer({ sendUrl: 'url', appName: 'tracktor-UT' }) }) it('Should be able to initialize Tracktor with the userAgent & generate visitorId, sessionStamp & deviceId', () => { const tracktor = new Tracktor(userConfig, storage) const config = tracktor.userConfig expect(config).toEqual(userConfig) expect(config.deviceId.startsWith('w_')).toBe(true) expect(config.sessionStamp).toBeDefined() expect(typeof config.sessionStamp).toBe('number') expect(config.visitorId).toBeDefined() }) it('Should be able to provide the visitorId during the initialization', () => { const tracktor = new Tracktor({ ...userConfig, visitorId }, storage) expect(tracktor.userConfig.visitorId).toBe(visitorId) }) it('Should be able to provide the sessionStamp during the initialization', () => { const tracktor = new Tracktor({ ...userConfig, sessionStamp }, storage) expect(tracktor.userConfig.sessionStamp).toBe(sessionStamp) }) it('Should be able to provide the deviceId during the initialization', () => { const tracktor = new Tracktor({ ...userConfig, deviceId }, storage) expect(tracktor.userConfig.deviceId).toBe(deviceId) }) it('Should get visitorId, sessionStamp & deviceId from the storage', () => { storage.list = { [VISITOR_ID]: visitorId, [SESSION_STAMP]: sessionStamp, [DEVICE_ID]: deviceId, } const tracktor = new Tracktor(userConfig, storage) const config = tracktor.userConfig expect(config.deviceId).toBe(storage.list[DEVICE_ID]) expect(config.sessionStamp).toBe(storage.list[SESSION_STAMP]) expect(config.visitorId).toBe(storage.list[VISITOR_ID]) }) it('Should be able to update userId config', () => { const userId = 'user-id' const tracktor = new Tracktor(userConfig, storage) expect(tracktor.userConfig.userId).toBeUndefined() tracktor.updateUserId(userId) expect(tracktor.userConfig.userId).toBe(userId) tracktor.updateUserId(undefined) expect(tracktor.userConfig.userId).toBeUndefined() }) }) })