Bluesky app fork with some witchin' additions 馃挮
at main 79 lines 2.4 kB view raw
1jest.mock('#/storage', () => ({ 2 device: { 3 get: jest.fn(), 4 set: jest.fn(), 5 }, 6})) 7 8jest.mock('#/analytics/identifiers/util', () => ({ 9 isSessionIdExpired: jest.fn(), 10})) 11 12jest.mock('#/lib/appState', () => ({ 13 onAppStateChange: jest.fn(() => ({remove: jest.fn()})), 14})) 15 16beforeEach(() => { 17 jest.resetModules() 18 jest.clearAllMocks() 19}) 20 21function getMocks() { 22 const {device} = require('#/storage') 23 const {isSessionIdExpired} = require('#/analytics/identifiers/util') 24 return { 25 device: jest.mocked(device), 26 isSessionIdExpired: jest.mocked(isSessionIdExpired), 27 } 28} 29 30describe('session initialization', () => { 31 it('creates new session and sets timestamp when none exists', () => { 32 const {device, isSessionIdExpired} = getMocks() 33 device.get.mockReturnValue(undefined) 34 isSessionIdExpired.mockReturnValue(false) 35 36 const {getInitialSessionId} = require('./session') 37 const id = getInitialSessionId() 38 39 expect(id).toBeDefined() 40 expect(typeof id).toBe('string') 41 expect(device.set).toHaveBeenCalledWith(['nativeSessionId'], id) 42 expect(device.set).toHaveBeenCalledWith( 43 ['nativeSessionIdLastEventAt'], 44 expect.any(Number), 45 ) 46 }) 47 48 it('reuses existing session when not expired', () => { 49 const {device, isSessionIdExpired} = getMocks() 50 const existingId = 'existing-session-id' 51 device.get.mockImplementation((key: string[]) => { 52 if (key[0] === 'nativeSessionId') return existingId 53 if (key[0] === 'nativeSessionIdLastEventAt') return Date.now() 54 return undefined 55 }) 56 isSessionIdExpired.mockReturnValue(false) 57 58 const {getInitialSessionId} = require('./session') 59 60 expect(getInitialSessionId()).toBe(existingId) 61 }) 62 63 it('creates new session when existing is expired', () => { 64 const {device, isSessionIdExpired} = getMocks() 65 const existingId = 'existing-session-id' 66 device.get.mockImplementation((key: string[]) => { 67 if (key[0] === 'nativeSessionId') return existingId 68 if (key[0] === 'nativeSessionIdLastEventAt') return Date.now() - 999999 69 return undefined 70 }) 71 isSessionIdExpired.mockReturnValue(true) 72 73 const {getInitialSessionId} = require('./session') 74 const id = getInitialSessionId() 75 76 expect(id).not.toBe(existingId) 77 expect(device.set).toHaveBeenCalledWith(['nativeSessionId'], id) 78 }) 79})