Skip to content

Commit

Permalink
feat: hog site functions (#1546)
Browse files Browse the repository at this point in the history
* site functions

* test fixes

* more tests

* fix some break others

* tests

* random comments
  • Loading branch information
mariusandra authored Nov 27, 2024
1 parent 26a8abc commit 3644681
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 58 deletions.
31 changes: 0 additions & 31 deletions src/__tests__/decide.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Decide } from '../decide'
import { PostHogPersistence } from '../posthog-persistence'
import { RequestRouter } from '../utils/request-router'
import { expectScriptToExist, expectScriptToNotExist } from './helpers/script-utils'
import { PostHog } from '../posthog-core'
import { DecideResponse, PostHogConfig, Properties } from '../types'
import '../entrypoints/external-scripts-loader'
Expand Down Expand Up @@ -246,35 +245,5 @@ describe('Decide', () => {
expect(posthog._afterDecideResponse).toHaveBeenCalledWith(decideResponse)
expect(posthog.featureFlags.receivedFeatureFlags).not.toHaveBeenCalled()
})

it('runs site apps if opted in', () => {
posthog.config = {
api_host: 'https://test.com',
opt_in_site_apps: true,
persistence: 'memory',
} as PostHogConfig

subject({ siteApps: [{ id: 1, url: '/site_app/1/tokentoken/hash/' }] } as DecideResponse)

expectScriptToExist('https://test.com/site_app/1/tokentoken/hash/')
})

it('does not run site apps code if not opted in', () => {
;(window as any).POSTHOG_DEBUG = true
// don't technically need to run this but this test assumes opt_in_site_apps is false, let's make that explicit
posthog.config = {
api_host: 'https://test.com',
opt_in_site_apps: false,
persistence: 'memory',
} as unknown as PostHogConfig

subject({ siteApps: [{ id: 1, url: '/site_app/1/tokentoken/hash/' }] } as DecideResponse)

expect(console.error).toHaveBeenCalledWith(
'[PostHog.js]',
'PostHog site apps are disabled. Enable the "opt_in_site_apps" config to proceed.'
)
expectScriptToNotExist('https://test.com/site_app/1/tokentoken/hash/')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ describe('LazyLoadedDeadClicksAutocapture', () => {
$dead_click_selection_changed_timeout: true,
$ce_version: 1,
$el_text: 'text',
$elements_chain: 'body:text="text"nth-child="2"nth-of-type="1"',
$elements: [
{
$el_text: 'text',
Expand Down Expand Up @@ -270,6 +271,7 @@ describe('LazyLoadedDeadClicksAutocapture', () => {
$dead_click_selection_changed_timeout: false,
$ce_version: 1,
$el_text: 'text',
$elements_chain: 'body:text="text"nth-child="2"nth-of-type="1"',
$elements: [
{
$el_text: 'text',
Expand Down Expand Up @@ -312,6 +314,7 @@ describe('LazyLoadedDeadClicksAutocapture', () => {
$dead_click_selection_changed_timeout: false,
$ce_version: 1,
$el_text: 'text',
$elements_chain: 'body:text="text"nth-child="2"nth-of-type="1"',
$elements: [
{
$el_text: 'text',
Expand Down Expand Up @@ -354,6 +357,7 @@ describe('LazyLoadedDeadClicksAutocapture', () => {
$dead_click_selection_changed_timeout: false,
$ce_version: 1,
$el_text: 'text',
$elements_chain: 'body:text="text"nth-child="2"nth-of-type="1"',
$elements: [
{
$el_text: 'text',
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/posthog-persistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ describe('persistence', () => {
it('should migrate data from cookies to localStorage', () => {
const lib = new PostHogPersistence(makePostHogConfig('bla', 'cookie'))
lib.register_once({ distinct_id: 'testy', test_prop: 'test_value' }, undefined, undefined)
expect(document.cookie).toEqual(
expect(document.cookie).toContain(
'ph__posthog=%7B%22distinct_id%22%3A%22testy%22%2C%22test_prop%22%3A%22test_value%22%7D'
)
const lib2 = new PostHogPersistence(makePostHogConfig('bla', 'localStorage+cookie'))
expect(document.cookie).toEqual('ph__posthog=%7B%22distinct_id%22%3A%22testy%22%7D')
expect(document.cookie).toContain('ph__posthog=%7B%22distinct_id%22%3A%22testy%22%7D')
lib2.register({ test_prop2: 'test_val', distinct_id: 'test2' })
expect(document.cookie).toEqual('ph__posthog=%7B%22distinct_id%22%3A%22test2%22%7D')
expect(document.cookie).toContain('ph__posthog=%7B%22distinct_id%22%3A%22test2%22%7D')
expect(lib2.props).toEqual({ distinct_id: 'test2', test_prop: 'test_value', test_prop2: 'test_val' })
lib2.remove()
expect(localStorage.getItem('ph__posthog')).toEqual(null)
Expand Down
Loading

0 comments on commit 3644681

Please sign in to comment.