-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathPostHogContext.test.tsx
More file actions
58 lines (50 loc) · 1.93 KB
/
PostHogContext.test.tsx
File metadata and controls
58 lines (50 loc) · 1.93 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
import * as React from 'react'
import { render } from '@testing-library/react'
import { PostHogProvider, PostHog, PostHogContext } from '..'
import posthogJs from 'posthog-js'
import { setDefaultPostHogInstance } from '../posthog-default'
jest.mock('posthog-js', () => ({
__esModule: true,
default: {
init: jest.fn(),
__loaded: false,
},
}))
describe('PostHogContext component', () => {
const posthog = {} as unknown as PostHog
beforeEach(() => {
setDefaultPostHogInstance(posthogJs)
})
afterEach(() => {
setDefaultPostHogInstance(undefined)
})
it('should return a client instance from the context if available', () => {
function ClientConsumer() {
const { client } = React.useContext(PostHogContext)
return <div data-testid="client">{client === posthog ? 'match' : 'mismatch'}</div>
}
const { getByTestId } = render(
<PostHogProvider client={posthog}>
<ClientConsumer />
</PostHogProvider>
)
expect(getByTestId('client').textContent).toBe('match')
})
it("should not throw error if a client instance can't be found in the context", () => {
// eslint-disable-next-line no-console
console.warn = jest.fn()
expect(() => {
render(
// we have to cast `as any` so that we can test for when
// posthog might not exist - in SSR for example
<PostHogProvider client={undefined as any}>
<div>Hello</div>
</PostHogProvider>
)
}).not.toThrow()
// eslint-disable-next-line no-console
expect(console.warn).toHaveBeenCalledWith(
'[PostHog.js] No `apiKey` or `client` were provided to `PostHogProvider`. Using default global `window.posthog` instance. You must initialize it manually. This is not recommended behavior.'
)
})
})