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.

17.9 KB
Raw Blame
import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'

import { EXPERIMENT_TARGET_SELECTOR } from 'lib/utils/actions'

jest.mock('lib/lemon-ui/LemonToast/LemonToast', () => ({
    lemonToast: { success: jest.fn(), error: jest.fn() },
}))

// The failure-path tests intentionally exercise toolbarLogger, which logs to the console by design
jest.mock('~/toolbar/shared/toolbarLogger', () => ({
    toolbarLogger: { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() },
}))

import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
import { toolbarLogic } from '~/toolbar/shared/bar/toolbarLogic'
import { toolbarConfigLogic } from '~/toolbar/shared/toolbarConfigLogic'

import { experimentsLogic } from './experimentsLogic'
import { experimentsTabLogic } from './experimentsTabLogic'

// The toolbar logger mirrors intentional error/auth paths to the console (its job on
// customer pages); tests exercise those paths on purpose, so stub the boundary.
jest.mock('~/toolbar/shared/toolbarLogger', () => ({
    toolbarLogger: { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() },
}))

const web_experiments = [
    {
        id: 1,
        name: 'Test Experiment 1',
        variants: {
            control: {
                transforms: [],
            },
        },
    },
    {
        id: 2,
        name: 'Test Experiment 2',
        variants: {
            control: {
                transforms: [],
            },
            test: {
                transforms: [
                    {
                        html: '<b> Hello world! </b>',
                        selector: 'h1',
                    },
                ],
            },
            test2: {
                transforms: [
                    {
                        html: '<b> Goodbye world! </b>',
                        selector: 'h1',
                    },
                ],
            },
        },
    },
]

