Skip to content

: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.

fix(cohorts): reset negation when switching criterion type (#73353)

Luke Baberb5f888d21027
diff --git a/frontend/src/scenes/cohorts/CohortEdit.test.tsx b/frontend/src/scenes/cohorts/CohortEdit.test.tsx
index 0d928fbd456c..457c712c5ecd 100644
--- a/frontend/src/scenes/cohorts/CohortEdit.test.tsx
+++ b/frontend/src/scenes/cohorts/CohortEdit.test.tsx
@@ -1,16 +1,18 @@
import '@testing-library/jest-dom'
-import { cleanup, render, screen } from '@testing-library/react'
+import { cleanup, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { expectLogic, partial } from 'kea-test-utils'
import { cohortEditLogic } from 'scenes/cohorts/cohortEditLogic'
import { NEW_COHORT } from 'scenes/cohorts/CohortFilters/constants'
+import { BehavioralFilterKey } from 'scenes/cohorts/CohortFilters/types'
import { toPaginatedResponse } from '~/mocks/handlers'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import { mockCohort } from '~/test/mocks'
+import { AnyCohortCriteriaType, BehavioralEventType } from '~/types'
import { CohortEdit } from './CohortEdit'
@@ -491,6 +493,78 @@ describe('cohortEditLogic', () => {
)
})
+ describe('criteria row type switching', () => {
+ afterEach(() => {
+ cleanup()
+ })
+
+ const q = (selector: string): HTMLElement => {
+ const el = document.querySelector(selector)
+ if (!el) {
+ throw new Error(`not found: ${selector}`)
+ }
+ return el as HTMLElement
+ }
+
+ // mockCohort's single criterion is negated ("Did not complete event", stored as
+ // {value: performed_event, negation: true}). Negated criteria store the positive enum
+ // plus a negation flag, so a type pick that doesn't reset negation used to leave rows
+ // permanently stuck on the negated variant (e.g. "Do not have the property").
+ test.each([
+ {
+ pick: 'cohort-personPropertyBehavioral-have_property-type',
+ expectedLabel: 'Have the property',
+ expectedCriteria: {
+ type: BehavioralFilterKey.Person,
+ value: BehavioralEventType.HaveProperty,
+ negation: false,
+ },
+ },
+ {
+ pick: 'cohort-eventBehavioral-performed_event-type',
+ expectedLabel: 'Completed event',
+ expectedCriteria: {
+ type: BehavioralFilterKey.Behavioral,
+ value: BehavioralEventType.PerformEvent,
+ negation: false,
+ },
+ },
+ {
+ pick: 'cohort-personPropertyBehavioral-not_have_property-type',
+ expectedLabel: 'Do not have the property',
+ expectedCriteria: {
+ type: BehavioralFilterKey.Person,
+ value: BehavioralEventType.HaveProperty,
+ negation: true,
+ },
+ },
+ ])(
+ 'switching a negated row via $pick lands on $expectedLabel',
+ async ({ pick, expectedLabel, expectedCriteria }) => {
+ render(<CohortEdit id={1} />)
+
+ await waitFor(() => {
+ expect(q('[data-attr="cohort-selector-field-value"]')).toHaveTextContent('Did not complete event')
+ })
+
+ await userEvent.click(q('[data-attr="cohort-selector-field-value"]'))
+ await waitFor(() => {
+ expect(q(`[data-attr="${pick}"]`)).toBeInTheDocument()
+ })
+ await userEvent.click(q(`[data-attr="${pick}"]`))
+
+ logic = cohortEditLogic({ id: 1 })
+ await waitFor(() => {
+ const group = logic.values.cohort.filters.properties.values[0] as {
+ values: AnyCohortCriteriaType[]
+ }
+ expect(group.values[0]).toEqual(expect.objectContaining(expectedCriteria))
+ })
+ expect(q('[data-attr="cohort-selector-field-value"]')).toHaveTextContent(expectedLabel)
+ }
+ )
+ })
+
describe('locked type and populate-from controls on existing cohorts', () => {
afterEach(() => {
cleanup()
diff --git a/frontend/src/scenes/cohorts/CohortFilters/CohortField.tsx b/frontend/src/scenes/cohorts/CohortFilters/CohortField.tsx
index 30b84bd8df62..f852d033fced 100644
--- a/frontend/src/scenes/cohorts/CohortFilters/CohortField.tsx
+++ b/frontend/src/scenes/cohorts/CohortFilters/CohortField.tsx
@@ -18,6 +18,7 @@ import { formatDate } from 'lib/utils/datetime'
import { cohortFieldLogic } from 'scenes/cohorts/CohortFilters/cohortFieldLogic'
import {
BehavioralFilterKey,
+ BehavioralFilterType,
CohortEventFiltersFieldProps,
CohortFieldBaseProps,
CohortNumberFieldProps,
@@ -28,9 +29,11 @@ import {
CohortTextFieldProps,
FieldOptionsType,
} from 'scenes/cohorts/CohortFilters/types'
+import { determineFilterType } from 'scenes/cohorts/cohortUtils'
import { propertyDefinitionsModel } from '~/models/propertyDefinitionsModel'
import {
+ AnyCohortCriteriaType,
AnyPropertyFilter,
PropertyDefinitionType,
PropertyFilterType,
@@ -85,7 +88,21 @@ export function CohortSelectorField({
<LemonButton
key={_value}
onClick={() => {
- onChange({ [fieldKey]: _value })
+ if (fieldKey === 'value') {
+ // Criterion-type picks must set `negation` explicitly: negated
+ // criteria are stored as their positive counterpart plus
+ // `negation: true` (see determineFilterType), so merging
+ // `{ value }` alone can leave a stale flag — switching a
+ // "Do not have the property" row to "Have the property"
+ // would otherwise be a no-op.
+ const { negation } = determineFilterType(
+ criteria.type as BehavioralFilterKey,
+ _value as BehavioralFilterType
+ )
+ onChange({ value: _value, negation } as AnyCohortCriteriaType)
+ } else {
+ onChange({ [fieldKey]: _value })
+ }
}}
active={_value == value}
fullWidth