-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathPostHogProviderSlim.test.tsx
More file actions
59 lines (50 loc) · 1.87 KB
/
PostHogProviderSlim.test.tsx
File metadata and controls
59 lines (50 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as React from 'react'
import { render } from '@testing-library/react'
import { PostHogProvider } from '../PostHogProviderSlim'
import { PostHogContext } from '../PostHogContext'
import type { PostHog } from 'posthog-js'
// Do NOT call setDefaultPostHogInstance — this simulates the slim bundle
// where no default instance exists.
let contextClient: PostHog | undefined
function ClientConsumer() {
const { client } = React.useContext(PostHogContext)
contextClient = client
return <div>consumed</div>
}
describe('PostHogProvider (slim)', () => {
afterEach(() => {
contextClient = undefined
})
it('renders children', () => {
const client = { config: {} } as unknown as PostHog
const { getByText } = render(
<PostHogProvider client={client}>
<div>Hello</div>
</PostHogProvider>
)
expect(getByText('Hello')).toBeTruthy()
})
it('provides the exact client instance via context', () => {
const client = { config: {} } as unknown as PostHog
render(
<PostHogProvider client={client}>
<ClientConsumer />
</PostHogProvider>
)
expect(contextClient).toBe(client)
})
it('provides bootstrap from client config', () => {
const bootstrap = { featureFlags: { 'test-flag': true } }
const client = { config: { bootstrap } } as unknown as PostHog
function BootstrapConsumer() {
const { bootstrap: ctx } = React.useContext(PostHogContext)
return <div data-testid="bootstrap">{JSON.stringify(ctx)}</div>
}
const { getByTestId } = render(
<PostHogProvider client={client}>
<BootstrapConsumer />
</PostHogProvider>
)
expect(JSON.parse(getByTestId('bootstrap').textContent!)).toEqual(bootstrap)
})
})