Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Dec 3, 2024
1 parent e8ed9f2 commit a49302d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions src/__tests__/web-experiments.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebExperiments } from '../web-experiments'
import { PostHog } from '../posthog-core'
import { DecideResponse, PostHogConfig } from '../types'
import { PostHogConfig } from '../types'
import { PostHogPersistence } from '../posthog-persistence'
import { WebExperiment } from '../web-experiments-types'
import { RequestRouter } from '../utils/request-router'
Expand Down Expand Up @@ -111,7 +111,10 @@ describe('Web Experimentation', () => {
},
} as unknown as WebExperiment

const simulateFeatureFlags: jest.Mock = jest.fn()

beforeEach(() => {
let cachedFlags = {}
persistence = { props: {}, register: jest.fn() } as unknown as PostHogPersistence
posthog = makePostHog({
config: {
Expand All @@ -129,6 +132,14 @@ describe('Web Experimentation', () => {
.mockImplementation(({ callback }) => callback({ statusCode: 200, json: experimentsResponse })),
consent: { isOptedOut: () => true } as unknown as ConsentManager,
onFeatureFlags: jest.fn(),
getFeatureFlag: (key: string) => {
return cachedFlags[key]
},
})

simulateFeatureFlags.mockImplementation((flags) => {
cachedFlags = flags
webExperiment.onFeatureFlags(Object.keys(flags))
})

posthog.requestRouter = new RequestRouter(posthog)
Expand Down Expand Up @@ -171,11 +182,10 @@ describe('Web Experimentation', () => {
function assertElementChanged(variant: string, expectedProperty: string, value: string) {
const elParent = createTestDocument()
webExperiment = new WebExperiments(posthog)
webExperiment.onRemoteConfig({
featureFlags: {
'signup-button-test': variant,
},
} as unknown as DecideResponse)

simulateFeatureFlags({
'signup-button-test': variant,
})

switch (expectedProperty) {
case 'css':
Expand All @@ -201,11 +211,10 @@ describe('Web Experimentation', () => {
webExperiment._is_bot = () => true
const elParent = createTestDocument()

webExperiment.onRemoteConfig({
featureFlags: {
'signup-button-test': 'Sign me up',
},
} as unknown as DecideResponse)
simulateFeatureFlags({
'signup-button-test': 'Sign me up',
})

expect(elParent.innerText).toEqual('original')
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/web-experiments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class WebExperiments {
this._flagToExperiments?.set(webExperiment.feature_flag_key, webExperiment)
}

const selectedVariant = this.instance.featureFlags.getFeatureFlag(webExperiment.feature_flag_key)
const selectedVariant = this.instance.getFeatureFlag(webExperiment.feature_flag_key)
if (isString(selectedVariant) && webExperiment.variants[selectedVariant]) {
this.applyTransforms(
webExperiment.name,
Expand Down

0 comments on commit a49302d

Please sign in to comment.