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.

12.7 KB
Raw Blame
import { render } from '@testing-library/react'
import { Provider } from 'kea'

import { ActivityLogRow } from 'lib/components/ActivityLog/ActivityLogRow'
import { ActivityChange, ActivityLogItem, humanize } from 'lib/components/ActivityLog/humanizeActivity'

import { initKeaTests } from '~/test/init'
import {
    ActivityScope,
    BasicSurveyQuestion,
    LinkSurveyQuestion,
    MultipleSurveyQuestion,
    RatingSurveyQuestion,
    SurveyQuestion,
    SurveyQuestionBranchingType,
    SurveyQuestionType,
} from '~/types'

import {
    describeBranchingChanges,
    describeCommonChanges,
    describeFieldChange,
    describeLinkChanges,
    describeMultipleChoiceChanges,
    describeQuestionChanges,
    describeRatingChanges,
    surveyActivityDescriber,
} from './surveyActivityDescriber'

const getTextContent = (jsxElement: JSX.Element): string => {
    const { container } = render(jsxElement)
    return container.textContent || ''
}

describe('describeFieldChange', () => {
    test('sets field with unit', () => {
        const result = describeFieldChange('wait period', null, 30, 'days')
        expect(getTextContent(result)).toBe('set wait period to 30 days')
    })

    test('removes field with unit', () => {
        const result = describeFieldChange('wait period', 30, null, 'days')
        expect(getTextContent(result)).toBe('removed wait period (was 30 days)')
    })

    test('changes field with unit', () => {
        const result = describeFieldChange('wait period', 30, 60, 'days')
        expect(getTextContent(result)).toBe('changed wait period from 30 days to 60 days')
    })

    test('sets field without unit', () => {
        const result = describeFieldChange('response limit', null, 100)
        expect(getTextContent(result)).toBe('set response limit to 100')
    })

    test('removes field without unit', () => {
        const result = describeFieldChange('response limit', 100, null)
        expect(getTextContent(result)).toBe('removed response limit (was 100)')
    })

    test('changes field without unit', () => {
        const result = describeFieldChange('response limit', 100, 200)
        expect(getTextContent(result)).toBe('changed response limit from 100 to 200')
    })

    test('handles undefined before value', () => {
        const result = describeFieldChange('iteration count', undefined, 5)
        expect(getTextContent(result)).toBe('set iteration count to 5')
    })

    test('handles undefined after value', () => {
        const result = describeFieldChange('iteration count', 5, undefined)
        expect(getTextContent(result)).toBe('removed iteration count (was 5)')
    })

    test('handles empty string before value', () => {
        const result = describeFieldChange('survey title', '', 'New Title')
        expect(getTextContent(result)).toBe('set survey title to New Title')
    })

    test('handles empty string after value', () => {
        const result = describeFieldChange('survey title', 'Old Title', '')
        expect(getTextContent(result)).toBe('removed survey title (was Old Title)')
    })

    test('handles both values as empty strings', () => {
        const result = describeFieldChange('survey title', '', '')
        expect(getTextContent(result)).toBe('')
    })

    test('handles before and after as identical', () => {
        const result = describeFieldChange('response limit', 100, 100)
        expect(getTextContent(result)).toBe('')
    })

    test('handles string values with unit', () => {
        const result = describeFieldChange('response time', 'fast', 'slow', 'seconds')
        expect(getTextContent(result)).toBe('changed response time from fast seconds to slow seconds')
    })

    test('handles boolean values', () => {
        const result = describeFieldChange('is active', false, true)
        expect(getTextContent(result)).toBe('changed is active from false to true')
    })

    test('handles null values', () => {
        const result = describeFieldChange('response limit', null, null)
        expect(getTextContent(result)).toBe('')
    })
})

