@posthog/core 1.2.1 → 1.2.3
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/error-tracking/parsers/base.d.ts +0 -5
- package/dist/error-tracking/parsers/base.d.ts.map +1 -1
- package/dist/error-tracking/parsers/base.js +1 -21
- package/dist/error-tracking/parsers/base.mjs +1 -6
- 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/logger.d.ts +11 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +74 -0
- package/dist/logger.mjs +37 -0
- package/dist/posthog-core-stateless.d.ts +2 -1
- package/dist/posthog-core-stateless.d.ts.map +1 -1
- package/dist/posthog-core-stateless.js +31 -29
- package/dist/posthog-core-stateless.mjs +31 -29
- package/dist/posthog-core.d.ts.map +1 -1
- package/dist/posthog-core.js +9 -9
- package/dist/posthog-core.mjs +9 -9
- package/dist/testing/test-utils.d.ts.map +1 -1
- package/dist/testing/test-utils.js +4 -6
- package/dist/testing/test-utils.mjs +4 -6
- package/dist/types.d.ts +1 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/error-tracking/parsers/base.ts +0 -5
- package/src/index.ts +0 -1
- package/src/logger.ts +58 -0
- package/src/posthog-core-stateless.ts +35 -35
- package/src/posthog-core.ts +9 -17
- package/src/testing/test-utils.ts +4 -6
- package/src/types.ts +1 -3
- 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,51 +0,0 @@
|
|
|
1
|
-
import { waitForPromises, 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
|
-
jest.useFakeTimers()
|
|
9
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', { flushAt: 10 })
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
describe('on', () => {
|
|
16
|
-
it('should listen to various events', () => {
|
|
17
|
-
const mock = jest.fn()
|
|
18
|
-
const mockOther = jest.fn()
|
|
19
|
-
posthog.on('identify', mock)
|
|
20
|
-
posthog.on('identify', mockOther)
|
|
21
|
-
|
|
22
|
-
posthog.identify('user-1')
|
|
23
|
-
expect(mock).toHaveBeenCalledTimes(1)
|
|
24
|
-
expect(mockOther).toHaveBeenCalledTimes(1)
|
|
25
|
-
expect(mock.mock.lastCall[0]).toMatchObject({ type: 'identify' })
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should unsubscribe when called', () => {
|
|
29
|
-
const mock = jest.fn()
|
|
30
|
-
const unsubscribe = posthog.on('identify', mock)
|
|
31
|
-
|
|
32
|
-
posthog.identify('user-1')
|
|
33
|
-
expect(mock).toHaveBeenCalledTimes(1)
|
|
34
|
-
posthog.identify('user-1')
|
|
35
|
-
expect(mock).toHaveBeenCalledTimes(2)
|
|
36
|
-
unsubscribe()
|
|
37
|
-
posthog.identify('user-1')
|
|
38
|
-
expect(mock).toHaveBeenCalledTimes(2)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('should subscribe to flush events', async () => {
|
|
42
|
-
const mock = jest.fn()
|
|
43
|
-
posthog.on('flush', mock)
|
|
44
|
-
posthog.capture('event')
|
|
45
|
-
expect(mock).toHaveBeenCalledTimes(0)
|
|
46
|
-
jest.runOnlyPendingTimers()
|
|
47
|
-
await waitForPromises()
|
|
48
|
-
expect(mock).toHaveBeenCalledTimes(1)
|
|
49
|
-
})
|
|
50
|
-
})
|
|
51
|
-
})
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { PostHogPersistedProperty } from '@/types'
|
|
2
|
-
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from '@/testing'
|
|
3
|
-
|
|
4
|
-
describe('PostHog Core', () => {
|
|
5
|
-
let posthog: PostHogCoreTestClient
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
|
-
let mocks: PostHogCoreTestClientMocks
|
|
8
|
-
|
|
9
|
-
const getEnrichedProperties = (): any => {
|
|
10
|
-
// NOTE: Hacky override so we can just test the props functionality
|
|
11
|
-
return (posthog as any).enrichProperties()
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', {})
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
describe('register', () => {
|
|
19
|
-
it('should register properties to storage', () => {
|
|
20
|
-
posthog.register({ foo: 'bar' })
|
|
21
|
-
expect(getEnrichedProperties()).toMatchObject({ foo: 'bar' })
|
|
22
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual({ foo: 'bar' })
|
|
23
|
-
posthog.register({ foo2: 'bar2' })
|
|
24
|
-
expect(getEnrichedProperties()).toMatchObject({ foo: 'bar', foo2: 'bar2' })
|
|
25
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual({ foo: 'bar', foo2: 'bar2' })
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should unregister properties from storage', () => {
|
|
29
|
-
posthog.register({ foo: 'bar', foo2: 'bar2' })
|
|
30
|
-
posthog.unregister('foo')
|
|
31
|
-
expect(getEnrichedProperties().foo).toBeUndefined()
|
|
32
|
-
expect(getEnrichedProperties().foo2).toEqual('bar2')
|
|
33
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual({ foo2: 'bar2' })
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
it('should register properties only for the session', () => {
|
|
37
|
-
posthog.registerForSession({ foo: 'bar' })
|
|
38
|
-
expect(getEnrichedProperties()).toMatchObject({ foo: 'bar' })
|
|
39
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual(undefined)
|
|
40
|
-
|
|
41
|
-
posthog.register({ foo: 'bar2' })
|
|
42
|
-
expect(getEnrichedProperties()).toMatchObject({ foo: 'bar' })
|
|
43
|
-
posthog.unregisterForSession('foo')
|
|
44
|
-
expect(getEnrichedProperties()).toMatchObject({ foo: 'bar2' })
|
|
45
|
-
})
|
|
46
|
-
})
|
|
47
|
-
})
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { PostHogPersistedProperty } from '@/types'
|
|
2
|
-
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from '@/testing'
|
|
3
|
-
|
|
4
|
-
describe('PostHog Core', () => {
|
|
5
|
-
let posthog: PostHogCoreTestClient
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
|
-
let mocks: PostHogCoreTestClientMocks
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', {})
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
describe('reset', () => {
|
|
14
|
-
it('should reset the storage when called', () => {
|
|
15
|
-
const distinctId = posthog.getDistinctId()
|
|
16
|
-
posthog.overrideFeatureFlag({
|
|
17
|
-
foo: 'bar',
|
|
18
|
-
})
|
|
19
|
-
posthog.register({
|
|
20
|
-
prop: 1,
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.AnonymousId)).toEqual(distinctId)
|
|
24
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags)).toEqual({ foo: 'bar' })
|
|
25
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual({ prop: 1 })
|
|
26
|
-
|
|
27
|
-
posthog.reset()
|
|
28
|
-
|
|
29
|
-
expect(posthog.getDistinctId()).not.toEqual(distinctId)
|
|
30
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags)).toEqual(undefined)
|
|
31
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual(undefined)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it("shouldn't reset the events capture queue", async () => {
|
|
35
|
-
posthog.getDistinctId()
|
|
36
|
-
posthog.capture('custom-event')
|
|
37
|
-
|
|
38
|
-
const expectedQueue = [
|
|
39
|
-
{
|
|
40
|
-
message: expect.objectContaining({
|
|
41
|
-
event: 'custom-event',
|
|
42
|
-
library: 'posthog-core-tests',
|
|
43
|
-
}),
|
|
44
|
-
},
|
|
45
|
-
]
|
|
46
|
-
|
|
47
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Queue)).toEqual(expectedQueue)
|
|
48
|
-
posthog.reset()
|
|
49
|
-
|
|
50
|
-
const newDistinctId = posthog.getDistinctId()
|
|
51
|
-
|
|
52
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Queue)).toEqual(expectedQueue)
|
|
53
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.AnonymousId)).toEqual(newDistinctId)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it('should not reset specific props when set', () => {
|
|
57
|
-
const distinctId = posthog.getDistinctId()
|
|
58
|
-
posthog.overrideFeatureFlag({
|
|
59
|
-
foo: 'bar',
|
|
60
|
-
})
|
|
61
|
-
posthog.register({
|
|
62
|
-
prop: 1,
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.AnonymousId)).toEqual(distinctId)
|
|
66
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags)).toEqual({ foo: 'bar' })
|
|
67
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual({ prop: 1 })
|
|
68
|
-
|
|
69
|
-
posthog.reset([PostHogPersistedProperty.OverrideFeatureFlags])
|
|
70
|
-
|
|
71
|
-
expect(posthog.getDistinctId()).not.toEqual(distinctId)
|
|
72
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags)).toEqual({ foo: 'bar' })
|
|
73
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.Props)).toEqual(undefined)
|
|
74
|
-
})
|
|
75
|
-
})
|
|
76
|
-
})
|
|
@@ -1,63 +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
|
-
jest.useFakeTimers()
|
|
9
|
-
jest.setSystemTime(new Date('2022-01-01T12:00:00'))
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', { flushAt: 1 })
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
describe('sessions', () => {
|
|
16
|
-
it('should create a sessionId if not set', () => {
|
|
17
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.SessionId)).toEqual(undefined)
|
|
18
|
-
posthog.capture('test')
|
|
19
|
-
expect(mocks.storage.setItem).toHaveBeenCalledWith(PostHogPersistedProperty.SessionId, expect.any(String))
|
|
20
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.SessionId)).toEqual(expect.any(String))
|
|
21
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp)).toEqual(Date.now())
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
it('should re-use existing sessionId', () => {
|
|
25
|
-
posthog.setPersistedProperty(PostHogPersistedProperty.SessionId, 'test-session-id')
|
|
26
|
-
const now = Date.now()
|
|
27
|
-
posthog.setPersistedProperty(PostHogPersistedProperty.SessionLastTimestamp, now)
|
|
28
|
-
posthog.setPersistedProperty(PostHogPersistedProperty.SessionStartTimestamp, now)
|
|
29
|
-
posthog.capture('test')
|
|
30
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.SessionId)).toEqual('test-session-id')
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('should generate new sessionId if expired', () => {
|
|
34
|
-
jest.setSystemTime(new Date('2022-01-01T12:00:00'))
|
|
35
|
-
posthog.capture('test')
|
|
36
|
-
const sessionId = posthog.getPersistedProperty(PostHogPersistedProperty.SessionId)
|
|
37
|
-
|
|
38
|
-
// Check 29 minutes later
|
|
39
|
-
jest.setSystemTime(new Date('2022-01-01T12:29:00'))
|
|
40
|
-
posthog.capture('test')
|
|
41
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.SessionId)).toEqual(sessionId)
|
|
42
|
-
|
|
43
|
-
// Check another 29 minutes later
|
|
44
|
-
jest.setSystemTime(new Date('2022-01-01T12:58:00'))
|
|
45
|
-
posthog.capture('test')
|
|
46
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.SessionId)).toEqual(sessionId)
|
|
47
|
-
|
|
48
|
-
// Check more than 30 minutes later
|
|
49
|
-
jest.setSystemTime(new Date('2022-01-01T13:30:00'))
|
|
50
|
-
posthog.capture('test')
|
|
51
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.SessionId)).not.toEqual(sessionId)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('should reset sessionId if called', () => {
|
|
55
|
-
posthog.capture('test')
|
|
56
|
-
const sessionId = posthog.getPersistedProperty(PostHogPersistedProperty.SessionId)
|
|
57
|
-
|
|
58
|
-
posthog.resetSessionId()
|
|
59
|
-
posthog.capture('test2')
|
|
60
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.SessionId)).not.toEqual(sessionId)
|
|
61
|
-
})
|
|
62
|
-
})
|
|
63
|
-
})
|
|
@@ -1,102 +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
|
-
jest.useFakeTimers()
|
|
9
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', { flushAt: 1 })
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
describe('setGroupPropertiesForFlags', () => {
|
|
16
|
-
it('should store setGroupPropertiesForFlags as persisted with group_properties key', () => {
|
|
17
|
-
const props = { organisation: { name: 'bar' }, project: { name: 'baz' } }
|
|
18
|
-
posthog.setGroupPropertiesForFlags(props)
|
|
19
|
-
|
|
20
|
-
expect(mocks.storage.setItem).toHaveBeenCalledWith('group_properties', props)
|
|
21
|
-
|
|
22
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.GroupProperties)).toEqual(props)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('should update setGroupPropertiesForFlags appropriately', () => {
|
|
26
|
-
const props = { organisation: { name: 'bar' }, project: { name: 'baz' } }
|
|
27
|
-
posthog.setGroupPropertiesForFlags(props)
|
|
28
|
-
|
|
29
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.GroupProperties)).toEqual(props)
|
|
30
|
-
|
|
31
|
-
posthog.setGroupPropertiesForFlags({ organisation: { name: 'bar2' }, project: { name2: 'baz' } })
|
|
32
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.GroupProperties)).toEqual({
|
|
33
|
-
organisation: { name: 'bar2' },
|
|
34
|
-
project: { name: 'baz', name2: 'baz' },
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
posthog.setGroupPropertiesForFlags({ organisation2: { name: 'bar' } })
|
|
38
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.GroupProperties)).toEqual({
|
|
39
|
-
organisation: { name: 'bar2' },
|
|
40
|
-
project: { name: 'baz', name2: 'baz' },
|
|
41
|
-
organisation2: { name: 'bar' },
|
|
42
|
-
})
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('should clear setGroupPropertiesForFlags on reset', () => {
|
|
46
|
-
const props = { organisation: { name: 'bar' }, project: { name: 'baz' } }
|
|
47
|
-
posthog.setGroupPropertiesForFlags(props)
|
|
48
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.GroupProperties)).toEqual(props)
|
|
49
|
-
|
|
50
|
-
posthog.reset()
|
|
51
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.GroupProperties)).toEqual(undefined)
|
|
52
|
-
|
|
53
|
-
posthog.setGroupPropertiesForFlags(props)
|
|
54
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.GroupProperties)).toEqual(props)
|
|
55
|
-
})
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
describe('setPersonPropertiesForFlags', () => {
|
|
59
|
-
it('should store setPersonPropertiesForFlags as persisted with person_properties key', () => {
|
|
60
|
-
const props = { organisation: 'bar', project: 'baz' }
|
|
61
|
-
posthog.setPersonPropertiesForFlags(props)
|
|
62
|
-
|
|
63
|
-
expect(mocks.storage.setItem).toHaveBeenCalledWith('person_properties', props)
|
|
64
|
-
|
|
65
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.PersonProperties)).toEqual(props)
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('should update setPersonPropertiesForFlags appropriately', () => {
|
|
69
|
-
const props = { organisation: 'bar', project: 'baz' }
|
|
70
|
-
posthog.setPersonPropertiesForFlags(props)
|
|
71
|
-
|
|
72
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.PersonProperties)).toEqual(props)
|
|
73
|
-
|
|
74
|
-
posthog.setPersonPropertiesForFlags({ organisation: 'bar2', project2: 'baz' })
|
|
75
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.PersonProperties)).toEqual({
|
|
76
|
-
organisation: 'bar2',
|
|
77
|
-
project: 'baz',
|
|
78
|
-
project2: 'baz',
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
posthog.setPersonPropertiesForFlags({ organisation2: 'bar' })
|
|
82
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.PersonProperties)).toEqual({
|
|
83
|
-
organisation: 'bar2',
|
|
84
|
-
project: 'baz',
|
|
85
|
-
project2: 'baz',
|
|
86
|
-
organisation2: 'bar',
|
|
87
|
-
})
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
it('should clear setPersonPropertiesForFlags on reset', () => {
|
|
91
|
-
const props = { organisation: 'bar', project: 'baz' }
|
|
92
|
-
posthog.setPersonPropertiesForFlags(props)
|
|
93
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.PersonProperties)).toEqual(props)
|
|
94
|
-
|
|
95
|
-
posthog.reset()
|
|
96
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.PersonProperties)).toEqual(undefined)
|
|
97
|
-
|
|
98
|
-
posthog.setPersonPropertiesForFlags(props)
|
|
99
|
-
expect(posthog.getPersistedProperty(PostHogPersistedProperty.PersonProperties)).toEqual(props)
|
|
100
|
-
})
|
|
101
|
-
})
|
|
102
|
-
})
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from '@/testing'
|
|
2
|
-
|
|
3
|
-
describe('PostHog Core', () => {
|
|
4
|
-
let posthog: PostHogCoreTestClient
|
|
5
|
-
let mocks: PostHogCoreTestClientMocks
|
|
6
|
-
|
|
7
|
-
describe('shutdown', () => {
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
jest.useRealTimers()
|
|
10
|
-
;[posthog, mocks] = createTestClient('TEST_API_KEY', {
|
|
11
|
-
flushAt: 10,
|
|
12
|
-
preloadFeatureFlags: false,
|
|
13
|
-
})
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
it('flush messsages once called', async () => {
|
|
17
|
-
for (let i = 0; i < 5; i++) {
|
|
18
|
-
posthog.capture('test-event')
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
await posthog.shutdown()
|
|
22
|
-
expect(mocks.fetch).toHaveBeenCalledTimes(1)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('respects timeout', async () => {
|
|
26
|
-
mocks.fetch.mockImplementation(async () => {
|
|
27
|
-
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
28
|
-
console.log('FETCH RETURNED')
|
|
29
|
-
return {
|
|
30
|
-
status: 200,
|
|
31
|
-
text: () => Promise.resolve('ok'),
|
|
32
|
-
json: () => Promise.resolve({ status: 'ok' }),
|
|
33
|
-
}
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
posthog.capture('test-event')
|
|
37
|
-
|
|
38
|
-
await posthog
|
|
39
|
-
.shutdown(100)
|
|
40
|
-
.then(() => {
|
|
41
|
-
throw new Error('Should not resolve')
|
|
42
|
-
})
|
|
43
|
-
.catch((e) => {
|
|
44
|
-
expect(e).toEqual('Timeout while shutting down PostHog. Some events may not have been sent.')
|
|
45
|
-
})
|
|
46
|
-
expect(mocks.fetch).toHaveBeenCalledTimes(1)
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('return the same promise if called multiple times in parallel', async () => {
|
|
50
|
-
mocks.fetch.mockImplementation(async () => {
|
|
51
|
-
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
52
|
-
return {
|
|
53
|
-
status: 200,
|
|
54
|
-
text: () => Promise.resolve('ok'),
|
|
55
|
-
json: () => Promise.resolve({ status: 'ok' }),
|
|
56
|
-
}
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
posthog.capture('test-event')
|
|
60
|
-
|
|
61
|
-
const p1 = posthog.shutdown(100)
|
|
62
|
-
const p2 = posthog.shutdown(100)
|
|
63
|
-
expect(p1).toEqual(p2)
|
|
64
|
-
await Promise.allSettled([p1, p2])
|
|
65
|
-
expect(mocks.fetch).toHaveBeenCalledTimes(1)
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('can handle being called multiple times in series (discouraged but some users will do this)', async () => {
|
|
69
|
-
mocks.fetch.mockImplementation(async () => {
|
|
70
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
71
|
-
console.log('FETCH RETURNED')
|
|
72
|
-
return {
|
|
73
|
-
status: 200,
|
|
74
|
-
text: () => Promise.resolve('ok'),
|
|
75
|
-
json: () => Promise.resolve({ status: 'ok' }),
|
|
76
|
-
}
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
posthog.capture('test-event')
|
|
80
|
-
await posthog.shutdown()
|
|
81
|
-
|
|
82
|
-
posthog.capture('test-event')
|
|
83
|
-
await posthog.shutdown()
|
|
84
|
-
|
|
85
|
-
expect(mocks.fetch).toHaveBeenCalledTimes(2)
|
|
86
|
-
})
|
|
87
|
-
})
|
|
88
|
-
})
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { assert, removeTrailingSlash, currentISOTime, currentTimestamp } from '@/utils'
|
|
2
|
-
|
|
3
|
-
describe('utils', () => {
|
|
4
|
-
describe('assert', () => {
|
|
5
|
-
it('should throw on falsey values', () => {
|
|
6
|
-
;[false, '', null, undefined, 0, {}, []].forEach((x) => {
|
|
7
|
-
expect(() => assert(x, 'error')).toThrow('error')
|
|
8
|
-
})
|
|
9
|
-
})
|
|
10
|
-
it('should not throw on truthy value', () => {
|
|
11
|
-
expect(() => assert('string', 'error')).not.toThrow('error')
|
|
12
|
-
})
|
|
13
|
-
})
|
|
14
|
-
describe('removeTrailingSlash', () => {
|
|
15
|
-
it('should removeSlashes', () => {
|
|
16
|
-
expect(removeTrailingSlash('me////')).toEqual('me')
|
|
17
|
-
expect(removeTrailingSlash('me/wat///')).toEqual('me/wat')
|
|
18
|
-
expect(removeTrailingSlash('me/')).toEqual('me')
|
|
19
|
-
expect(removeTrailingSlash('/me')).toEqual('/me')
|
|
20
|
-
})
|
|
21
|
-
})
|
|
22
|
-
describe.skip('retriable', () => {
|
|
23
|
-
it('should do something', () => {})
|
|
24
|
-
})
|
|
25
|
-
describe('currentTimestamp', () => {
|
|
26
|
-
it('should get the timestamp', () => {
|
|
27
|
-
expect(currentTimestamp()).toEqual(Date.now())
|
|
28
|
-
})
|
|
29
|
-
})
|
|
30
|
-
describe('currentISOTime', () => {
|
|
31
|
-
it('should get the iso time', () => {
|
|
32
|
-
jest.setSystemTime(new Date('2022-01-01'))
|
|
33
|
-
expect(currentISOTime()).toEqual('2022-01-01T00:00:00.000Z')
|
|
34
|
-
})
|
|
35
|
-
})
|
|
36
|
-
})
|