: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 { MOCK_TEAM_ID } from 'lib/api.mock'
import '@testing-library/jest-dom'
import { cleanup, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { teamLogic } from 'scenes/teamLogic'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import {
AccessControlLevel,
FeatureFlagBucketingIdentifier,
FeatureFlagEvaluationRuntime,
FeatureFlagType,
FeatureFlagWithV1Config,
} from '~/types'
import { SelectExistingFeatureFlagModal } from './SelectExistingFeatureFlagModal'
import { selectExistingFeatureFlagModalLogic } from './selectExistingFeatureFlagModalLogic'
describe('SelectExistingFeatureFlagModal', () => {
const mockOnClose = jest.fn()
const mockOnSelect = jest.fn()
let logic: ReturnType<typeof selectExistingFeatureFlagModalLogic.build>
const baseFeatureFlag: FeatureFlagWithV1Config = {
id: 1,
key: 'test-flag',
name: 'Test Flag',
filters: {
groups: [],
payloads: {},
multivariate: {
variants: [
{ key: 'control', rollout_percentage: 50 },
{ key: 'test', rollout_percentage: 50 },
],
},
},
created_at: '2021-01-01',
updated_at: '2021-01-01',
created_by: null,
is_remote_configuration: false,
deleted: false,
archived: false,
active: true,
experiment_set: null,
experiment_set_metadata: null,
features: null,
surveys: null,
can_edit: true,
tags: [],
ensure_experience_continuity: null,
user_access_level: AccessControlLevel.Admin,
status: 'ACTIVE',
has_encrypted_payloads: false,
version: 0,
last_modified_by: null,
evaluation_runtime: FeatureFlagEvaluationRuntime.ALL,
evaluation_contexts: [],
bucketing_identifier: FeatureFlagBucketingIdentifier.DISTINCT_ID,
}
const mockFeatureFlags: FeatureFlagType[] = [
baseFeatureFlag,
{
...baseFeatureFlag,
id: 2,
key: 'another-flag',
name: 'Another Flag',
filters: {
...baseFeatureFlag.filters,
multivariate: {
variants: [
{ key: 'control', rollout_percentage: 33 },
{ key: 'variant-1', rollout_percentage: 33 },
{ key: 'variant-2', rollout_percentage: 34 },
],
},
},
},
]
beforeEach(async () => {
useMocks({
get: {
[`/api/projects/${MOCK_TEAM_ID}/feature_flags/`]: () => [
200,
{
results: mockFeatureFlags,
count: mockFeatureFlags.length,
},
],
},
})
initKeaTests()
logic = selectExistingFeatureFlagModalLogic()
logic.mount()
// Wait for teamLogic to have currentProjectId ready before opening modal
await waitFor(() => {
expect(teamLogic.values.currentProjectId).toBe(MOCK_TEAM_ID)
})
logic.actions.openSelectExistingFeatureFlagModal()
// Wait for the feature flags data to be populated rather than just checking loading state.
// This avoids a race where `loadCurrentTeamSuccess` fires after the modal opens and
// triggers a second `loadFeatureFlags` call, briefly resetting loading back to true.
await waitFor(() => {
expect(logic.values.featureFlags.results.length).toBeGreaterThan(0)
expect(logic.values.featureFlagsLoading).toBe(false)
})
jest.clearAllMocks()
})
afterEach(() => {
logic.unmount()
cleanup()
})
describe('basic rendering', () => {
it('renders modal with title and description', () => {
render(<SelectExistingFeatureFlagModal onClose={mockOnClose} onSelect={mockOnSelect} />)
expect(screen.getByText('Choose an existing feature flag')).toBeInTheDocument()
expect(screen.getByText(/Select an existing multivariate feature flag/)).toBeInTheDocument()
})
it('renders search filter', () => {
render(<SelectExistingFeatureFlagModal onClose={mockOnClose} onSelect={mockOnSelect} />)
expect(screen.getByPlaceholderText('Search for feature flags')).toBeInTheDocument()
})
it('renders table columns', () => {
render(<SelectExistingFeatureFlagModal onClose={mockOnClose} onSelect={mockOnSelect} />)
expect(screen.getByText('Key')).toBeInTheDocument()
expect(screen.getByText('Name')).toBeInTheDocument()
})
})
describe('interactions', () => {
it('calls onSelect and onClose when Select button is clicked', async () => {
const resetFiltersSpy = jest.spyOn(logic.actions, 'resetFilters')
render(<SelectExistingFeatureFlagModal onClose={mockOnClose} onSelect={mockOnSelect} />)
const selectButtons = await screen.findAllByText('Select')
await userEvent.click(selectButtons[0])
expect(mockOnSelect).toHaveBeenCalledWith(mockFeatureFlags[0])
expect(resetFiltersSpy).toHaveBeenCalled()
expect(mockOnClose).toHaveBeenCalled()
resetFiltersSpy.mockRestore()
})
it('calls resetFilters and onClose when modal is closed', async () => {
const resetFiltersSpy = jest.spyOn(logic.actions, 'resetFilters')
render(<SelectExistingFeatureFlagModal onClose={mockOnClose} onSelect={mockOnSelect} />)
const closeButton = screen.getByLabelText(/close/i)
await userEvent.click(closeButton)
expect(resetFiltersSpy).toHaveBeenCalled()
expect(mockOnClose).toHaveBeenCalled()
resetFiltersSpy.mockRestore()
})
it('calls setFilters when sorting is changed', async () => {
const setFiltersSpy = jest.spyOn(logic.actions, 'setFilters')
render(<SelectExistingFeatureFlagModal onClose={mockOnClose} onSelect={mockOnSelect} />)
const keyHeader = await screen.findByText('Key')
await userEvent.click(keyHeader)
expect(setFiltersSpy).toHaveBeenCalledWith({
order: 'key',
page: 1,
})
setFiltersSpy.mockRestore()
})
})
})