@posthog/core 1.2.1 → 1.2.2
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.
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -20
- package/dist/index.mjs +1 -2
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/index.ts +0 -1
- package/src/types.ts +1 -1
- package/src/__tests__/featureFlagUtils.spec.ts +0 -427
- package/src/__tests__/gzip.spec.ts +0 -69
- package/src/__tests__/posthog.ai.spec.ts +0 -110
- package/src/__tests__/posthog.capture.spec.ts +0 -91
- package/src/__tests__/posthog.core.spec.ts +0 -135
- package/src/__tests__/posthog.debug.spec.ts +0 -36
- package/src/__tests__/posthog.enqueue.spec.ts +0 -93
- package/src/__tests__/posthog.featureflags.spec.ts +0 -1106
- package/src/__tests__/posthog.featureflags.v1.spec.ts +0 -922
- package/src/__tests__/posthog.flush.spec.ts +0 -237
- package/src/__tests__/posthog.gdpr.spec.ts +0 -50
- package/src/__tests__/posthog.groups.spec.ts +0 -96
- package/src/__tests__/posthog.identify.spec.ts +0 -194
- package/src/__tests__/posthog.init.spec.ts +0 -110
- package/src/__tests__/posthog.listeners.spec.ts +0 -51
- package/src/__tests__/posthog.register.spec.ts +0 -47
- package/src/__tests__/posthog.reset.spec.ts +0 -76
- package/src/__tests__/posthog.sessions.spec.ts +0 -63
- package/src/__tests__/posthog.setProperties.spec.ts +0 -102
- package/src/__tests__/posthog.shutdown.spec.ts +0 -88
- package/src/__tests__/utils.spec.ts +0 -36
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { isGzipSupported, gzipCompress } from '@/gzip'
|
|
2
|
-
import { gzip } from 'node:zlib'
|
|
3
|
-
import { randomBytes, randomUUID } from 'node:crypto'
|
|
4
|
-
import { promisify } from 'node:util'
|
|
5
|
-
|
|
6
|
-
const RANDOM_TEST_INPUT = JSON.stringify({
|
|
7
|
-
abc: randomBytes(16),
|
|
8
|
-
def: randomBytes(64),
|
|
9
|
-
})
|
|
10
|
-
const API_TEST_INPUT = JSON.stringify({
|
|
11
|
-
api_key: 'TEST_API_KEY',
|
|
12
|
-
batch: [
|
|
13
|
-
{
|
|
14
|
-
event: 'custom-event',
|
|
15
|
-
distinct_id: 'user-distinct-id',
|
|
16
|
-
library: 'posthog-core-tests',
|
|
17
|
-
library_version: '2.0.0-alpha',
|
|
18
|
-
properties: {
|
|
19
|
-
$lib: 'posthog-core-tests',
|
|
20
|
-
$lib_version: '2.0.0-alpha',
|
|
21
|
-
$session_id: 'session.id',
|
|
22
|
-
},
|
|
23
|
-
timestamp: new Date().toISOString(),
|
|
24
|
-
uuid: randomUUID(),
|
|
25
|
-
type: 'capture',
|
|
26
|
-
},
|
|
27
|
-
],
|
|
28
|
-
sent_at: new Date().toISOString(),
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
describe('gzip', () => {
|
|
32
|
-
describe('isGzipSupported', () => {
|
|
33
|
-
it('should return true if CompressStream exists', () => {
|
|
34
|
-
expect(globalThis.CompressionStream).toBeDefined()
|
|
35
|
-
expect(isGzipSupported()).toBe(true)
|
|
36
|
-
})
|
|
37
|
-
it('should return false if CompressStream not available', () => {
|
|
38
|
-
const CompressionStream = globalThis.CompressionStream
|
|
39
|
-
delete (globalThis as any).CompressionStream
|
|
40
|
-
expect(isGzipSupported()).toBe(false)
|
|
41
|
-
;(globalThis as any).CompressionStream = CompressionStream
|
|
42
|
-
})
|
|
43
|
-
})
|
|
44
|
-
describe('gzipCompress', () => {
|
|
45
|
-
it('compressed random data should match node', async () => {
|
|
46
|
-
const compressed = await gzipCompress(RANDOM_TEST_INPUT)
|
|
47
|
-
expect(compressed).not.toBe(null)
|
|
48
|
-
if (!compressed) {
|
|
49
|
-
return
|
|
50
|
-
}
|
|
51
|
-
const webCompress = Buffer.from(await compressed.arrayBuffer())
|
|
52
|
-
const nodeCompress = await promisify(gzip)(RANDOM_TEST_INPUT)
|
|
53
|
-
expect(webCompress).not.toBeFalsy()
|
|
54
|
-
expect(webCompress).toEqual(nodeCompress)
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
it('compressed mock request should match node', async () => {
|
|
58
|
-
const compressed = await gzipCompress(API_TEST_INPUT)
|
|
59
|
-
expect(compressed).not.toBe(null)
|
|
60
|
-
if (!compressed) {
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
const webCompress = Buffer.from(await compressed.arrayBuffer())
|
|
64
|
-
const nodeCompress = await promisify(gzip)(API_TEST_INPUT)
|
|
65
|
-
expect(webCompress).not.toBeFalsy()
|
|
66
|
-
expect(webCompress).toEqual(nodeCompress)
|
|
67
|
-
})
|
|
68
|
-
})
|
|
69
|
-
})
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
parseBody,
|
|
3
|
-
waitForPromises,
|
|
4
|
-
createTestClient,
|
|
5
|
-
PostHogCoreTestClient,
|
|
6
|
-
PostHogCoreTestClientMocks,
|
|
7
|
-
} from '@/testing'
|
|
8
|
-
|
|
9
|
-
describe('PostHog Core', () => {
|
|
10
|
-
let posthog: PostHogCoreTestClient
|
|
11
|
-
let mocks: PostHogCoreTestClientMocks
|
|
12
|
-
|
|
13
|
-
jest.useFakeTimers()
|
|
14
|
-
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', { flushAt: 1 })
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
describe('ai', () => {
|
|
20
|
-
it('should capture feedback', async () => {
|
|
21
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
22
|
-
|
|
23
|
-
posthog.captureTraceFeedback('trace-id', 'feedback')
|
|
24
|
-
|
|
25
|
-
await waitForPromises()
|
|
26
|
-
expect(mocks.fetch).toHaveBeenCalledTimes(1)
|
|
27
|
-
const body = parseBody(mocks.fetch.mock.calls[0])
|
|
28
|
-
|
|
29
|
-
expect(body).toMatchObject({
|
|
30
|
-
batch: [
|
|
31
|
-
{
|
|
32
|
-
event: '$ai_feedback',
|
|
33
|
-
properties: {
|
|
34
|
-
$ai_feedback_text: 'feedback',
|
|
35
|
-
$ai_trace_id: 'trace-id',
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
],
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
it('should convert numeric traceId in captureTraceFeedback', async () => {
|
|
43
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
44
|
-
|
|
45
|
-
posthog.captureTraceFeedback(10, 'feedback')
|
|
46
|
-
|
|
47
|
-
await waitForPromises()
|
|
48
|
-
expect(mocks.fetch).toHaveBeenCalledTimes(1)
|
|
49
|
-
const body = parseBody(mocks.fetch.mock.calls[0])
|
|
50
|
-
|
|
51
|
-
expect(body).toMatchObject({
|
|
52
|
-
batch: [
|
|
53
|
-
{
|
|
54
|
-
event: '$ai_feedback',
|
|
55
|
-
properties: {
|
|
56
|
-
$ai_feedback_text: 'feedback',
|
|
57
|
-
$ai_trace_id: '10',
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
],
|
|
61
|
-
})
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
it('should capture a metric', async () => {
|
|
65
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
66
|
-
|
|
67
|
-
posthog.captureTraceMetric('trace-id', 'metric-name', 'good')
|
|
68
|
-
|
|
69
|
-
await waitForPromises()
|
|
70
|
-
expect(mocks.fetch).toHaveBeenCalledTimes(1)
|
|
71
|
-
const body = parseBody(mocks.fetch.mock.calls[0])
|
|
72
|
-
|
|
73
|
-
expect(body).toMatchObject({
|
|
74
|
-
batch: [
|
|
75
|
-
{
|
|
76
|
-
event: '$ai_metric',
|
|
77
|
-
properties: {
|
|
78
|
-
$ai_metric_name: 'metric-name',
|
|
79
|
-
$ai_metric_value: 'good',
|
|
80
|
-
$ai_trace_id: 'trace-id',
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
],
|
|
84
|
-
})
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
it('should convert numeric arguments in captureTraceMetric', async () => {
|
|
88
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
89
|
-
|
|
90
|
-
posthog.captureTraceMetric(10, 'metric-name', 1)
|
|
91
|
-
|
|
92
|
-
await waitForPromises()
|
|
93
|
-
expect(mocks.fetch).toHaveBeenCalledTimes(1)
|
|
94
|
-
const body = parseBody(mocks.fetch.mock.calls[0])
|
|
95
|
-
|
|
96
|
-
expect(body).toMatchObject({
|
|
97
|
-
batch: [
|
|
98
|
-
{
|
|
99
|
-
event: '$ai_metric',
|
|
100
|
-
properties: {
|
|
101
|
-
$ai_metric_name: 'metric-name',
|
|
102
|
-
$ai_metric_value: '1',
|
|
103
|
-
$ai_trace_id: '10',
|
|
104
|
-
},
|
|
105
|
-
},
|
|
106
|
-
],
|
|
107
|
-
})
|
|
108
|
-
})
|
|
109
|
-
})
|
|
110
|
-
})
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
parseBody,
|
|
3
|
-
waitForPromises,
|
|
4
|
-
createTestClient,
|
|
5
|
-
PostHogCoreTestClient,
|
|
6
|
-
PostHogCoreTestClientMocks,
|
|
7
|
-
} from '@/testing'
|
|
8
|
-
import { uuidv7 } from '@/vendor/uuidv7'
|
|
9
|
-
|
|
10
|
-
describe('PostHog Core', () => {
|
|
11
|
-
let posthog: PostHogCoreTestClient
|
|
12
|
-
let mocks: PostHogCoreTestClientMocks
|
|
13
|
-
|
|
14
|
-
jest.useFakeTimers()
|
|
15
|
-
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', { flushAt: 1 })
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
describe('capture', () => {
|
|
21
|
-
it('should capture an event', async () => {
|
|
22
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
23
|
-
|
|
24
|
-
posthog.capture('custom-event')
|
|
25
|
-
|
|
26
|
-
await waitForPromises()
|
|
27
|
-
expect(mocks.fetch).toHaveBeenCalledTimes(1)
|
|
28
|
-
const [url, options] = mocks.fetch.mock.calls[0]
|
|
29
|
-
expect(url).toMatch(/^https:\/\/us\.i\.posthog\.com\/batch\//)
|
|
30
|
-
expect(options.method).toBe('POST')
|
|
31
|
-
const body = parseBody(mocks.fetch.mock.calls[0])
|
|
32
|
-
|
|
33
|
-
expect(body).toEqual({
|
|
34
|
-
api_key: 'TEST_API_KEY',
|
|
35
|
-
batch: [
|
|
36
|
-
{
|
|
37
|
-
event: 'custom-event',
|
|
38
|
-
distinct_id: posthog.getDistinctId(),
|
|
39
|
-
library: 'posthog-core-tests',
|
|
40
|
-
library_version: '2.0.0-alpha',
|
|
41
|
-
properties: {
|
|
42
|
-
$lib: 'posthog-core-tests',
|
|
43
|
-
$lib_version: '2.0.0-alpha',
|
|
44
|
-
$session_id: expect.any(String),
|
|
45
|
-
},
|
|
46
|
-
timestamp: '2022-01-01T00:00:00.000Z',
|
|
47
|
-
uuid: expect.any(String),
|
|
48
|
-
type: 'capture',
|
|
49
|
-
},
|
|
50
|
-
],
|
|
51
|
-
sent_at: expect.any(String),
|
|
52
|
-
})
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('should allow overriding the timestamp', async () => {
|
|
56
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
57
|
-
|
|
58
|
-
posthog.capture('custom-event', { foo: 'bar' }, { timestamp: new Date('2021-01-02') })
|
|
59
|
-
await waitForPromises()
|
|
60
|
-
const body = parseBody(mocks.fetch.mock.calls[0])
|
|
61
|
-
expect(body).toMatchObject({
|
|
62
|
-
api_key: 'TEST_API_KEY',
|
|
63
|
-
batch: [
|
|
64
|
-
{
|
|
65
|
-
event: 'custom-event',
|
|
66
|
-
timestamp: '2021-01-02T00:00:00.000Z',
|
|
67
|
-
},
|
|
68
|
-
],
|
|
69
|
-
})
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('should allow overriding the uuid', async () => {
|
|
73
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
74
|
-
|
|
75
|
-
const id = uuidv7()
|
|
76
|
-
|
|
77
|
-
posthog.capture('custom-event', { foo: 'bar' }, { uuid: id })
|
|
78
|
-
await waitForPromises()
|
|
79
|
-
const body = parseBody(mocks.fetch.mock.calls[0])
|
|
80
|
-
|
|
81
|
-
expect(body).toMatchObject({
|
|
82
|
-
batch: [
|
|
83
|
-
{
|
|
84
|
-
event: 'custom-event',
|
|
85
|
-
uuid: expect.any(String),
|
|
86
|
-
},
|
|
87
|
-
],
|
|
88
|
-
})
|
|
89
|
-
})
|
|
90
|
-
})
|
|
91
|
-
})
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from '@/testing'
|
|
2
|
-
|
|
3
|
-
describe('PostHog Core', () => {
|
|
4
|
-
let posthog: PostHogCoreTestClient
|
|
5
|
-
let mocks: PostHogCoreTestClientMocks
|
|
6
|
-
|
|
7
|
-
jest.useFakeTimers()
|
|
8
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
9
|
-
|
|
10
|
-
const errorAPIResponse = Promise.resolve({
|
|
11
|
-
status: 400,
|
|
12
|
-
text: () => Promise.resolve('error'),
|
|
13
|
-
json: () =>
|
|
14
|
-
Promise.resolve({
|
|
15
|
-
status: 'error',
|
|
16
|
-
}),
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
describe('getFlags', () => {
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', { flushAt: 1 })
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
it('should handle successful v1 response and return normalized response', async () => {
|
|
25
|
-
const mockV1Response = {
|
|
26
|
-
featureFlags: { 'test-flag': true },
|
|
27
|
-
featureFlagPayloads: { 'test-flag': { a: 'payload' } },
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const expectedResponse = {
|
|
31
|
-
...mockV1Response,
|
|
32
|
-
flags: {
|
|
33
|
-
'test-flag': {
|
|
34
|
-
key: 'test-flag',
|
|
35
|
-
enabled: true,
|
|
36
|
-
variant: undefined,
|
|
37
|
-
reason: undefined,
|
|
38
|
-
metadata: {
|
|
39
|
-
id: undefined,
|
|
40
|
-
version: undefined,
|
|
41
|
-
description: undefined,
|
|
42
|
-
payload: '{"a":"payload"}',
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
mocks.fetch.mockImplementation((url) => {
|
|
49
|
-
if (url.includes('/flags/?v=2&config=true')) {
|
|
50
|
-
return Promise.resolve({
|
|
51
|
-
status: 200,
|
|
52
|
-
text: () => Promise.resolve('ok'),
|
|
53
|
-
json: () => Promise.resolve(mockV1Response),
|
|
54
|
-
})
|
|
55
|
-
}
|
|
56
|
-
return errorAPIResponse
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
const response = await posthog.getFlags('test-distinct-id')
|
|
60
|
-
expect(response).toEqual(expectedResponse)
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
it('should handle successful v4 response and return normalized response', async () => {
|
|
64
|
-
const mockV4Response = {
|
|
65
|
-
flags: {
|
|
66
|
-
'test-flag': {
|
|
67
|
-
key: 'test-flag',
|
|
68
|
-
enabled: true,
|
|
69
|
-
variant: 'test-payload',
|
|
70
|
-
reason: {
|
|
71
|
-
code: 'matched_condition',
|
|
72
|
-
description: 'matched condition set 1',
|
|
73
|
-
condition_index: 0,
|
|
74
|
-
},
|
|
75
|
-
metadata: {
|
|
76
|
-
id: 1,
|
|
77
|
-
version: 1,
|
|
78
|
-
description: 'test-flag',
|
|
79
|
-
payload: '{"a":"payload"}',
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const expectedResponse = {
|
|
86
|
-
...mockV4Response,
|
|
87
|
-
featureFlags: { 'test-flag': 'test-payload' },
|
|
88
|
-
featureFlagPayloads: { 'test-flag': { a: 'payload' } },
|
|
89
|
-
}
|
|
90
|
-
mocks.fetch.mockImplementation((url) => {
|
|
91
|
-
if (url.includes('/flags/?v=2&config=true')) {
|
|
92
|
-
return Promise.resolve({
|
|
93
|
-
status: 200,
|
|
94
|
-
text: () => Promise.resolve('ok'),
|
|
95
|
-
json: () => Promise.resolve(mockV4Response),
|
|
96
|
-
})
|
|
97
|
-
}
|
|
98
|
-
return errorAPIResponse
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
const response = await posthog.getFlags('test-distinct-id')
|
|
102
|
-
expect(response).toEqual(expectedResponse)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
it('should handle error response', async () => {
|
|
106
|
-
mocks.fetch.mockImplementation((url) => {
|
|
107
|
-
if (url.includes('/flags/?v=2&config=true')) {
|
|
108
|
-
return Promise.resolve({
|
|
109
|
-
status: 400,
|
|
110
|
-
text: () => Promise.resolve('error'),
|
|
111
|
-
json: () => Promise.resolve({ error: 'went wrong' }),
|
|
112
|
-
})
|
|
113
|
-
}
|
|
114
|
-
return errorAPIResponse
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
const response = await posthog.getFlags('test-distinct-id')
|
|
118
|
-
expect(response).toBeUndefined()
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
it('should handle network errors', async () => {
|
|
122
|
-
const emitSpy = jest.spyOn(posthog['_events'], 'emit')
|
|
123
|
-
mocks.fetch.mockImplementation((url) => {
|
|
124
|
-
if (url.includes('/flags/?v=2&config=true')) {
|
|
125
|
-
return Promise.reject(new Error('Network error'))
|
|
126
|
-
}
|
|
127
|
-
return errorAPIResponse
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
const response = await posthog.getFlags('test-distinct-id')
|
|
131
|
-
expect(response).toBeUndefined()
|
|
132
|
-
expect(emitSpy).toHaveBeenCalledWith('error', expect.any(Error))
|
|
133
|
-
})
|
|
134
|
-
})
|
|
135
|
-
})
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from '@/testing'
|
|
2
|
-
|
|
3
|
-
describe('PostHog Core', () => {
|
|
4
|
-
let posthog: PostHogCoreTestClient
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
6
|
-
let mocks: PostHogCoreTestClientMocks
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', {})
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
describe('debug', () => {
|
|
13
|
-
it('should log emitted events when enabled', () => {
|
|
14
|
-
const spy = jest.spyOn(console, 'log')
|
|
15
|
-
|
|
16
|
-
posthog.capture('test-event1')
|
|
17
|
-
expect(spy).toHaveBeenCalledTimes(0)
|
|
18
|
-
|
|
19
|
-
posthog.debug()
|
|
20
|
-
posthog.capture('test-event1')
|
|
21
|
-
expect(spy).toHaveBeenCalledTimes(1)
|
|
22
|
-
expect(spy).toHaveBeenCalledWith(
|
|
23
|
-
'PostHog Debug',
|
|
24
|
-
'capture',
|
|
25
|
-
expect.objectContaining({
|
|
26
|
-
event: 'test-event1',
|
|
27
|
-
})
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
spy.mockReset()
|
|
31
|
-
posthog.debug(false)
|
|
32
|
-
posthog.capture('test-event1')
|
|
33
|
-
expect(spy).toHaveBeenCalledTimes(0)
|
|
34
|
-
})
|
|
35
|
-
})
|
|
36
|
-
})
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { PostHogPersistedProperty } from '@/types'
|
|
2
|
-
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from '@/testing'
|
|
3
|
-
|
|
4
|
-
describe('PostHog Core', () => {
|
|
5
|
-
let posthog: PostHogCoreTestClient
|
|
6
|
-
let mocks: PostHogCoreTestClientMocks
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
function createSut(maxQueueSize: number = 1000, flushAt: number = 20): void {
|
|
13
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', {
|
|
14
|
-
maxQueueSize: maxQueueSize,
|
|
15
|
-
flushAt: flushAt,
|
|
16
|
-
})
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
describe('enqueue', () => {
|
|
20
|
-
it('should add a message to the queue', () => {
|
|
21
|
-
createSut()
|
|
22
|
-
|
|
23
|
-
posthog.capture('type', {
|
|
24
|
-
foo: 'bar',
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Queue)).toHaveLength(1)
|
|
28
|
-
|
|
29
|
-
const item = posthog.getPersistedProperty<any[]>(PostHogPersistedProperty.Queue)?.pop()
|
|
30
|
-
|
|
31
|
-
expect(item).toMatchObject({
|
|
32
|
-
message: {
|
|
33
|
-
library: 'posthog-core-tests',
|
|
34
|
-
library_version: '2.0.0-alpha',
|
|
35
|
-
type: 'capture',
|
|
36
|
-
properties: {
|
|
37
|
-
foo: 'bar',
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
expect(mocks.fetch).not.toHaveBeenCalled()
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('should delete oldest message if queue is full', () => {
|
|
46
|
-
createSut(2, 2)
|
|
47
|
-
|
|
48
|
-
posthog.capture('type1', {
|
|
49
|
-
foo: 'bar',
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
posthog.capture('type2', {
|
|
53
|
-
foo: 'bar',
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
posthog.capture('type3', {
|
|
57
|
-
foo: 'bar',
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Queue)).toHaveLength(2)
|
|
61
|
-
|
|
62
|
-
let item = posthog.getPersistedProperty<any[]>(PostHogPersistedProperty.Queue)?.pop()
|
|
63
|
-
|
|
64
|
-
expect(item).toMatchObject({
|
|
65
|
-
message: {
|
|
66
|
-
library: 'posthog-core-tests',
|
|
67
|
-
library_version: '2.0.0-alpha',
|
|
68
|
-
type: 'capture',
|
|
69
|
-
properties: {
|
|
70
|
-
foo: 'bar',
|
|
71
|
-
},
|
|
72
|
-
event: 'type3',
|
|
73
|
-
},
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
item = posthog.getPersistedProperty<any[]>(PostHogPersistedProperty.Queue)?.pop()
|
|
77
|
-
|
|
78
|
-
expect(item).toMatchObject({
|
|
79
|
-
message: {
|
|
80
|
-
library: 'posthog-core-tests',
|
|
81
|
-
library_version: '2.0.0-alpha',
|
|
82
|
-
type: 'capture',
|
|
83
|
-
properties: {
|
|
84
|
-
foo: 'bar',
|
|
85
|
-
},
|
|
86
|
-
event: 'type2',
|
|
87
|
-
},
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
expect(mocks.fetch).not.toHaveBeenCalled()
|
|
91
|
-
})
|
|
92
|
-
})
|
|
93
|
-
})
|