Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reacting to config changes #1138

Merged
merged 17 commits into from
Apr 17, 2024
92 changes: 4 additions & 88 deletions src/__tests__/decide.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Decide } from '../decide'
import { PostHogPersistence } from '../posthog-persistence'
import { RequestRouter } from '../utils/request-router'
import { checkScriptsForSrc } from './helpers/script-utils'


const expectDecodedSendRequest = (send_request, data, noCompression) => {
const lastCall = send_request.mock.calls[send_request.mock.calls.length - 1]
Expand All @@ -19,24 +21,6 @@ const expectDecodedSendRequest = (send_request, data, noCompression) => {
})
}

const checkScriptsForSrc = (src, negate = false) => {
const scripts = document.querySelectorAll('body > script')
let foundScript = false
for (let i = 0; i < scripts.length; i++) {
if (scripts[i].src === src) {
foundScript = true
break
}
}

if (foundScript && negate) {
throw new Error(`Script with src ${src} was found when it should not have been.`)
} else if (!foundScript && !negate) {
throw new Error(`Script with src ${src} was not found when it should have been.`)
} else {
return true
}
}

describe('Decide', () => {
given('decide', () => new Decide(given.posthog))
Expand All @@ -51,16 +35,6 @@ describe('Decide', () => {
_afterDecideResponse: jest.fn(),
get_distinct_id: jest.fn().mockImplementation(() => 'distinctid'),
_send_request: jest.fn().mockImplementation(({ callback }) => callback?.({ config: given.decideResponse })),
toolbar: {
maybeLoadToolbar: jest.fn(),
afterDecideResponse: jest.fn(),
},
sessionRecording: {
afterDecideResponse: jest.fn(),
},
autocapture: {
afterDecideResponse: jest.fn(),
},
featureFlags: {
receivedFeatureFlags: jest.fn(),
setReloadingPaused: jest.fn(),
Expand Down Expand Up @@ -197,11 +171,8 @@ describe('Decide', () => {
given('decideResponse', () => ({}))
given.subject()

expect(given.posthog.sessionRecording.afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
expect(given.posthog.toolbar.afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
expect(given.posthog.featureFlags.receivedFeatureFlags).toHaveBeenCalledWith(given.decideResponse, false)
expect(given.posthog._afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
expect(given.posthog.autocapture.afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
})

it('Make sure receivedFeatureFlags is called with errors if the decide response fails', () => {
Expand All @@ -228,10 +199,7 @@ describe('Decide', () => {

given.subject()

expect(given.posthog.autocapture.afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
expect(given.posthog.sessionRecording.afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
expect(given.posthog.toolbar.afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)

expect(given.posthog._afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
expect(given.posthog.featureFlags.receivedFeatureFlags).not.toHaveBeenCalled()
})

Expand All @@ -248,10 +216,7 @@ describe('Decide', () => {

given.subject()

expect(given.posthog.autocapture.afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
expect(given.posthog.sessionRecording.afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
expect(given.posthog.toolbar.afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)

expect(given.posthog._afterDecideResponse).toHaveBeenCalledWith(given.decideResponse)
expect(given.posthog.featureFlags.receivedFeatureFlags).not.toHaveBeenCalled()
})

Expand All @@ -274,54 +239,5 @@ describe('Decide', () => {
)
expect(checkScriptsForSrc('https://test.com/site_app/1/tokentoken/hash/', true)).toBe(true)
})

it('Make sure surveys are not loaded when decide response says no', () => {
given('decideResponse', () => ({
featureFlags: { 'test-flag': true },
surveys: false,
}))
given('config', () => ({
api_host: 'https://test.com',
token: 'testtoken',
persistence: 'memory',
}))

given.subject()
// Make sure the script is not loaded
expect(checkScriptsForSrc('https://test.com/static/surveys.js', true)).toBe(true)
})

it('Make sure surveys are loaded when decide response says so', () => {
given('decideResponse', () => ({
featureFlags: { 'test-flag': true },
surveys: true,
}))
given('config', () => ({
api_host: 'https://test.com',
token: 'testtoken',
persistence: 'memory',
}))

given.subject()
// Make sure the script is loaded
expect(checkScriptsForSrc('https://test.com/static/surveys.js')).toBe(true)
})

it('Make sure surveys are not loaded when config says no', () => {
given('decideResponse', () => ({
featureFlags: { 'test-flag': true },
surveys: true,
}))
given('config', () => ({
api_host: 'https://test.com',
token: 'testtoken',
persistence: 'memory',
disable_surveys: true,
}))

given.subject()
// Make sure the script is not loaded
expect(checkScriptsForSrc('https://test.com/static/surveys.js', true)).toBe(true)
})
})
})
Loading
Loading