: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 { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import {
createDefaultTooltipAccessor,
type DefaultTooltipAccessor,
findChart,
getChartTooltip,
waitForChartTooltip,
} from '@posthog/quill-charts/testing'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { TrendsQuery } from '~/queries/schema/schema-general'
import { InsightLogicProps } from '~/types'
import { trendsDataLogic } from 'products/product_analytics/frontend/insights/trends/trendsDataLogic'
import { IndexedTrendResult } from 'products/product_analytics/frontend/insights/trends/types'
import { INSIGHT_TEST_ID } from './render-insight'
import { type InsightTooltipAccessor, createInsightTooltipAccessor } from './tooltip-helpers'
const DEBOUNCE_TIMEOUT = 3000
function getLogic(): ReturnType<typeof insightVizDataLogic.build> {
const props: InsightLogicProps = { dashboardItemId: INSIGHT_TEST_ID }
return insightVizDataLogic(props)
}
async function clickSelect(dataAttr: string, optionText: string | RegExp): Promise<void> {
const trigger = screen.getByTestId(dataAttr)
await userEvent.click(trigger)
const name = typeof optionText === 'string' ? new RegExp(`^${optionText}`) : optionText
const options = screen.getAllByRole('menuitem', { name })
await userEvent.click(options[0])
}
export async function searchAndSelect(triggerAttr: string, searchText: string, resultAttr: string): Promise<void> {
await userEvent.click(screen.getByTestId(triggerAttr))
const searchInput = await screen.findByTestId('taxonomic-filter-searchfield')
await userEvent.clear(searchInput)
await userEvent.type(searchInput, searchText)
await waitFor(
() => {
const el = screen.getByTestId(resultAttr)
expect(el.textContent?.toLowerCase()).toContain(searchText.toLowerCase())
},
{ timeout: DEBOUNCE_TIMEOUT }
)
await userEvent.click(screen.getByTestId(resultAttr))
}
export const series = {
async select(eventName: string, index = 0): Promise<void> {
await searchAndSelect(`trend-element-subject-${index}`, eventName, 'prop-filter-events-0')
await waitFor(() => expect((getQuerySource().series[index] as { event?: string }).event).toBe(eventName), {
timeout: DEBOUNCE_TIMEOUT,
})
},
}
export const breakdown = {
async set(propertyName: string): Promise<void> {
await searchAndSelect('add-breakdown-button', propertyName, 'prop-filter-event_properties-0')
await waitFor(
() => {
const bf = getQuerySource().breakdownFilter
expect(bf?.breakdowns?.[0]?.property ?? bf?.breakdown).toBe(propertyName)
},
{ timeout: DEBOUNCE_TIMEOUT }
)
},
}
export const interval = {
async set(value: 'minute' | 'hour' | 'day' | 'week' | 'month'): Promise<void> {
await clickSelect('interval-filter', value)
await waitFor(() => expect(getQuerySource().interval).toBe(value), { timeout: DEBOUNCE_TIMEOUT })
},
}
export const display = {
async set(optionText: string): Promise<void> {
const before = getQuerySource().trendsFilter?.display
await clickSelect('chart-filter', optionText)
await waitFor(
() => {
const after = getQuerySource().trendsFilter?.display
expect(after).toBeTruthy()
expect(after).not.toBe(before)
},
{ timeout: DEBOUNCE_TIMEOUT }
)
},
}
export const legend = {
/** Toggle a series' hidden state by matching its label. Drives `toggleResultHidden`
* on trendsDataLogic so the chart's getTrendsHidden updates as if the user had
* clicked the series in the legend / insights table. */
async toggle(label: string): Promise<void> {
const props: InsightLogicProps = { dashboardItemId: INSIGHT_TEST_ID }
const logic = trendsDataLogic(props)
const dataset = (logic.values.indexedResults as IndexedTrendResult[]).find((d) => d.label === label)
if (!dataset) {
const available = (logic.values.indexedResults as IndexedTrendResult[])
.map((d) => `"${d.label}"`)
.join(', ')
throw new Error(`No series labeled "${label}". Available: ${available}`)
}
const before = logic.values.getTrendsHidden(dataset)
logic.actions.toggleResultHidden(dataset)
// updateInsightFilter has a 300ms debounce; grant headroom to let the
// resulting querySource update propagate back to getTrendsHidden.
await waitFor(
() => {
expect(logic.values.getTrendsHidden(dataset)).toBe(!before)
},
{ timeout: DEBOUNCE_TIMEOUT }
)
},
}
export const compare = {
async enable(): Promise<void> {
await clickSelect('compare-filter', 'Compare to previous period')
await waitFor(() => expect(getQuerySource().compareFilter?.compare).toBe(true), { timeout: DEBOUNCE_TIMEOUT })
},
}
export function getQuerySource(): TrendsQuery {
return getLogic().values.querySource as TrendsQuery
}
export const chart = {
/** Current chart tooltip element, or null if none is rendered. */
getTooltip: getChartTooltip,
async hoverTooltip(index: number): Promise<InsightTooltipAccessor> {
const tooltip = await (await findChart()).hoverTooltip(index)
return createInsightTooltipAccessor(tooltip.element)
},
async clickAtIndex(index: number): Promise<void> {
await (await findChart()).clickAtIndex(index)
},
/** Click a row inside the pinned tooltip by matching its label text. Use
* after `clickAtIndex` has pinned a multi-series tooltip. */
async clickTooltipRow(label: string): Promise<void> {
createDefaultTooltipAccessor(await waitForChartTooltip()).clickRow(label)
},
}
/** Interactions for SQL (`DataVisualizationNode`) charts, which render quill's `DefaultTooltip`
* instead of the InsightTooltip table. */
export const sqlChart = {
async hoverTooltip(index: number): Promise<DefaultTooltipAccessor> {
return await (await findChart()).hoverTooltip(index)
},
}