/** * @jest-environment jsdom */ import Cookies from 'js-cookie' import { session, SessionOptions } from '../session' jest.useFakeTimers() describe('session', () => { beforeEach(() => { jest.clearAllTimers() window.localStorage.clear() Cookies.remove('ko_sid') }) test('init: starts a fresh session', () => { const now = new Date().getTime() expect(session.sessionId()).toBeUndefined() session.init({} as SessionOptions) const existing = session.sessionId() expect(existing).not.toBeUndefined() expect(existing?.id).toEqual(now.toString()) expect(existing?.lastTouched).toEqual(now) }) test('stores session ids in cookies and local storage', () => { const sesh = session.fetch({} as SessionOptions) expect(Cookies.get('ko_sid')).toEqual(JSON.stringify(sesh)) expect(window.localStorage.getItem('ko_sid')).toEqual(JSON.stringify(sesh)) }) test('stores session ids in cookies and local storage based on domain options', () => { const sesh = session.fetch({ cookie_defaults: { domain: 'koala.live' } } as SessionOptions) // this is not the same domain as we're in, so the cookie should not be set expect(Cookies.get('ko_sid')).toBeUndefined() // local storage is unaffected expect(window.localStorage.getItem('ko_sid')).toEqual(JSON.stringify(sesh)) }) test('fetch creates a new session if does not exist', () => { const now = new Date().getTime() expect(session.sessionId()).toBeUndefined() const sesh = session.fetch({} as SessionOptions) expect(sesh).not.toBeUndefined() expect(sesh?.id).toEqual(now.toString()) expect(sesh?.lastTouched).toEqual(now) }) test('expires session after 24h', () => { const yesterday = new Date().getTime() - 1000 * 60 * 60 * 25 const aFewMinutesAgo = new Date().getTime() - 1000 * 60 * 5 const now = new Date().getTime() Cookies.set('ko_sid', JSON.stringify({ id: yesterday.toString(), lastTouched: aFewMinutesAgo })) expect(session.sessionId()).not.toBeUndefined() const sesh = session.fetch({} as SessionOptions) expect(sesh.id).toEqual(now.toString()) expect(sesh.lastTouched).toEqual(now) }) test('does not renew session on access if valid', () => { const anHourAgo = new Date().getTime() - 1000 * 60 * 60 * 1 const fiveMinutesAgo = new Date().getTime() - 1000 * 60 * 5 const now = new Date().getTime() Cookies.set('ko_sid', JSON.stringify({ id: anHourAgo.toString(), lastTouched: fiveMinutesAgo })) expect(session.sessionId()).not.toBeUndefined() const sesh = session.fetch({} as SessionOptions) expect(sesh.id).toEqual(anHourAgo.toString()) expect(sesh.lastTouched).toEqual(now) }) test('renews stale sessions on access', () => { const anHourAgo = new Date().getTime() - 1000 * 60 * 60 * 1 const thirtyFiveMinutesAgo = new Date().getTime() - 1000 * 60 * 35 const now = new Date().getTime() Cookies.set('ko_sid', JSON.stringify({ id: anHourAgo.toString(), lastTouched: thirtyFiveMinutesAgo })) expect(session.sessionId()).not.toBeUndefined() const sesh = session.fetch({} as SessionOptions) expect(sesh.id).toEqual(now.toString()) expect(sesh.lastTouched).toEqual(now) }) test('touches the session every time it is fetched', () => { const then = new Date().getTime() - 10_000 Cookies.set('ko_sid', JSON.stringify({ id: then.toString(), lastTouched: then })) const now = new Date().getTime() const sesh = session.fetch({} as SessionOptions) expect(sesh.id).toEqual(then.toString()) expect(sesh.lastTouched).toEqual(now) }) })