posthog-js-lite 4.1.0 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,289 +0,0 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
-
5
- import { waitForPromises } from 'posthog-core/test/test-utils/test-utils'
6
- import { PostHog } from '..'
7
-
8
- describe('PostHogWeb', () => {
9
- let fetch: jest.Mock
10
- jest.useRealTimers()
11
-
12
- beforeEach(() => {
13
- ;(global as any).window.fetch = fetch = jest.fn(async (url) => {
14
- let res: any = { status: 'ok' }
15
- if (url.includes('flags')) {
16
- res = {
17
- featureFlags: {
18
- 'feature-1': true,
19
- 'feature-2': true,
20
- 'json-payload': true,
21
- 'feature-variant': 'variant',
22
- },
23
- // NOTE: Aren't these supposed to be strings?
24
-
25
- featureFlagPayloads: {
26
- 'feature-1': {
27
- color: 'blue',
28
- },
29
- 'json-payload': {
30
- a: 'payload',
31
- },
32
- 'feature-variant': 5,
33
- },
34
- }
35
- }
36
-
37
- return {
38
- status: 200,
39
- json: () => Promise.resolve(res),
40
- }
41
- })
42
- })
43
-
44
- describe('init', () => {
45
- it('should initialise', async () => {
46
- const posthog = new PostHog('TEST_API_KEY', {
47
- flushAt: 1,
48
- })
49
- expect(posthog.optedOut).toEqual(false)
50
-
51
- posthog.capture('test')
52
- await waitForPromises()
53
-
54
- expect(fetch).toHaveBeenCalledTimes(2)
55
- })
56
-
57
- it('should load feature flags on init', async () => {
58
- const posthog = new PostHog('TEST_API_KEY', {
59
- flushAt: 1,
60
- })
61
-
62
- await waitForPromises()
63
-
64
- expect(fetch).toHaveBeenCalledWith('https://us.i.posthog.com/flags/?v=2&config=true', {
65
- body: JSON.stringify({
66
- token: 'TEST_API_KEY',
67
- distinct_id: posthog.getDistinctId(),
68
- groups: {},
69
- person_properties: {},
70
- group_properties: {},
71
- $anon_distinct_id: posthog.getAnonymousId(),
72
- }),
73
- method: 'POST',
74
- headers: {
75
- 'Content-Type': 'application/json',
76
- },
77
- signal: expect.anything(),
78
- })
79
-
80
- expect(posthog.getFeatureFlags()).toEqual({
81
- 'feature-1': true,
82
- 'feature-2': true,
83
- 'json-payload': true,
84
- 'feature-variant': 'variant',
85
- })
86
-
87
- expect(posthog.getFeatureFlagPayloads()).toEqual({
88
- 'feature-1': {
89
- color: 'blue',
90
- },
91
- 'json-payload': {
92
- a: 'payload',
93
- },
94
- 'feature-variant': 5,
95
- })
96
- })
97
- })
98
-
99
- describe('History API tracking', () => {
100
- const originalPushState = window.history.pushState
101
- const originalReplaceState = window.history.replaceState
102
- let setPathname: (pathname: string) => void
103
-
104
- beforeEach(() => {
105
- const mockLocation = {
106
- pathname: '/initial-path',
107
- }
108
-
109
- Object.defineProperty(window, 'location', {
110
- value: mockLocation,
111
- writable: true,
112
- })
113
-
114
- setPathname = (pathname: string) => {
115
- mockLocation.pathname = pathname
116
- }
117
- })
118
-
119
- // Reset history methods after each test
120
- afterEach(() => {
121
- window.history.pushState = originalPushState
122
- window.history.replaceState = originalReplaceState
123
-
124
- const popstateHandler = (): void => {}
125
- window.addEventListener('popstate', popstateHandler)
126
- window.removeEventListener('popstate', popstateHandler)
127
- })
128
-
129
- it('should not patch history methods when captureHistoryEvents is disabled', () => {
130
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
131
- const posthog = new PostHog('TEST_API_KEY', {
132
- captureHistoryEvents: false,
133
- })
134
-
135
- expect(window.history.pushState).toBe(originalPushState)
136
- expect(window.history.replaceState).toBe(originalReplaceState)
137
- })
138
-
139
- it('should patch history methods when captureHistoryEvents is enabled', () => {
140
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
141
- const posthog = new PostHog('TEST_API_KEY', {
142
- captureHistoryEvents: true,
143
- })
144
-
145
- expect(window.history.pushState).not.toBe(originalPushState)
146
- expect(window.history.replaceState).not.toBe(originalReplaceState)
147
- })
148
-
149
- it('should capture pageview events on pushState when pathname changes', async () => {
150
- const posthog = new PostHog('TEST_API_KEY', {
151
- captureHistoryEvents: true,
152
- flushAt: 1,
153
- })
154
-
155
- const captureSpy = jest.spyOn(posthog, 'capture')
156
-
157
- // Change pathname
158
- setPathname('/test-page')
159
- window.history.pushState({}, '', '/test-page')
160
-
161
- expect(captureSpy).toHaveBeenCalledWith(
162
- '$pageview',
163
- expect.objectContaining({
164
- navigation_type: 'pushState',
165
- })
166
- )
167
- })
168
-
169
- it('should not capture pageview events on pushState when pathname does not change', async () => {
170
- const posthog = new PostHog('TEST_API_KEY', {
171
- captureHistoryEvents: true,
172
- flushAt: 1,
173
- })
174
-
175
- const captureSpy = jest.spyOn(posthog, 'capture')
176
- captureSpy.mockClear()
177
-
178
- // Don't change pathname
179
- window.history.pushState({}, '', '/initial-path')
180
-
181
- expect(captureSpy).not.toHaveBeenCalled()
182
- })
183
-
184
- it('should capture pageview events on replaceState when pathname changes', async () => {
185
- const posthog = new PostHog('TEST_API_KEY', {
186
- captureHistoryEvents: true,
187
- flushAt: 1,
188
- })
189
-
190
- const captureSpy = jest.spyOn(posthog, 'capture')
191
-
192
- // Change pathname
193
- setPathname('/replaced-page')
194
- window.history.replaceState({}, '', '/replaced-page')
195
-
196
- expect(captureSpy).toHaveBeenCalledWith(
197
- '$pageview',
198
- expect.objectContaining({
199
- navigation_type: 'replaceState',
200
- })
201
- )
202
- })
203
-
204
- it('should not capture pageview events on replaceState when pathname does not change', async () => {
205
- const posthog = new PostHog('TEST_API_KEY', {
206
- captureHistoryEvents: true,
207
- flushAt: 1,
208
- })
209
-
210
- const captureSpy = jest.spyOn(posthog, 'capture')
211
- captureSpy.mockClear()
212
-
213
- // Don't change pathname
214
- window.history.replaceState({}, '', '/initial-path')
215
-
216
- expect(captureSpy).not.toHaveBeenCalled()
217
- })
218
-
219
- it('should capture pageview events on popstate when pathname changes', async () => {
220
- const posthog = new PostHog('TEST_API_KEY', {
221
- captureHistoryEvents: true,
222
- flushAt: 1,
223
- })
224
-
225
- const captureSpy = jest.spyOn(posthog, 'capture')
226
-
227
- // Change pathname
228
- setPathname('/popstate-page')
229
- window.dispatchEvent(new Event('popstate'))
230
-
231
- expect(captureSpy).toHaveBeenCalledWith(
232
- '$pageview',
233
- expect.objectContaining({
234
- navigation_type: 'popstate',
235
- })
236
- )
237
- })
238
-
239
- it('should not capture pageview events on popstate when pathname does not change', async () => {
240
- const posthog = new PostHog('TEST_API_KEY', {
241
- captureHistoryEvents: true,
242
- flushAt: 1,
243
- })
244
-
245
- const captureSpy = jest.spyOn(posthog, 'capture')
246
- captureSpy.mockClear()
247
-
248
- // Don't change pathname
249
- window.dispatchEvent(new Event('popstate'))
250
-
251
- expect(captureSpy).not.toHaveBeenCalled()
252
- })
253
-
254
- it('should include navigation properties in capture call and rely on getCommonEventProperties', async () => {
255
- const posthog = new PostHog('TEST_API_KEY', {
256
- captureHistoryEvents: true,
257
- flushAt: 1,
258
- })
259
-
260
- const commonEventProps = { $lib: 'posthog-js-lite', $lib_version: '1.0.0' }
261
- const getCommonEventPropertiesSpy = jest
262
- .spyOn(posthog, 'getCommonEventProperties')
263
- .mockImplementation(() => commonEventProps)
264
-
265
- const coreCaptureMethod = jest.spyOn(PostHog.prototype, 'capture')
266
-
267
- // Change pathname
268
- setPathname('/captured-page')
269
- window.history.pushState({}, '', '/captured-page')
270
-
271
- // Will use a mock here for now and rely on the implementation since the tests setup is very simple at the moment
272
- expect(getCommonEventPropertiesSpy).toHaveBeenCalled()
273
-
274
- const captureCall = coreCaptureMethod.mock.calls.find(
275
- (call) => call[0] === '$pageview' && call[1] && call[1].navigation_type === 'pushState'
276
- )
277
-
278
- expect(captureCall).toBeDefined()
279
-
280
- const navigationProperties = {
281
- navigation_type: 'pushState',
282
- }
283
-
284
- if (captureCall) {
285
- expect(captureCall[1]).toMatchObject(navigationProperties)
286
- }
287
- })
288
- })
289
- })
package/tsconfig.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "extends": "../tsconfig.json",
3
- "compilerOptions": {
4
- "incremental": false,
5
- "lib": ["DOM", "ES2020", "ES2022.Error"]
6
- }
7
- }