describe('describeCommonChanges', () => {
    const before: SurveyQuestion = {
        question: 'What is your favorite color?',
        description: 'Choose a color',
        type: SurveyQuestionType.SingleChoice,
        optional: false,
        buttonText: 'Next',
        choices: ['Red', 'Blue', 'Green'],
    }
    const after: SurveyQuestion = {
        ...before,
        question: 'What is your favorite animal?',
        description: 'Choose an animal',
        optional: true,
        buttonText: 'Continue',
    }

    test('describes common changes', () => {
        const changes = describeCommonChanges(before, after)
        expect(changes).toHaveLength(4)
        expect(getTextContent(changes[0])).toBe(
            'changed question text from "What is your favorite color?" to "What is your favorite animal?"'
        )
        expect(getTextContent(changes[1])).toBe(
            'changed the question description from "Choose a color" to "Choose an animal"'
        )
        expect(getTextContent(changes[2])).toBe('made question optional')
        expect(getTextContent(changes[3])).toBe('changed button text from "Next" to "Continue"')
    })
})

describe('describeLinkChanges', () => {
    const before: LinkSurveyQuestion = {
        question: 'Visit our website',
        type: SurveyQuestionType.Link,
        link: 'http://example.com',
    }
    const after: LinkSurveyQuestion = {
        ...before,
        link: 'http://example.org',
    }

    test('describes link changes', () => {
        const changes = describeLinkChanges([before, after])
        expect(changes).toHaveLength(1)
        expect(getTextContent(changes[0])).toBe('updated link from http://example.com to http://example.org')
    })
})

describe('describeRatingChanges', () => {
    const before: RatingSurveyQuestion = {
        question: 'Rate our service',
        type: SurveyQuestionType.Rating,
        display: 'emoji',
        scale: 5,
        lowerBoundLabel: 'Poor',
        upperBoundLabel: 'Excellent',
    }
    const after: RatingSurveyQuestion = {
        ...before,
        display: 'number',
        scale: 10,
        lowerBoundLabel: 'Bad',
        upperBoundLabel: 'Good',
    }

    test('describes rating changes', () => {
        const changes = describeRatingChanges([before, after])
        expect(changes).toHaveLength(3)
        expect(getTextContent(changes[0])).toBe('changed rating display from emoji to number')
        expect(getTextContent(changes[1])).toBe('changed rating scale from 5 to 10')
        expect(getTextContent(changes[2])).toBe('updated rating labels from "Poor"-"Excellent" to "Bad"-"Good"')
    })
})

describe('describeMultipleChoiceChanges', () => {
    const before: MultipleSurveyQuestion = {
        question: 'Select your hobbies',
        type: SurveyQuestionType.MultipleChoice,
        choices: ['Reading', 'Traveling', 'Cooking'],
        shuffleOptions: false,
        hasOpenChoice: false,
    }
    const after: MultipleSurveyQuestion = {
        ...before,
        choices: ['Reading', 'Cooking', 'Gaming'],
        shuffleOptions: true,
        hasOpenChoice: true,
    }

    test('describes multiple choice changes', () => {
        const changes = describeMultipleChoiceChanges([before, after])
        expect(changes).toHaveLength(4)
        expect(getTextContent(changes[0])).toBe('added choices: Gaming')
        expect(getTextContent(changes[1])).toBe('removed choices: Traveling')
        expect(getTextContent(changes[2])).toBe('enabled option shuffling')
        expect(getTextContent(changes[3])).toBe('added open choice option')
    })
})

describe('describeBranchingChanges', () => {
    const before: MultipleSurveyQuestion = {
        question: 'Do you like ice cream?',
        type: SurveyQuestionType.SingleChoice,
        choices: ['Yes', 'No'],
        branching: {
            type: SurveyQuestionBranchingType.NextQuestion,
        },
    }
    const after: MultipleSurveyQuestion = {
        ...before,
        branching: {
            type: SurveyQuestionBranchingType.End,
        },
    }

    test('describes branching changes', () => {
        const changes = describeBranchingChanges(before, after)
        expect(changes).toHaveLength(1)
        expect(getTextContent(changes[0])).toBe('updated branching logic')
    })
})

