posthog-js-lite 2.3.0 → 2.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-js-lite",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "main": "lib/index.cjs.js",
5
5
  "module": "lib/index.esm.js",
6
6
  "types": "lib/index.d.ts",
@@ -21,6 +21,10 @@ export class PostHog extends PostHogCore {
21
21
  this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`
22
22
  this._storage = getStorage(options?.persistence || 'localStorage', window)
23
23
  this.setupBootstrap(options)
24
+
25
+ if (options?.preloadFeatureFlags !== false) {
26
+ this.reloadFeatureFlags()
27
+ }
24
28
  }
25
29
 
26
30
  getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
@@ -6,27 +6,89 @@ import { PostHog } from '..'
6
6
 
7
7
  describe('PosthogWeb', () => {
8
8
  let fetch: jest.Mock
9
- jest.useFakeTimers()
9
+ jest.useRealTimers()
10
10
 
11
11
  beforeEach(() => {
12
- ;(global as any).window.fetch = fetch = jest.fn(() =>
13
- Promise.resolve({
12
+ ;(global as any).window.fetch = fetch = jest.fn(async (url) => {
13
+ let res: any = { status: 'ok' }
14
+ if (url.includes('decide')) {
15
+ res = {
16
+ featureFlags: {
17
+ 'feature-1': true,
18
+ 'feature-2': true,
19
+ 'json-payload': true,
20
+ 'feature-variant': 'variant',
21
+ },
22
+ // NOTE: Aren't these supposed to be strings?
23
+
24
+ featureFlagPayloads: {
25
+ 'feature-1': {
26
+ color: 'blue',
27
+ },
28
+ 'json-payload': {
29
+ a: 'payload',
30
+ },
31
+ 'feature-variant': 5,
32
+ },
33
+ }
34
+ }
35
+
36
+ return {
14
37
  status: 200,
15
- json: () => Promise.resolve({ status: 'ok' }),
16
- })
17
- ) as any
38
+ json: () => Promise.resolve(res),
39
+ }
40
+ })
18
41
  })
19
42
 
20
43
  describe('init', () => {
21
44
  it('should initialise', () => {
22
- const postHog = new PostHog('TEST_API_KEY', {
45
+ const posthog = new PostHog('TEST_API_KEY', {
46
+ flushAt: 1,
47
+ })
48
+ expect(posthog.optedOut).toEqual(false)
49
+
50
+ posthog.capture('test')
51
+
52
+ expect(fetch).toHaveBeenCalledTimes(2)
53
+ })
54
+
55
+ it('should load feature flags on init', async () => {
56
+ const posthog = new PostHog('TEST_API_KEY', {
23
57
  flushAt: 1,
24
58
  })
25
- expect(postHog.optedOut).toEqual(false)
26
59
 
27
- postHog.capture('test')
60
+ expect(fetch).toHaveBeenCalledWith('https://app.posthog.com/decide/?v=3', {
61
+ body: JSON.stringify({
62
+ token: 'TEST_API_KEY',
63
+ distinct_id: posthog.getDistinctId(),
64
+ groups: {},
65
+ person_properties: {},
66
+ group_properties: {},
67
+ $anon_distinct_id: posthog.getAnonymousId(),
68
+ }),
69
+ method: 'POST',
70
+ headers: {
71
+ 'Content-Type': 'application/json',
72
+ },
73
+ signal: expect.anything(),
74
+ })
28
75
 
29
- expect(fetch).toHaveBeenCalledTimes(1)
76
+ expect(posthog.getFeatureFlags()).toEqual({
77
+ 'feature-1': true,
78
+ 'feature-2': true,
79
+ 'json-payload': true,
80
+ 'feature-variant': 'variant',
81
+ })
82
+
83
+ expect(posthog.getFeatureFlagPayloads()).toEqual({
84
+ 'feature-1': {
85
+ color: 'blue',
86
+ },
87
+ 'json-payload': {
88
+ a: 'payload',
89
+ },
90
+ 'feature-variant': 5,
91
+ })
30
92
  })
31
93
  })
32
94
  })