telemetry-first-touch-store.test.ts 1.6 KB

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