import { generateValueFor, getCookieDomain, getExpirationFor, VISITOR_ID, SESSION_STAMP, DEVICE_ID, } from './storage' import { addMonthToDate } from './helpers' jest.mock('./helpers', () => ({ addMonthToDate: jest.fn() })) describe('Storage', () => { describe('generateValueFor', () => { it('generate the visitor ID', () => { expect(typeof generateValueFor(VISITOR_ID)).toBe('string') expect(generateValueFor(VISITOR_ID).length).toBe(24) }) it('generate the session stamp', () => { expect(typeof generateValueFor(SESSION_STAMP)).toBe('number') }) it('generate the device id', () => { expect(generateValueFor(DEVICE_ID).startsWith('w_')).toBe(true) }) it('generate a uuid as a fallback if the key doesnt exist', () => { expect(generateValueFor('')).toBeDefined() }) }) describe('getExpirationFor', () => { beforeEach(() => { ;(addMonthToDate as jest.Mock).mockClear() }) it('get the expiration dateof the visitor ID', () => { getExpirationFor(VISITOR_ID) expect(addMonthToDate).toHaveBeenCalledWith(13) }) it('get the expiration date of the device id', () => { getExpirationFor(DEVICE_ID) expect(addMonthToDate).toHaveBeenCalledWith(1) }) it('get the default expiration date', () => { getExpirationFor('') expect(addMonthToDate).not.toHaveBeenCalled() }) }) describe('getCookieDomain', () => { it('Should get the domain', () => { expect(getCookieDomain('localhost')).toBe('localhost') expect(getCookieDomain('127.0.0.1')).toBe('localhost') expect(getCookieDomain('192.168.10.10')).toBe('localhost') expect(getCookieDomain('m.domain.com')).toBe('.domain.com') expect(getCookieDomain('www.domain.com')).toBe('.domain.com') expect(getCookieDomain('www.rs.domain.gg')).toBe('.domain.gg') expect(getCookieDomain('www.domain.co.uk')).toBe('.domain.co.uk') expect(getCookieDomain('web-preprod-fr-fr--v3-s22.pp.blbl.cr')).toBe('.blbl.cr') }) }) })