:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import '@testing-library/jest-dom'
import { cleanup, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { router } from 'kea-router'
import posthog from 'posthog-js'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { featureFlagsLogic } from 'scenes/feature-flags/featureFlagsLogic'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import { ExperimentStatus, type Experiment as ExperimentType } from '~/types'
import { NEW_EXPERIMENT } from 'products/experiments/frontend/constants'
import { experimentsLogic } from '../../../../products/experiments/frontend/scenes/experimentsLogic'
import { Experiment } from './Experiment'
import { FORM_MODES } from './experimentLogic'
import { experimentSceneLogic } from './experimentSceneLogic'
jest.mock('./ExperimentWizard/ExperimentWizard', () => ({
ExperimentWizard: () => <div data-attr="experiment-wizard" />,
}))
jest.mock('lib/hooks/useFileSystemLogView', () => ({
useFileSystemLogView: () => {},
}))
jest.mock('scenes/feature-flags/JSONEditorInput', () => ({
JSONEditorInput: ({ onChange, value, placeholder, readOnly }: any) => (
<input
data-attr="json-editor-mock"
value={value || ''}
onChange={(e) => onChange?.(e.target.value)}
placeholder={placeholder}
readOnly={readOnly}
/>
),
}))
const DRAFT_EXPERIMENT: ExperimentType = {
...NEW_EXPERIMENT,
id: 123,
name: 'Draft Experiment',
type: 'product',
metrics: [],
saved_metrics: [],
start_date: null as any,
end_date: null as any,
archived: false,
}
const apiMocks = {
get: {
'/api/projects/:team/experiments': () => [200, { results: [], count: 0 }],
'/api/projects/:team/experiments/123': () => [200, DRAFT_EXPERIMENT],
'/api/projects/:team/feature_flags/': () => [200, { results: [], count: 0 }],
'/api/projects/:team/experiment_holdouts': () => [200, { results: [], count: 0 }],
'/api/projects/:team/experiment_saved_metrics': () => [200, { results: [], count: 0 }],
'/api/user_home_settings/@me/': () => [200, {}],
},
post: {
'/api/environments/:team/query/': () => [200, { results: [] }],
},
patch: {
'/api/environments/:team/add_product_intent/': () => [200, {}],
},
}
function mockApiForExperiment(experimentData?: ExperimentType): Record<string, any> {
return experimentData
? { ...apiMocks, get: { ...apiMocks.get, '/api/projects/:team/experiments/123': () => [200, experimentData] } }
: apiMocks
}
function mountKeaLogics(): void {
initKeaTests()
featureFlagLogic.mount()
featureFlagsLogic.mount()
experimentsLogic.mount()
}
function cleanupKea(...logics: Array<{ unmount: () => void }>): void {
cleanup()
for (const logic of logics) {
logic.unmount()
}
experimentsLogic.unmount()
featureFlagsLogic.unmount()
featureFlagLogic.unmount()
}
function renderExperimentViewPage(experimentData: ExperimentType): {
sceneLogic: ReturnType<typeof experimentSceneLogic>
} {
// Set the URL so urlToAction initializes correctly
router.actions.push('/experiments/123')
const sceneLogic = experimentSceneLogic({ experimentId: 123, formMode: FORM_MODES.update })
sceneLogic.mount()
sceneLogic.actions.setSceneState(123, FORM_MODES.update)
sceneLogic.values.experimentLogicRef!.logic.actions.loadExperimentSuccess(experimentData)
render(<Experiment />)
return { sceneLogic }
}
beforeAll(() => {
const modalRoot = document.createElement('div')
modalRoot.setAttribute('id', 'root')
document.body.appendChild(modalRoot)
})
describe('Experiment component', () => {
it('create mode shows wizard', async () => {
localStorage.clear()
sessionStorage.clear()
useMocks(apiMocks)
mountKeaLogics()
const createSceneLogic = experimentSceneLogic({ experimentId: 'new', formMode: FORM_MODES.create })
createSceneLogic.mount()
// Explicitly set create mode since urlToAction may override props default
createSceneLogic.actions.setSceneState('new', FORM_MODES.create)
render(<Experiment />)
expect(screen.getByTestId('experiment-wizard')).toBeInTheDocument()
cleanupKea(createSceneLogic)
})
it.each([
{
experimentOverrides: {},
description: 'draft',
expectLaunchButton: true,
expectWarningBanner: false,
},
{
experimentOverrides: {
start_date: '2024-01-01T00:00:00Z',
status: ExperimentStatus.Running,
},
description: 'running experiment',
expectLaunchButton: false,
expectWarningBanner: true,
},
])(
'$description without metrics shows add metric buttons',
async ({ experimentOverrides, expectLaunchButton, expectWarningBanner }) => {
const experimentData: ExperimentType = { ...DRAFT_EXPERIMENT, ...experimentOverrides }
localStorage.clear()
sessionStorage.clear()
useMocks(mockApiForExperiment(experimentData))
mountKeaLogics()
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const { sceneLogic } = renderExperimentViewPage(experimentData)
await waitFor(() => {
screen.getAllByText('Add primary metric')
})
screen.getAllByText('Add secondary metric')
// No creation form should be shown in view mode
expect(screen.queryByTestId('experiment-wizard')).not.toBeInTheDocument()
if (expectLaunchButton) {
const launchButton = document.querySelector('[data-attr="launch-experiment"]')
expect(launchButton).toBeInTheDocument()
// LemonButton sets aria-disabled={disabled}; React omits the attribute when false, or "false" when present
expect(launchButton?.getAttribute('aria-disabled')).not.toBe('true')
} else {
expect(document.querySelector('[data-attr="launch-experiment"]')).not.toBeInTheDocument()
}
const shownFindingCodes = captureSpy.mock.calls
.filter(([event]) => event === 'experiment health finding shown')
.map(([, properties]) => properties?.finding_code)
await userEvent.click(screen.getAllByText('Add secondary metric')[0])
await userEvent.click(screen.getAllByText('Add primary metric')[0])
const actedOn = captureSpy.mock.calls
.filter(([event]) => event === 'experiment health finding acted on')
.map(([, properties]) => [properties?.finding_code, properties?.action_kind])
if (expectWarningBanner) {
expect(screen.getByText('No metrics defined')).toBeInTheDocument()
// The fixture has no feature flag, so the flag-state banner renders too.
expect(shownFindingCodes).toEqual(expect.arrayContaining(['no_metric', 'flag_off_while_running']))
expect(actedOn).toEqual([
['no_metric', 'add_secondary_metric'],
['no_metric', 'add_primary_metric'],
])
} else {
expect(screen.queryByText('No metrics defined')).not.toBeInTheDocument()
expect(shownFindingCodes).toEqual([])
expect(actedOn).toEqual([])
}
cleanupKea(sceneLogic)
}
)
})