|
| 1 | +/* RouteTracker.test.tsx — unit tests for the New Relic page-view tracker. |
| 2 | + * |
| 3 | + * Covers: |
| 4 | + * 1. Calls setPageViewName(pathname) on initial mount. |
| 5 | + * 2. Calls setCustomAttribute for tier, is_admin, commit_id on mount. |
| 6 | + * 3. Re-fires setPageViewName + attributes when the route changes |
| 7 | + * (this is the SPA-soft-nav case the agent's pro_plus_spa mode is |
| 8 | + * designed to capture). |
| 9 | + * 4. Does NOT crash when window.newrelic is absent (fail-open). |
| 10 | + * 5. Falls back to "anonymous" / false when ctx.me is null |
| 11 | + * (pre-auth marketing browse). |
| 12 | + * 6. Reflects tier upgrades — re-stamps the new tier when ctx.me.team.tier |
| 13 | + * changes (the upgrade-webhook path). |
| 14 | + * |
| 15 | + * useDashboardCtx is mocked module-level so we can vary `me` per test |
| 16 | + * without touching the real subscription store. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
| 20 | +import { render } from '@testing-library/react' |
| 21 | +import { MemoryRouter, Routes, Route, useNavigate } from 'react-router-dom' |
| 22 | +import { useEffect } from 'react' |
| 23 | + |
| 24 | +// ─── Mock useDashboardCtx ──────────────────────────────────────────────── |
| 25 | +// Mutated per-test to flip tier / admin flag / null-me. |
| 26 | +let mockMe: { |
| 27 | + user?: { id: string; email: string } |
| 28 | + team?: { tier: string } |
| 29 | + is_platform_admin?: boolean |
| 30 | +} | null = null |
| 31 | + |
| 32 | +vi.mock('../hooks/useDashboardCtx', () => ({ |
| 33 | + useDashboardCtx: () => ({ |
| 34 | + me: mockMe, |
| 35 | + meErr: null, |
| 36 | + meLoading: false, |
| 37 | + env: 'production', |
| 38 | + envs: ['production'], |
| 39 | + counts: { resources: 0, deployments: 0, vault: 0, team: 1 }, |
| 40 | + resources: [], |
| 41 | + billing: null, |
| 42 | + billingLoading: false, |
| 43 | + }), |
| 44 | +})) |
| 45 | + |
| 46 | +// Imported after the mock so the module under test resolves the stubbed hook. |
| 47 | +import { RouteTracker } from './RouteTracker' |
| 48 | + |
| 49 | +type NRStub = { |
| 50 | + setPageViewName: ReturnType<typeof vi.fn> |
| 51 | + setCustomAttribute: ReturnType<typeof vi.fn> |
| 52 | +} |
| 53 | + |
| 54 | +function installNewrelicStub(): NRStub { |
| 55 | + const stub: NRStub = { |
| 56 | + setPageViewName: vi.fn(), |
| 57 | + setCustomAttribute: vi.fn(), |
| 58 | + } |
| 59 | + ;(window as unknown as { newrelic: NRStub }).newrelic = stub |
| 60 | + return stub |
| 61 | +} |
| 62 | + |
| 63 | +describe('RouteTracker', () => { |
| 64 | + let originalNewrelic: unknown |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + originalNewrelic = (window as unknown as { newrelic?: unknown }).newrelic |
| 68 | + mockMe = null |
| 69 | + }) |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + if (originalNewrelic === undefined) { |
| 73 | + delete (window as unknown as { newrelic?: unknown }).newrelic |
| 74 | + } else { |
| 75 | + ;(window as unknown as { newrelic?: unknown }).newrelic = originalNewrelic |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + it('calls setPageViewName with the initial pathname', () => { |
| 80 | + const nr = installNewrelicStub() |
| 81 | + render( |
| 82 | + <MemoryRouter initialEntries={['/app/resources']}> |
| 83 | + <RouteTracker /> |
| 84 | + </MemoryRouter>, |
| 85 | + ) |
| 86 | + expect(nr.setPageViewName).toHaveBeenCalledWith('/app/resources') |
| 87 | + }) |
| 88 | + |
| 89 | + it('stamps tier / is_admin / commit_id custom attributes', () => { |
| 90 | + mockMe = { |
| 91 | + user: { id: 'u1', email: 'a@b.test' }, |
| 92 | + team: { tier: 'pro' }, |
| 93 | + is_platform_admin: true, |
| 94 | + } |
| 95 | + const nr = installNewrelicStub() |
| 96 | + render( |
| 97 | + <MemoryRouter initialEntries={['/app']}> |
| 98 | + <RouteTracker /> |
| 99 | + </MemoryRouter>, |
| 100 | + ) |
| 101 | + // The three custom attributes we promise to stamp on every page view. |
| 102 | + expect(nr.setCustomAttribute).toHaveBeenCalledWith('tier', 'pro') |
| 103 | + expect(nr.setCustomAttribute).toHaveBeenCalledWith('is_admin', true) |
| 104 | + // commit_id is sourced from VITE_COMMIT_ID; in test it's "dev". |
| 105 | + const commitCalls = nr.setCustomAttribute.mock.calls.filter((c) => c[0] === 'commit_id') |
| 106 | + expect(commitCalls.length).toBeGreaterThanOrEqual(1) |
| 107 | + expect(typeof commitCalls[0][1]).toBe('string') |
| 108 | + expect((commitCalls[0][1] as string).length).toBeGreaterThan(0) |
| 109 | + }) |
| 110 | + |
| 111 | + it('falls back to anonymous tier and is_admin=false when ctx.me is null', () => { |
| 112 | + mockMe = null // unauthenticated |
| 113 | + const nr = installNewrelicStub() |
| 114 | + render( |
| 115 | + <MemoryRouter initialEntries={['/pricing']}> |
| 116 | + <RouteTracker /> |
| 117 | + </MemoryRouter>, |
| 118 | + ) |
| 119 | + expect(nr.setCustomAttribute).toHaveBeenCalledWith('tier', 'anonymous') |
| 120 | + expect(nr.setCustomAttribute).toHaveBeenCalledWith('is_admin', false) |
| 121 | + }) |
| 122 | + |
| 123 | + it('re-fires setPageViewName when the route changes (SPA soft nav)', () => { |
| 124 | + const nr = installNewrelicStub() |
| 125 | + |
| 126 | + // Helper inside the router that triggers navigation post-mount. |
| 127 | + function Nav() { |
| 128 | + const navigate = useNavigate() |
| 129 | + useEffect(() => { |
| 130 | + navigate('/app/billing') |
| 131 | + }, [navigate]) |
| 132 | + return null |
| 133 | + } |
| 134 | + |
| 135 | + render( |
| 136 | + <MemoryRouter initialEntries={['/app/resources']}> |
| 137 | + <RouteTracker /> |
| 138 | + <Routes> |
| 139 | + <Route path="/app/resources" element={<Nav />} /> |
| 140 | + <Route path="/app/billing" element={<div>billing</div>} /> |
| 141 | + </Routes> |
| 142 | + </MemoryRouter>, |
| 143 | + ) |
| 144 | + |
| 145 | + // First mount: /app/resources. Then the Nav effect pushes /app/billing. |
| 146 | + // setPageViewName must have been called for BOTH pathnames. |
| 147 | + const names = nr.setPageViewName.mock.calls.map((c) => c[0]) |
| 148 | + expect(names).toContain('/app/resources') |
| 149 | + expect(names).toContain('/app/billing') |
| 150 | + }) |
| 151 | + |
| 152 | + it('does not crash when window.newrelic is absent (fail-open)', () => { |
| 153 | + delete (window as unknown as { newrelic?: unknown }).newrelic |
| 154 | + expect(() => |
| 155 | + render( |
| 156 | + <MemoryRouter initialEntries={['/login']}> |
| 157 | + <RouteTracker /> |
| 158 | + </MemoryRouter>, |
| 159 | + ), |
| 160 | + ).not.toThrow() |
| 161 | + }) |
| 162 | + |
| 163 | + it('stamps the new tier when team.tier changes (upgrade webhook path)', () => { |
| 164 | + // First render: hobby |
| 165 | + mockMe = { user: { id: 'u', email: 'x@y' }, team: { tier: 'hobby' } } |
| 166 | + const nr = installNewrelicStub() |
| 167 | + const { rerender } = render( |
| 168 | + <MemoryRouter initialEntries={['/app']}> |
| 169 | + <RouteTracker /> |
| 170 | + </MemoryRouter>, |
| 171 | + ) |
| 172 | + expect(nr.setCustomAttribute).toHaveBeenCalledWith('tier', 'hobby') |
| 173 | + |
| 174 | + // Simulate the upgrade webhook flipping the ctx state. |
| 175 | + nr.setCustomAttribute.mockClear() |
| 176 | + mockMe = { user: { id: 'u', email: 'x@y' }, team: { tier: 'pro' } } |
| 177 | + rerender( |
| 178 | + <MemoryRouter initialEntries={['/app']}> |
| 179 | + <RouteTracker /> |
| 180 | + </MemoryRouter>, |
| 181 | + ) |
| 182 | + expect(nr.setCustomAttribute).toHaveBeenCalledWith('tier', 'pro') |
| 183 | + }) |
| 184 | + |
| 185 | + it('renders no markup (null)', () => { |
| 186 | + const { container } = render( |
| 187 | + <MemoryRouter> |
| 188 | + <RouteTracker /> |
| 189 | + </MemoryRouter>, |
| 190 | + ) |
| 191 | + // The component returns null; nothing should be appended. |
| 192 | + expect(container.firstChild).toBeNull() |
| 193 | + }) |
| 194 | +}) |
0 commit comments