|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +import { describe, expect, it, vi, beforeEach } from 'vitest'; |
| 3 | + |
| 4 | +const captureMock = vi.hoisted(() => vi.fn()); |
| 5 | +vi.mock('posthog-node', () => ({ |
| 6 | + PostHog: vi.fn(function () { |
| 7 | + return { |
| 8 | + capture: captureMock, |
| 9 | + shutdown: vi.fn().mockResolvedValue(undefined), |
| 10 | + }; |
| 11 | + }), |
| 12 | +})); |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + captureMock.mockClear(); |
| 16 | + process.env.NEXT_PUBLIC_POSTHOG_TOKEN = 'phc_test'; |
| 17 | +}); |
| 18 | + |
| 19 | +describe('captureLeadQualified', () => { |
| 20 | + it('fires marketing:lead_qualified when domain is non-personal and company is non-empty', async () => { |
| 21 | + const { captureLeadQualified } = await import('./server'); |
| 22 | + await captureLeadQualified({ |
| 23 | + |
| 24 | + company: 'Acme', |
| 25 | + sourcePage: '/pricing', |
| 26 | + }); |
| 27 | + expect(captureMock).toHaveBeenCalledTimes(1); |
| 28 | + const call = captureMock.mock.calls[0][0]; |
| 29 | + expect(call.event).toBe('marketing:lead_qualified'); |
| 30 | + expect(call.properties).toMatchObject({ |
| 31 | + email_domain: 'acme.com', |
| 32 | + company: 'Acme', |
| 33 | + source_page: '/pricing', |
| 34 | + track: 'enterprise', |
| 35 | + }); |
| 36 | + expect(call.distinctId).toMatch(/^email_sha256:[a-f0-9]{64}$/); |
| 37 | + }); |
| 38 | + |
| 39 | + it('skips when the email domain is personal', async () => { |
| 40 | + const { captureLeadQualified } = await import('./server'); |
| 41 | + await captureLeadQualified({ |
| 42 | + |
| 43 | + company: 'Acme', |
| 44 | + sourcePage: '/pricing', |
| 45 | + }); |
| 46 | + expect(captureMock).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('skips when company is missing', async () => { |
| 50 | + const { captureLeadQualified } = await import('./server'); |
| 51 | + await captureLeadQualified({ |
| 52 | + |
| 53 | + sourcePage: '/pricing', |
| 54 | + }); |
| 55 | + expect(captureMock).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('skips when company is blank string', async () => { |
| 59 | + const { captureLeadQualified } = await import('./server'); |
| 60 | + await captureLeadQualified({ |
| 61 | + |
| 62 | + company: ' ', |
| 63 | + sourcePage: '/pricing', |
| 64 | + }); |
| 65 | + expect(captureMock).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('skips when email is malformed', async () => { |
| 69 | + const { captureLeadQualified } = await import('./server'); |
| 70 | + await captureLeadQualified({ |
| 71 | + email: 'not-an-email', |
| 72 | + company: 'Acme', |
| 73 | + }); |
| 74 | + expect(captureMock).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments