|
| 1 | +import { PostHog } from '../posthog-core' |
| 2 | +import { PostHogConfig } from '../types' |
| 3 | +import { AllExtensions } from '../extensions/extension-bundles' |
| 4 | +import { Autocapture } from '../autocapture' |
| 5 | +import { SessionRecording } from '../extensions/replay/session-recording' |
| 6 | +import { createPosthogInstance } from './helpers/posthog-instance' |
| 7 | + |
| 8 | +describe('__extensionClasses enrollment', () => { |
| 9 | + let savedDefaults: PostHogConfig['__extensionClasses'] |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + savedDefaults = PostHog.__defaultExtensionClasses |
| 13 | + console.error = jest.fn() |
| 14 | + }) |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + PostHog.__defaultExtensionClasses = savedDefaults |
| 18 | + }) |
| 19 | + |
| 20 | + it('initializes only extensions provided via __extensionClasses', async () => { |
| 21 | + PostHog.__defaultExtensionClasses = {} |
| 22 | + |
| 23 | + const posthog = await createPosthogInstance(undefined, { |
| 24 | + __preview_deferred_init_extensions: false, |
| 25 | + __extensionClasses: { autocapture: Autocapture, sessionRecording: SessionRecording }, |
| 26 | + capture_pageview: false, |
| 27 | + }) |
| 28 | + |
| 29 | + // Wait for deferred init |
| 30 | + await new Promise((resolve) => setTimeout(resolve, 200)) |
| 31 | + |
| 32 | + expect(posthog.autocapture).toBeDefined() |
| 33 | + expect(posthog.sessionRecording).toBeDefined() |
| 34 | + |
| 35 | + expect(posthog.heatmaps).toBeUndefined() |
| 36 | + expect(posthog.exceptionObserver).toBeUndefined() |
| 37 | + expect(posthog.deadClicksAutocapture).toBeUndefined() |
| 38 | + expect(posthog.webVitalsAutocapture).toBeUndefined() |
| 39 | + expect(posthog.productTours).toBeUndefined() |
| 40 | + expect(posthog.siteApps).toBeUndefined() |
| 41 | + }) |
| 42 | + |
| 43 | + it('initializes no extensions when none are provided and no defaults exist', async () => { |
| 44 | + PostHog.__defaultExtensionClasses = {} |
| 45 | + |
| 46 | + const posthog = await createPosthogInstance(undefined, { |
| 47 | + __preview_deferred_init_extensions: false, |
| 48 | + capture_pageview: false, |
| 49 | + }) |
| 50 | + |
| 51 | + expect(posthog.autocapture).toBeUndefined() |
| 52 | + expect(posthog.sessionRecording).toBeUndefined() |
| 53 | + expect(posthog.heatmaps).toBeUndefined() |
| 54 | + expect(posthog.exceptionObserver).toBeUndefined() |
| 55 | + expect(posthog.deadClicksAutocapture).toBeUndefined() |
| 56 | + expect(posthog.webVitalsAutocapture).toBeUndefined() |
| 57 | + expect(posthog.productTours).toBeUndefined() |
| 58 | + expect(posthog.siteApps).toBeUndefined() |
| 59 | + }) |
| 60 | + |
| 61 | + it('__extensionClasses overrides __defaultExtensionClasses', async () => { |
| 62 | + PostHog.__defaultExtensionClasses = AllExtensions |
| 63 | + |
| 64 | + class MockAutocapture extends Autocapture {} |
| 65 | + |
| 66 | + const posthog = await createPosthogInstance(undefined, { |
| 67 | + __preview_deferred_init_extensions: false, |
| 68 | + __extensionClasses: { autocapture: MockAutocapture }, |
| 69 | + capture_pageview: false, |
| 70 | + }) |
| 71 | + |
| 72 | + expect(posthog.autocapture).toBeInstanceOf(MockAutocapture) |
| 73 | + }) |
| 74 | + |
| 75 | + it('default extensions are used when __extensionClasses is not provided', async () => { |
| 76 | + PostHog.__defaultExtensionClasses = AllExtensions |
| 77 | + |
| 78 | + const posthog = await createPosthogInstance(undefined, { |
| 79 | + __preview_deferred_init_extensions: false, |
| 80 | + capture_pageview: false, |
| 81 | + }) |
| 82 | + |
| 83 | + expect(posthog.autocapture).toBeDefined() |
| 84 | + expect(posthog.sessionRecording).toBeDefined() |
| 85 | + expect(posthog.heatmaps).toBeDefined() |
| 86 | + expect(posthog.exceptionObserver).toBeDefined() |
| 87 | + expect(posthog.deadClicksAutocapture).toBeDefined() |
| 88 | + expect(posthog.webVitalsAutocapture).toBeDefined() |
| 89 | + expect(posthog.productTours).toBeDefined() |
| 90 | + expect(posthog.siteApps).toBeDefined() |
| 91 | + }) |
| 92 | +}) |
0 commit comments