// The toolbar calls `global.fetch` directly (not the app api client / MSW). Reassign the mock per
// test in beforeEach — the MSW jest harness installs its own `global.fetch` in a global beforeAll,
// which runs after this module loads and would otherwise clobber a top-level assignment.
const installFetchMock = (): void => {
    global.fetch = jest.fn(() =>
        Promise.resolve({
            ok: true,
            json: () => Promise.resolve({ results: web_experiments }),
        } as any as Response)
    )
}
describe('experimentsTabLogic', () => {
    // A new variant starts scoped to the page it was created on, which is the test page here.
    const currentPageConditions = { url: 'http://localhost/project/997', urlMatchType: 'icontains' }
    let theExperimentsTabLogic: ReturnType<typeof experimentsTabLogic.build>
    let theExperimentsLogic: ReturnType<typeof experimentsLogic.build>
    let theToolbarLogic: ReturnType<typeof toolbarLogic.build>
    let theToolbarConfigLogic: ReturnType<typeof toolbarConfigLogic.build>

    beforeEach(() => {
        installFetchMock()
        const { lemonToast } = jest.requireMock('lib/lemon-ui/LemonToast/LemonToast')
        ;(lemonToast.success as jest.Mock).mockClear()
        ;(lemonToast.error as jest.Mock).mockClear()
        useMocks({
            get: {
                '/api/projects/:team/web_experiments/': () => web_experiments,
            },
            post: {
                '/api/projects/@current/web_experiments/': () => ({
                    id: 3,
                    name: 'New Web Experiment',
                }),
            },
            patch: {
                '/api/projects/@current/web_experiments/1': () => ({
                    id: 3,
                    name: 'Updated web experiment',
                }),
            },
        })
        initKeaTests()

        theToolbarConfigLogic = toolbarConfigLogic.build({ apiURL: 'http://localhost', accessToken: 'test-token' })
        theToolbarConfigLogic.mount()

        theToolbarLogic = toolbarLogic()
        theToolbarLogic.mount()

        theExperimentsLogic = experimentsLogic()
        theExperimentsLogic.mount()

        theExperimentsTabLogic = experimentsTabLogic()
        theExperimentsTabLogic.mount()
    })

    describe('core assumptions', () => {
        it('mounts other logics', async () => {
            await expectLogic(theExperimentsTabLogic).toMount([router, experimentsLogic, toolbarConfigLogic])
        })
    })

    describe('selecting experiments', () => {
        it('can select an experiment', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment(1)
            })
                .toMatchValues({
                    selectedExperimentId: 1,
                })
                .toDispatchActions(['selectExperiment'])
        })

        it('can deselect an experiment', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment(null)
            })
                .toMatchValues({
                    selectedExperimentId: null,
                })
                .toDispatchActions(['selectExperiment'])
        })

        it('does not restore a previous experiment snapshot after the page changed', async () => {
            const heading = document.createElement('h1')
            heading.textContent = 'Pricing page'
            document.body.appendChild(heading)
            try {
                await expectLogic(theExperimentsLogic).toDispatchActions(['getExperimentsSuccess'])
                theExperimentsTabLogic.actions.selectExperiment(2)
                expect(theExperimentsTabLogic.values.experimentForm.original_html_state).toEqual({
                    h1: { html: 'Pricing page' },
                })
                theExperimentsTabLogic.actions.selectExperiment(null)
                heading.textContent = 'Blog page'

                theExperimentsTabLogic.actions.selectExperiment(1)

                expect(heading.textContent).toEqual('Blog page')
            } finally {
                heading.remove()
            }
        })

        it('can create a new experiment', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment('new')
            })
                .toMatchValues({
                    experimentForm: {
                        name: '',
                        variants: {
                            control: {
                                transforms: [],
                                rollout_percentage: 50,
                            },
                            test: {
                                is_new: true,
                                transforms: [{}],
                                conditions: currentPageConditions,
                                rollout_percentage: 50,
                            },
                        },
                        original_html_state: {},
                    },
                })
                .toDispatchActions(['selectExperiment'])
        })

        it('can add a new variant', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment(1)
                theExperimentsTabLogic.actions.addNewVariant()
            })
                .toMatchValues({
                    experimentForm: {
                        name: 'Test Experiment 1',
                        variants: {
                            control: {
                                transforms: [],
                                rollout_percentage: 50,
                            },
                            'test-0': {
                                transforms: [{}],
                                rollout_percentage: 50,
                                is_new: true,
                                conditions: currentPageConditions,
                            },
                        },
                        original_html_state: {},
                    },
                })
                .toDispatchActions(['selectExperiment', 'addNewVariant'])
        })

        it('can remove an existing variant', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment(1)
                theExperimentsTabLogic.actions.removeVariant('test-0')
            })
                .toMatchValues({
                    experimentForm: {
                        name: 'Test Experiment 1',
                        variants: {
                            control: {
                                transforms: [],
                                rollout_percentage: 100,
                            },
                        },
                        original_html_state: {},
                    },
                })
                .toDispatchActions(['selectExperiment'])
        })
    })

    describe('creating experiments', () => {
        it('can create a new experiment', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.newExperiment()
                theExperimentsTabLogic.actions.setExperimentFormValue('name', 'New Test Experiment')
                theExperimentsTabLogic.actions.submitExperimentForm()
            })
                .toMatchValues({
                    experimentForm: {
                        name: 'New Test Experiment',
                        variants: {
                            control: {
                                transforms: [],
                                rollout_percentage: 50,
                            },
                            test: {
                                is_new: true,
                                transforms: [{}],
                                conditions: currentPageConditions,
                                rollout_percentage: 50,
                            },
                        },
                        original_html_state: {},
                    },
                })
                .toDispatchActions(['newExperiment', 'setExperimentFormValue', 'submitExperimentForm'])
        })
    })

    describe('selecting html elements', () => {
        it('can highlight all elements on a page', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment(1)
                theExperimentsTabLogic.actions.inspectForElementWithIndex('test', 'all-elements', 0)
            })
                .toDispatchActions(['selectExperiment', 'inspectForElementWithIndex'])
                .toMatchValues({
                    elementSelector: EXPERIMENT_TARGET_SELECTOR,
                })
        })

        it('can highlight only images on a page', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment(1)
                theExperimentsTabLogic.actions.inspectForElementWithIndex('test', 'images', 0)
            })
                .toDispatchActions(['selectExperiment', 'inspectForElementWithIndex'])
                .toMatchValues({
                    elementSelector: 'img',
                })
        })

        it('can highlight only buttons on a page', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment(1)
                theExperimentsTabLogic.actions.inspectForElementWithIndex('test', 'buttons', 0)
            })
                .toDispatchActions(['selectExperiment', 'inspectForElementWithIndex'])
                .toMatchValues({
                    elementSelector: 'input[type="button"],button',
                })
        })

        it('can highlight only headers on a page', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment(1)
                theExperimentsTabLogic.actions.inspectForElementWithIndex('test', 'headers', 0)
            })
                .toDispatchActions(['selectExperiment', 'inspectForElementWithIndex'])
                .toMatchValues({
                    elementSelector: 'h1,h2,h3,h4,h5,h6',
                })
        })
    })

    describe('form submission error handling', () => {
        const savedFetch = global.fetch

        afterEach(() => {
            global.fetch = savedFetch
        })

        it('shows error toast when API returns error with detail', async () => {
            const { lemonToast } = jest.requireMock('lib/lemon-ui/LemonToast/LemonToast')

            global.fetch = jest.fn(() =>
                Promise.resolve({
                    ok: false,
                    status: 400,
                    json: () => Promise.resolve({ detail: 'Invalid experiment config' }),
                } as any as Response)
            )

            theExperimentsTabLogic.actions.newExperiment()
            theExperimentsTabLogic.actions.setExperimentFormValue('name', 'Bad Experiment')

            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.submitExperimentForm()
            })
                .toDispatchActions(['submitExperimentFormFailure'])
                .toNotHaveDispatchedActions(['submitExperimentFormSuccess'])

            expect(lemonToast.error).toHaveBeenCalledWith('Invalid experiment config')
        })

        it('shows generic error when API returns error and json parsing fails', async () => {
            const { lemonToast } = jest.requireMock('lib/lemon-ui/LemonToast/LemonToast')

            global.fetch = jest.fn(() =>
                Promise.resolve({
                    ok: false,
                    status: 500,
                    json: () => Promise.reject(new Error('parse error')),
                } as any as Response)
            )

            theExperimentsTabLogic.actions.newExperiment()
            theExperimentsTabLogic.actions.setExperimentFormValue('name', 'Server Error Experiment')

            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.submitExperimentForm()
            }).delay(0)

            expect(lemonToast.error).toHaveBeenCalledWith('Failed to save experiment')
        })

        it('handles network error gracefully', async () => {
            const { lemonToast } = jest.requireMock('lib/lemon-ui/LemonToast/LemonToast')

            global.fetch = jest.fn(() => Promise.reject(new Error('Network error')))

            theExperimentsTabLogic.actions.newExperiment()
            theExperimentsTabLogic.actions.setExperimentFormValue('name', 'Network Error Experiment')

            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.submitExperimentForm()
            }).delay(0)

            expect(lemonToast.error).toHaveBeenCalledWith('Failed to save experiment')
        })
    })

    describe('editing experiments', () => {
        it('can edit an existing experiment', async () => {
            await expectLogic(theExperimentsTabLogic, () => {
                theExperimentsTabLogic.actions.selectExperiment(1)
                theExperimentsTabLogic.actions.setExperimentFormValue('name', 'Updated Experiment 1')
                theExperimentsTabLogic.actions.submitExperimentForm()
            })
                .toDispatchActions(['selectExperiment', 'setExperimentFormValue', 'submitExperimentForm'])
                .toMatchValues({
                    experimentForm: {
                        name: 'Updated Experiment 1',
                        variants: {
                            control: {
                                transforms: [],
                                rollout_percentage: 100,
                            },
                        },
                        original_html_state: {},
                    },
                })
        })

        it('can apply changes from a variant', async () => {
            await expectLogic(theExperimentsLogic, () => {
                theExperimentsLogic.actions.getExperiments()
            })
                .delay(0)
                .then(() => {
                    theExperimentsTabLogic.actions.selectExperiment(2)
                    theExperimentsTabLogic.actions.applyVariant('test')
                })
        })

        it('puts the page back when the user leaves a previewed experiment', async () => {
            document.body.innerHTML = '<h1>Original headline</h1>'
            await expectLogic(theExperimentsLogic, () => {
                theExperimentsLogic.actions.getExperiments()
            }).toDispatchActions(['getExperimentsSuccess'])

            theExperimentsTabLogic.actions.selectExperiment(2)
            theExperimentsTabLogic.actions.applyVariant('test')
            expect(document.querySelector('h1')?.innerHTML).toEqual('<b> Hello world! </b>')

            theExperimentsTabLogic.actions.selectExperiment(null)
            expect(document.querySelector('h1')?.innerHTML).toEqual('Original headline')
        })

        it('keeps a new experiment form when the experiment list reloads', async () => {
            theExperimentsTabLogic.actions.newExperiment()
            theExperimentsTabLogic.actions.setExperimentFormValue('name', 'Shorter headline')

            await expectLogic(theExperimentsLogic, () => {
                theExperimentsLogic.actions.getExperiments()
            }).toDispatchActions(['getExperimentsSuccess'])

            expect(theExperimentsTabLogic.values.experimentForm.name).toEqual('Shorter headline')
        })

        it('can switch between variants', async () => {
            await expectLogic(theExperimentsLogic, () => {
                theExperimentsLogic.actions.getExperiments()
            })
                .delay(0)
                .then(() => {
                    theExperimentsTabLogic.actions.selectExperiment(2)
                    theExperimentsTabLogic.actions.applyVariant('test')
                    theExperimentsTabLogic.actions.applyVariant('test2')
                })
        })
    })
})