describe('describeQuestionChanges', () => {
    const before: MultipleSurveyQuestion = {
        question: 'Do you like ice cream?',
        type: SurveyQuestionType.SingleChoice,
        description: 'Please answer honestly',
        optional: false,
        buttonText: 'Next',
        choices: ['Yes', 'No'],
        branching: {
            type: SurveyQuestionBranchingType.NextQuestion,
        },
    }
    const after: MultipleSurveyQuestion = {
        question: 'Do you like pizza?',
        type: SurveyQuestionType.MultipleChoice,
        description: 'Please answer honestly',
        optional: true,
        buttonText: 'Continue',
        choices: ['Yes', 'No', 'Maybe'],
        branching: {
            type: SurveyQuestionBranchingType.End,
        },
    }
    test('describes all changes in a question', () => {
        const changes = describeQuestionChanges(before, after)
        expect(changes).toHaveLength(6)
        expect(getTextContent(changes[0])).toBe(
            'changed question text from "Do you like ice cream?" to "Do you like pizza?"'
        )
        expect(getTextContent(changes[1])).toBe('made question optional')
        expect(getTextContent(changes[2])).toBe('changed button text from "Next" to "Continue"')
        expect(getTextContent(changes[3])).toBe(
            'changed question type from Single choice select to Multiple choice select'
        )
        expect(getTextContent(changes[4])).toBe('added choices: Maybe')
        expect(getTextContent(changes[5])).toBe('updated branching logic')
    })
})

describe('surveyActivityDescriber', () => {
    const question: BasicSurveyQuestion = {
        question: 'What do you think?',
        type: SurveyQuestionType.Open,
    }

    const questionsChange = (before: ActivityChange['before'], after: ActivityChange['after']): ActivityLogItem => ({
        activity: 'updated',
        created_at: '2026-01-01T00:00:00Z',
        scope: ActivityScope.SURVEY,
        item_id: '1',
        user: { first_name: 'Alice', last_name: 'Adams', email: '[email protected]' },
        detail: {
            name: 'Example survey',
            changes: [{ type: ActivityScope.SURVEY, action: 'changed', field: 'questions', before, after }],
            merge: null,
            trigger: null,
        },
    })

    test.each([
        ['the questions list was empty before', null, [question], 'from 0 to 1'],
        ['the questions list is empty after', [question], null, 'from 1 to 0'],
    ])('describes the question count when %s', (_, before, after, expected) => {
        const { description } = surveyActivityDescriber(questionsChange(before, after))
        expect(getTextContent(<>{description}</>)).toContain(expected)
    })

    test.each([
        [
            'its schedule ended',
            { job_type: 'survey_schedule_ended', job_id: '1', payload: {} },
            'Example survey after its last scheduled repeat',
        ],
        [
            'it reached its response limit',
            { job_type: 'survey_responses_limit_reached', job_id: '1', payload: { responses_limit: 300 } },
            'Example survey after reaching its limit of 300 responses',
        ],
        ['a person stopped it', null, 'stoppedExample survey'],
    ])('says why the survey stopped when %s', (_, trigger, expected) => {
        initKeaTests()
        // Activity rows render the summary, so assert on the row rather than the description alone.
        const [humanized] = humanize(
            [
                {
                    activity: 'updated',
                    created_at: '2026-01-01T00:00:00Z',
                    scope: ActivityScope.SURVEY,
                    item_id: '1',
                    is_system: true,
                    detail: {
                        name: 'Example survey',
                        changes: [
                            {
                                type: ActivityScope.SURVEY,
                                action: 'created',
                                field: 'end_date',
                                before: null,
                                after: '2026-01-01T00:00:00Z',
                            },
                        ],
                        merge: null,
                        trigger,
                    },
                },
            ],
            () => surveyActivityDescriber
        )
        const row = getTextContent(
            <Provider>
                <ActivityLogRow logItem={humanized} />
            </Provider>
        )
        expect(row.replace(/\s+/g, ' ')).toMatch(new RegExp(`${expected}$`))
    })
})