Files
digital-psychology/apps/user-h5/src/lib/analytics.spec.ts
T
jackyu66gitandCursor bd22d9dddd feat: P1 合盘/星座/问答与测测完整设计包及模拟器取证工具
落地 synastry/star/ask API 与 H5 页面,补齐 cece-frontend-re complete-design 证据文档,并加入 Android 模拟器截图抓取脚本。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-03 11:37:53 +08:00

35 lines
1.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { AnalyticsEvent, track, trackPageView } from './analytics'
describe('analytics.track', () => {
beforeEach(() => {
window.gtag = vi.fn()
})
afterEach(() => {
delete window.gtag
})
it('calls gtag with event name and params', () => {
track(AnalyticsEvent.HomeCtaPortrait)
expect(window.gtag).toHaveBeenCalledWith('event', 'home_cta_portrait', {})
})
it('omits undefined params', () => {
track(AnalyticsEvent.PortraitCompleted, { source: 'query', report_id: undefined })
expect(window.gtag).toHaveBeenCalledWith('event', 'portrait_completed', { source: 'query' })
})
it('does not throw when gtag missing', () => {
delete window.gtag
expect(() => track(AnalyticsEvent.RelationCompleted)).not.toThrow()
})
it('trackPageView sends page_path', () => {
trackPageView('/portrait', '个人画像')
expect(window.gtag).toHaveBeenCalledWith('event', 'page_view', {
page_path: '/portrait',
page_title: '个人画像',
})
})
})