|
| 1 | +/* UpgradePromptCard.test.tsx — unit tests for the in-context upgrade card (U2). |
| 2 | + * |
| 3 | + * Two responsibilities, tested separately: |
| 4 | + * 1. Per-feature copy (title + body) — the surface-specific framing |
| 5 | + * 2. CTA label resolution — P1 experiment variant from /auth/me, with |
| 6 | + * fallbacks to per-feature override and DEFAULT_UPGRADE_CTA |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, afterEach, vi } from 'vitest' |
| 10 | +import { render, screen, cleanup } from '@testing-library/react' |
| 11 | +import type { ReactNode } from 'react' |
| 12 | + |
| 13 | +// Module-level mock for useDashboardCtx so each test controls what /auth/me |
| 14 | +// returns. Mutable holder so the suite can flip variants between cases. |
| 15 | +let mockMe: any = null |
| 16 | +vi.mock('../hooks/useDashboardCtx', () => ({ |
| 17 | + useDashboardCtx: () => ({ |
| 18 | + me: mockMe, |
| 19 | + meErr: null, |
| 20 | + meLoading: false, |
| 21 | + env: 'production', |
| 22 | + envs: ['production'], |
| 23 | + counts: { resources: 0, deployments: 0, vault: 0, team: 1 }, |
| 24 | + resources: [], |
| 25 | + billing: null, |
| 26 | + billingLoading: false, |
| 27 | + }), |
| 28 | +})) |
| 29 | + |
| 30 | +import { UpgradePromptCard } from './UpgradePromptCard' |
| 31 | +import { |
| 32 | + UPGRADE_COPY, |
| 33 | + DEFAULT_UPGRADE_CTA, |
| 34 | + BILLING_PATH, |
| 35 | + type UpgradeFeature, |
| 36 | +} from './upgradeCopy' |
| 37 | + |
| 38 | +afterEach(() => { |
| 39 | + mockMe = null |
| 40 | + cleanup() |
| 41 | +}) |
| 42 | + |
| 43 | +function withMe(me: any, ui: ReactNode) { |
| 44 | + mockMe = me |
| 45 | + return render(<>{ui}</>) |
| 46 | +} |
| 47 | + |
| 48 | +describe('UpgradePromptCard — per-feature copy', () => { |
| 49 | + const FEATURES: UpgradeFeature[] = [ |
| 50 | + 'vault_prod', |
| 51 | + 'provision_twin', |
| 52 | + 'family_bindings', |
| 53 | + 'quota_wall', |
| 54 | + 'custom_domain', |
| 55 | + ] |
| 56 | + |
| 57 | + for (const feature of FEATURES) { |
| 58 | + it(`renders the ${feature} title and body from upgradeCopy`, () => { |
| 59 | + withMe(null, <UpgradePromptCard feature={feature} />) |
| 60 | + const card = screen.getByTestId(`upgrade-prompt-${feature}`) |
| 61 | + expect(card).toBeTruthy() |
| 62 | + expect(card.getAttribute('data-feature')).toBe(feature) |
| 63 | + // Title and body come straight from the central copy map. |
| 64 | + expect(screen.getByTestId('upgrade-prompt-title').textContent).toContain( |
| 65 | + UPGRADE_COPY[feature].title, |
| 66 | + ) |
| 67 | + expect(screen.getByTestId('upgrade-prompt-body').textContent).toContain( |
| 68 | + UPGRADE_COPY[feature].body, |
| 69 | + ) |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + it('renders different copy for two different feature keys', () => { |
| 74 | + const { container, rerender } = withMe( |
| 75 | + null, |
| 76 | + <UpgradePromptCard feature="vault_prod" />, |
| 77 | + ) |
| 78 | + const vaultTitle = container.querySelector('[data-testid="upgrade-prompt-title"]')!.textContent |
| 79 | + rerender(<UpgradePromptCard feature="custom_domain" />) |
| 80 | + const cdTitle = container.querySelector('[data-testid="upgrade-prompt-title"]')!.textContent |
| 81 | + expect(vaultTitle).not.toEqual(cdTitle) |
| 82 | + }) |
| 83 | + |
| 84 | + it('renders the priceLine footer when defined', () => { |
| 85 | + withMe(null, <UpgradePromptCard feature="vault_prod" />) |
| 86 | + expect(screen.getByTestId('upgrade-prompt-price').textContent).toContain('$9/mo') |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +describe('UpgradePromptCard — CTA label resolution', () => { |
| 91 | + it('falls back to DEFAULT_UPGRADE_CTA when /auth/me has no experiment', () => { |
| 92 | + withMe(null, <UpgradePromptCard feature="family_bindings" />) |
| 93 | + expect(screen.getByTestId('upgrade-prompt-cta').textContent).toBe(DEFAULT_UPGRADE_CTA) |
| 94 | + }) |
| 95 | + |
| 96 | + it("falls back to DEFAULT_UPGRADE_CTA when /auth/me has experiments but no upgrade_cta", () => { |
| 97 | + withMe( |
| 98 | + { user: {}, team: { tier: 'hobby' }, experiments: {} }, |
| 99 | + <UpgradePromptCard feature="family_bindings" />, |
| 100 | + ) |
| 101 | + expect(screen.getByTestId('upgrade-prompt-cta').textContent).toBe(DEFAULT_UPGRADE_CTA) |
| 102 | + }) |
| 103 | + |
| 104 | + it('uses P1 experiment label when /auth/me supplies one', () => { |
| 105 | + withMe( |
| 106 | + { |
| 107 | + user: {}, |
| 108 | + team: { tier: 'hobby' }, |
| 109 | + experiments: { upgrade_cta: { variant: 'B', label: 'Start free trial' } }, |
| 110 | + }, |
| 111 | + <UpgradePromptCard feature="family_bindings" />, |
| 112 | + ) |
| 113 | + expect(screen.getByTestId('upgrade-prompt-cta').textContent).toBe('Start free trial') |
| 114 | + }) |
| 115 | + |
| 116 | + it('respects the variantOverride prop (used by tests / parents)', () => { |
| 117 | + withMe( |
| 118 | + null, |
| 119 | + <UpgradePromptCard |
| 120 | + feature="vault_prod" |
| 121 | + variantOverride={{ label: 'Unlock for $9' }} |
| 122 | + />, |
| 123 | + ) |
| 124 | + expect(screen.getByTestId('upgrade-prompt-cta').textContent).toBe('Unlock for $9') |
| 125 | + }) |
| 126 | + |
| 127 | + it('variantOverride={null} forces the default even when /auth/me has a variant', () => { |
| 128 | + withMe( |
| 129 | + { |
| 130 | + user: {}, |
| 131 | + team: { tier: 'hobby' }, |
| 132 | + experiments: { upgrade_cta: { variant: 'B', label: 'Start free trial' } }, |
| 133 | + }, |
| 134 | + <UpgradePromptCard feature="family_bindings" variantOverride={null} />, |
| 135 | + ) |
| 136 | + expect(screen.getByTestId('upgrade-prompt-cta').textContent).toBe(DEFAULT_UPGRADE_CTA) |
| 137 | + }) |
| 138 | +}) |
| 139 | + |
| 140 | +describe('UpgradePromptCard — link target', () => { |
| 141 | + it('defaults to BILLING_PATH (/app/billing)', () => { |
| 142 | + withMe(null, <UpgradePromptCard feature="quota_wall" />) |
| 143 | + expect( |
| 144 | + (screen.getByTestId('upgrade-prompt-cta') as HTMLAnchorElement).getAttribute('href'), |
| 145 | + ).toBe(BILLING_PATH) |
| 146 | + }) |
| 147 | + |
| 148 | + it('respects an explicit href prop', () => { |
| 149 | + withMe( |
| 150 | + null, |
| 151 | + <UpgradePromptCard feature="quota_wall" href="/custom/upgrade" />, |
| 152 | + ) |
| 153 | + expect( |
| 154 | + (screen.getByTestId('upgrade-prompt-cta') as HTMLAnchorElement).getAttribute('href'), |
| 155 | + ).toBe('/custom/upgrade') |
| 156 | + }) |
| 157 | +}) |
0 commit comments