-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathPostHogProvider.test.tsx
More file actions
140 lines (116 loc) · 4.62 KB
/
PostHogProvider.test.tsx
File metadata and controls
140 lines (116 loc) · 4.62 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as React from 'react'
import { render, act } from '@testing-library/react'
import { PostHogProvider, PostHog } from '..'
import posthogJs from 'posthog-js'
import { setDefaultPostHogInstance } from '../posthog-default'
// Mock posthog-js
jest.mock('posthog-js', () => ({
__esModule: true,
default: {
init: jest.fn(),
set_config: jest.fn(),
__loaded: false,
},
}))
describe('PostHogProvider component', () => {
beforeEach(() => {
setDefaultPostHogInstance(posthogJs)
})
afterEach(() => {
setDefaultPostHogInstance(undefined)
})
it('should render children components', () => {
const posthog = {} as unknown as PostHog
const { getByText } = render(
<PostHogProvider client={posthog}>
<div>Test</div>
</PostHogProvider>
)
expect(getByText('Test')).toBeTruthy()
})
describe('when using apiKey initialization', () => {
const apiKey = 'test-api-key'
const initialOptions = { api_host: 'https://app.posthog.com' }
const updatedOptions = { api_host: 'https://eu.posthog.com' }
beforeEach(() => {
jest.clearAllMocks()
})
it('should call set_config when options change', () => {
const { rerender } = render(
<PostHogProvider apiKey={apiKey} options={initialOptions}>
<div>Test</div>
</PostHogProvider>
)
// First render should initialize
expect(posthogJs.init).toHaveBeenCalledWith(apiKey, initialOptions)
// Rerender with new options
act(() => {
rerender(
<PostHogProvider apiKey={apiKey} options={updatedOptions}>
<div>Test</div>
</PostHogProvider>
)
})
// Should call set_config with new options
expect(posthogJs.set_config).toHaveBeenCalledWith(updatedOptions)
})
it('should NOT call set_config when we pass new options that are the same as the previous options', () => {
const { rerender } = render(
<PostHogProvider apiKey={apiKey} options={initialOptions}>
<div>Test</div>
</PostHogProvider>
)
// First render should initialize
expect(posthogJs.init).toHaveBeenCalledWith(apiKey, initialOptions)
// Rerender with new options
const sameOptionsButDifferentReference = { ...initialOptions }
act(() => {
rerender(
<PostHogProvider apiKey={apiKey} options={sameOptionsButDifferentReference}>
<div>Test</div>
</PostHogProvider>
)
})
// Should NOT call set_config
expect(posthogJs.set_config).not.toHaveBeenCalled()
})
it('should warn when attempting to change apiKey', () => {
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
const newApiKey = 'different-api-key'
const { rerender } = render(
<PostHogProvider apiKey={apiKey} options={initialOptions}>
<div>Test</div>
</PostHogProvider>
)
// First render should initialize
expect(posthogJs.init).toHaveBeenCalledWith(apiKey, initialOptions)
// Rerender with new apiKey
act(() => {
rerender(
<PostHogProvider apiKey={newApiKey} options={initialOptions}>
<div>Test</div>
</PostHogProvider>
)
})
// Should warn about apiKey change
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('You have provided a different `apiKey` to `PostHogProvider`')
)
consoleSpy.mockRestore()
})
it('warns if posthogJs has been loaded elsewhere', () => {
;(posthogJs as any).__loaded = true
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
render(
<PostHogProvider apiKey={apiKey} options={initialOptions}>
<div>Test</div>
</PostHogProvider>
)
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('`posthog` was already loaded elsewhere. This may cause issues.')
)
consoleSpy.mockRestore()
;(posthogJs as any).__loaded = false
})
})
})