|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +import { jest, beforeEach, afterEach, describe, test, expect } from '@jest/globals'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Unit tests for AnalyticsService.capture() and enabled-flag behaviour. |
| 8 | + */ |
| 9 | +describe('Analytics capture() and enabled-flag:', () => { |
| 10 | + let AnalyticsService; |
| 11 | + let mockPostHogInstance; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + jest.resetModules(); |
| 15 | + |
| 16 | + mockPostHogInstance = { |
| 17 | + capture: jest.fn(), |
| 18 | + identify: jest.fn(), |
| 19 | + groupIdentify: jest.fn(), |
| 20 | + getFeatureFlag: jest.fn().mockResolvedValue(undefined), |
| 21 | + isFeatureEnabled: jest.fn().mockResolvedValue(undefined), |
| 22 | + shutdown: jest.fn().mockResolvedValue(undefined), |
| 23 | + }; |
| 24 | + |
| 25 | + jest.unstable_mockModule('posthog-node', () => ({ |
| 26 | + PostHog: jest.fn().mockImplementation(() => mockPostHogInstance), |
| 27 | + })); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + jest.restoreAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + // ───────────────────────────────────────────────────────────────── |
| 35 | + // 1. enabled=false disables client creation |
| 36 | + // ───────────────────────────────────────────────────────────────── |
| 37 | + describe('enabled flag:', () => { |
| 38 | + test('returns null client when enabled=false even if apiKey is present', async () => { |
| 39 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 40 | + default: { posthog: { enabled: false, apiKey: 'phc_test_key', host: 'https://eu.i.posthog.com' } }, |
| 41 | + })); |
| 42 | + |
| 43 | + const mod = await import('../analytics.js'); |
| 44 | + AnalyticsService = mod.default; |
| 45 | + |
| 46 | + await AnalyticsService.init(); |
| 47 | + AnalyticsService.capture({ distinctId: 'user-1', event: 'test' }); |
| 48 | + |
| 49 | + const { PostHog } = await import('posthog-node'); |
| 50 | + expect(PostHog).not.toHaveBeenCalled(); |
| 51 | + expect(mockPostHogInstance.capture).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + test('returns null client when apiKey is missing even if enabled=true', async () => { |
| 55 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 56 | + default: { posthog: { enabled: true } }, |
| 57 | + })); |
| 58 | + |
| 59 | + const mod = await import('../analytics.js'); |
| 60 | + AnalyticsService = mod.default; |
| 61 | + |
| 62 | + await AnalyticsService.init(); |
| 63 | + AnalyticsService.capture({ distinctId: 'user-1', event: 'test' }); |
| 64 | + |
| 65 | + const { PostHog } = await import('posthog-node'); |
| 66 | + expect(PostHog).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + test('creates client when enabled=true and apiKey is present', async () => { |
| 70 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 71 | + default: { posthog: { enabled: true, apiKey: 'phc_test_key', host: 'https://eu.i.posthog.com' } }, |
| 72 | + })); |
| 73 | + |
| 74 | + const mod = await import('../analytics.js'); |
| 75 | + AnalyticsService = mod.default; |
| 76 | + |
| 77 | + await AnalyticsService.init(); |
| 78 | + |
| 79 | + const { PostHog } = await import('posthog-node'); |
| 80 | + expect(PostHog).toHaveBeenCalledWith('phc_test_key', expect.objectContaining({ host: 'https://eu.i.posthog.com' })); |
| 81 | + }); |
| 82 | + |
| 83 | + test('passes flushAt and flushInterval to PostHog constructor', async () => { |
| 84 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 85 | + default: { posthog: { enabled: true, apiKey: 'phc_key', host: 'https://eu.i.posthog.com', flushAt: 20, flushInterval: 10000 } }, |
| 86 | + })); |
| 87 | + |
| 88 | + const mod = await import('../analytics.js'); |
| 89 | + AnalyticsService = mod.default; |
| 90 | + |
| 91 | + await AnalyticsService.init(); |
| 92 | + |
| 93 | + const { PostHog } = await import('posthog-node'); |
| 94 | + expect(PostHog).toHaveBeenCalledWith('phc_key', { |
| 95 | + host: 'https://eu.i.posthog.com', |
| 96 | + flushAt: 20, |
| 97 | + flushInterval: 10000, |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + test('singleton: two init() calls on the same module instance result in one PostHog client', async () => { |
| 102 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 103 | + default: { posthog: { enabled: true, apiKey: 'phc_key', host: 'https://eu.i.posthog.com' } }, |
| 104 | + })); |
| 105 | + |
| 106 | + const mod = await import('../analytics.js'); |
| 107 | + AnalyticsService = mod.default; |
| 108 | + |
| 109 | + await AnalyticsService.init(); |
| 110 | + await AnalyticsService.init(); // singleton guard: no-op, client already set |
| 111 | + |
| 112 | + const { PostHog } = await import('posthog-node'); |
| 113 | + expect(PostHog).toHaveBeenCalledTimes(1); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + // ───────────────────────────────────────────────────────────────── |
| 118 | + // 2. capture() no-ops |
| 119 | + // ───────────────────────────────────────────────────────────────── |
| 120 | + describe('capture() no-ops:', () => { |
| 121 | + test('is a no-op when client is null', async () => { |
| 122 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 123 | + default: { posthog: { enabled: false, apiKey: 'phc_key' } }, |
| 124 | + })); |
| 125 | + |
| 126 | + const mod = await import('../analytics.js'); |
| 127 | + AnalyticsService = mod.default; |
| 128 | + |
| 129 | + await AnalyticsService.init(); |
| 130 | + AnalyticsService.capture({ distinctId: 'user-1', event: 'some_event' }); |
| 131 | + |
| 132 | + expect(mockPostHogInstance.capture).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + test('is a no-op when distinctId is missing', async () => { |
| 136 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 137 | + default: { posthog: { enabled: true, apiKey: 'phc_key', host: 'https://eu.i.posthog.com' } }, |
| 138 | + })); |
| 139 | + |
| 140 | + const mod = await import('../analytics.js'); |
| 141 | + AnalyticsService = mod.default; |
| 142 | + |
| 143 | + await AnalyticsService.init(); |
| 144 | + AnalyticsService.capture({ event: 'some_event' }); |
| 145 | + |
| 146 | + expect(mockPostHogInstance.capture).not.toHaveBeenCalled(); |
| 147 | + }); |
| 148 | + |
| 149 | + test('is a no-op when event is missing', async () => { |
| 150 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 151 | + default: { posthog: { enabled: true, apiKey: 'phc_key', host: 'https://eu.i.posthog.com' } }, |
| 152 | + })); |
| 153 | + |
| 154 | + const mod = await import('../analytics.js'); |
| 155 | + AnalyticsService = mod.default; |
| 156 | + |
| 157 | + await AnalyticsService.init(); |
| 158 | + AnalyticsService.capture({ distinctId: 'user-1' }); |
| 159 | + |
| 160 | + expect(mockPostHogInstance.capture).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + // ───────────────────────────────────────────────────────────────── |
| 165 | + // 3. capture() auto-injects app + env |
| 166 | + // ───────────────────────────────────────────────────────────────── |
| 167 | + describe('capture() property injection:', () => { |
| 168 | + beforeEach(async () => { |
| 169 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 170 | + default: { posthog: { enabled: true, apiKey: 'phc_key', host: 'https://eu.i.posthog.com', appTag: 'myapp' } }, |
| 171 | + })); |
| 172 | + |
| 173 | + const mod = await import('../analytics.js'); |
| 174 | + AnalyticsService = mod.default; |
| 175 | + await AnalyticsService.init(); |
| 176 | + }); |
| 177 | + |
| 178 | + test('auto-injects app from appTag and env from NODE_ENV', async () => { |
| 179 | + const origEnv = process.env.NODE_ENV; |
| 180 | + process.env.NODE_ENV = 'test'; |
| 181 | + |
| 182 | + AnalyticsService.capture({ distinctId: 'user-1', event: 'my_event' }); |
| 183 | + |
| 184 | + expect(mockPostHogInstance.capture).toHaveBeenCalledWith({ |
| 185 | + distinctId: 'user-1', |
| 186 | + event: 'my_event', |
| 187 | + properties: { app: 'myapp', env: 'test' }, |
| 188 | + }); |
| 189 | + |
| 190 | + process.env.NODE_ENV = origEnv; |
| 191 | + }); |
| 192 | + |
| 193 | + test('custom properties win over defaults', async () => { |
| 194 | + AnalyticsService.capture({ distinctId: 'user-1', event: 'my_event', properties: { app: 'override', custom: 'val' } }); |
| 195 | + |
| 196 | + expect(mockPostHogInstance.capture).toHaveBeenCalledWith({ |
| 197 | + distinctId: 'user-1', |
| 198 | + event: 'my_event', |
| 199 | + properties: expect.objectContaining({ app: 'override', custom: 'val' }), |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + test('does not inject app when appTag is not configured', async () => { |
| 204 | + jest.resetModules(); |
| 205 | + |
| 206 | + jest.unstable_mockModule('posthog-node', () => ({ |
| 207 | + PostHog: jest.fn().mockImplementation(() => mockPostHogInstance), |
| 208 | + })); |
| 209 | + |
| 210 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 211 | + default: { posthog: { enabled: true, apiKey: 'phc_key', host: 'https://eu.i.posthog.com' } }, |
| 212 | + })); |
| 213 | + |
| 214 | + const mod = await import('../analytics.js'); |
| 215 | + AnalyticsService = mod.default; |
| 216 | + await AnalyticsService.init(); |
| 217 | + |
| 218 | + AnalyticsService.capture({ distinctId: 'user-1', event: 'my_event' }); |
| 219 | + |
| 220 | + const call = mockPostHogInstance.capture.mock.calls[0][0]; |
| 221 | + expect(call.properties).not.toHaveProperty('app'); |
| 222 | + expect(call.properties).toHaveProperty('env'); |
| 223 | + }); |
| 224 | + }); |
| 225 | + |
| 226 | + // ───────────────────────────────────────────────────────────────── |
| 227 | + // 4. shutdown idempotency |
| 228 | + // ───────────────────────────────────────────────────────────────── |
| 229 | + describe('shutdown idempotency:', () => { |
| 230 | + test('two shutdown() calls invoke client.shutdown exactly once', async () => { |
| 231 | + jest.unstable_mockModule('../../../config/index.js', () => ({ |
| 232 | + default: { posthog: { enabled: true, apiKey: 'phc_key', host: 'https://eu.i.posthog.com' } }, |
| 233 | + })); |
| 234 | + |
| 235 | + const mod = await import('../analytics.js'); |
| 236 | + AnalyticsService = mod.default; |
| 237 | + await AnalyticsService.init(); |
| 238 | + |
| 239 | + await AnalyticsService.shutdown(); |
| 240 | + await AnalyticsService.shutdown(); |
| 241 | + |
| 242 | + expect(mockPostHogInstance.shutdown).toHaveBeenCalledTimes(1); |
| 243 | + }); |
| 244 | + }); |
| 245 | +}); |
0 commit comments