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.

8.2 KB
Raw Blame
import { Meta, StoryObj } from '@storybook/react'
import { delay, HttpResponse } from 'msw'

import { App } from 'scenes/App'
import { createInsightStory } from 'scenes/insights/__mocks__/createInsightScene'

import { useStorybookMocks } from '~/mocks/browser'

import insight from '../../../mocks/fixtures/api/projects/team_id/insights/trendsLine.json'
import funnelOneStep from './funnelOneStep.json'

type Story = StoryObj<{}>
const meta: Meta = {
    component: App,
    title: 'Scenes-App/Insights/Error & Empty States',
    parameters: {
        layout: 'fullscreen',
        viewMode: 'story',
        pageUrl: `/insights/${insight.short_id}`,
        testOptions: {
            waitForSelector: '[data-attr="insight-empty-state"]',
        },
    },
}
export default meta

export const Empty: Story = {
    render: () => {
        useStorybookMocks({
            get: {
                '/api/environments/:team_id/insights/': async () => {
                    await delay(100)
                    return HttpResponse.json({ count: 1, results: [{ ...insight, result: [] }] })
                },
            },
        })

        return <App />
    },
}

export const ServerError: Story = {
    render: () => {
        useStorybookMocks({
            get: {
                '/api/environments/:team_id/insights/': async () => {
                    await delay(100)
                    return HttpResponse.json({ count: 1, results: [{ ...insight, result: null }] })
                },
                '/api/environments/:team_id/insights/:id': async () => {
                    await delay(100)
                    return HttpResponse.json(
                        {
                            type: 'server_error',
                            detail: 'There is nothing you can do to stop the impending catastrophe.',
                        },
                        { status: 500 }
                    )
                },
            },
            post: {
                // The query path must fail like the legacy endpoints above — otherwise the
                // default query mock succeeds and renders a freshness bar over the error state.
                '/api/environments/:team_id/query/:kind/': async () => {
                    await delay(100)
                    return HttpResponse.json(
                        {
                            type: 'server_error',
                            detail: 'There is nothing you can do to stop the impending catastrophe.',
                        },
                        { status: 500 }
                    )
                },
            },
        })

        return <App />
    },
}

export const QueryServerError: Story = {
    render: () => {
        useStorybookMocks({
            get: {
                '/api/environments/:team_id/insights/': async () => {
                    await delay(100)
                    return HttpResponse.json({
                        count: 1,
                        results: [insight],
                    })
                },
            },
            post: {
                '/api/environments/:team_id/query/:kind/': async () => {
                    await delay(100)
                    return HttpResponse.json(
                        {
                            type: 'server_error',
                            detail: 'There is nothing you can do to stop the impending catastrophe.',
                        },
                        { status: 500 }
                    )
                },
            },
        })

        return <App />
    },
    parameters: {
        testOptions: {
            waitForSelector: '[data-attr="insight-retry-button"]',
        },
    },
}

export const ValidationError: Story = {
    render: () => {
        useStorybookMocks({
            get: {
                '/api/environments/:team_id/insights/': async () => {
                    await delay(100)
                    return HttpResponse.json({ count: 1, results: [{ ...insight, result: null }] })
                },
            },
            post: {
                '/api/environments/:team_id/insights/:id': async () => {
                    await delay(100)
                    return HttpResponse.json(
                        {
                            type: 'validation_error',
                            detail: 'You forgot to hug the person next to you. Please do that now.',
                        },
                        { status: 400 }
                    )
                },
                // Fail the query path too, so the default query mock doesn't succeed and
                // render a freshness bar over the error state.
                '/api/environments/:team_id/query/:kind/': async () => {
                    await delay(100)
                    return HttpResponse.json(
                        {
                            type: 'validation_error',
                            detail: 'You forgot to hug the person next to you. Please do that now.',
                        },
                        { status: 400 }
                    )
                },
            },
        })

        return <App />
    },
}

export const MemoryLimitExceeded: Story = {
    render: () => {
        useStorybookMocks({
            get: {
                '/api/environments/:team_id/insights/': () => [
                    200,
                    { count: 1, results: [{ ...insight, result: null }] },
                ],
            },
            post: {
                '/api/environments/:team_id/query/:kind/': async () => {
                    await delay(100)
                    return HttpResponse.json(
                        {
                            type: 'server_error',
                            code: 'clickhouse_memory_limit_exceeded',
                            detail: "This query ran out of memory before it could finish, usually because it's scanning too much data. Try a shorter date range or narrower filters, or see our docs for more ways to speed it up: https://posthog.com/docs/product-analytics/troubleshooting#how-do-i-speed-up-my-insights-and-queries",
                        },
                        { status: 513 }
                    )
                },
            },
        })

        return <App />
    },
    parameters: {
        testOptions: {
            waitForLoadersToDisappear: false,
            waitForSelector: '[data-attr=insight-memory-limit-debug-with-ai]',
        },
    },
}

export const EstimatedQueryExecutionTimeTooLong: Story = {
    render: () => {
        useStorybookMocks({
            get: {
                '/api/environments/:team_id/insights/': () => [
                    200,
                    { count: 1, results: [{ ...insight, result: null }] },
                ],
            },
            post: {
                '/api/environments/:team_id/query/:kind/': async () => {
                    await delay(100)
                    return HttpResponse.json(
                        {
                            type: 'server_error',
                            detail: 'Estimated query execution time is too long. Try reducing its scope by changing the time range.',
                        },
                        { status: 512 }
                    )
                },
            },
        })

        return <App />
    },
    parameters: {
        testOptions: {
            waitForLoadersToDisappear: false,
            waitForSelector: '[data-attr=insight-loading-too-long]',
        },
    },
}

export const LongLoading: Story = {
    render: () => {
        useStorybookMocks({
            get: {
                '/api/environments/:team_id/insights/': () => [
                    200,
                    { count: 1, results: [{ ...insight, result: null }] },
                ],
            },
            post: {
                '/api/environments/:team_id/query/:kind/': async () => {
                    await delay('infinite')
                    return HttpResponse.json({})
                },
            },
        })

        return <App />
    },
    parameters: {
        testOptions: {
            waitForLoadersToDisappear: false,
            waitForSelector: '[data-attr=insight-loading-waiting-message]',
        },
    },
}

export const FunnelSingleStep: Story = createInsightStory(funnelOneStep as any)