telemetry-first-touch-store.test.ts60 lines · main
| 1 | import { afterEach, describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { |
| 4 | clearFirstTouchData, |
| 5 | getFirstTouchData, |
| 6 | setFirstTouchData, |
| 7 | } from './telemetry-first-touch-store' |
| 8 | |
| 9 | const makeFakeData = (pathname: string) => |
| 10 | ({ |
| 11 | page_url: `https://supabase.com${pathname}`, |
| 12 | pathname, |
| 13 | page_title: 'Test', |
| 14 | session_id: 'test-session', |
| 15 | ph: { referrer: 'https://google.com' }, |
| 16 | }) as ReturnType<typeof getFirstTouchData> & {} |
| 17 | |
| 18 | describe('telemetry-first-touch-store', () => { |
| 19 | // Reset between tests so module-scoped state doesn't leak |
| 20 | afterEach(() => { |
| 21 | clearFirstTouchData() |
| 22 | }) |
| 23 | |
| 24 | it('returns null before any write', () => { |
| 25 | expect(getFirstTouchData()).toBeNull() |
| 26 | }) |
| 27 | |
| 28 | it('stores data and returns it on read', () => { |
| 29 | const data = makeFakeData('/pricing') |
| 30 | setFirstTouchData(data) |
| 31 | expect(getFirstTouchData()).toEqual(data) |
| 32 | }) |
| 33 | |
| 34 | it('is write-once: a second call with different data is a no-op', () => { |
| 35 | const first = makeFakeData('/pricing') |
| 36 | const second = makeFakeData('/docs') |
| 37 | |
| 38 | setFirstTouchData(first) |
| 39 | setFirstTouchData(second) |
| 40 | |
| 41 | expect(getFirstTouchData()).toEqual(first) |
| 42 | }) |
| 43 | |
| 44 | it('clearFirstTouchData resets to null', () => { |
| 45 | setFirstTouchData(makeFakeData('/pricing')) |
| 46 | clearFirstTouchData() |
| 47 | expect(getFirstTouchData()).toBeNull() |
| 48 | }) |
| 49 | |
| 50 | it('allows writing again after clearFirstTouchData', () => { |
| 51 | const first = makeFakeData('/pricing') |
| 52 | const second = makeFakeData('/docs') |
| 53 | |
| 54 | setFirstTouchData(first) |
| 55 | clearFirstTouchData() |
| 56 | |
| 57 | setFirstTouchData(second) |
| 58 | expect(getFirstTouchData()).toEqual(second) |
| 59 | }) |
| 60 | }) |