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.

6.8 KB
Raw Blame
import '@testing-library/jest-dom'

import { act } from '@testing-library/react'
import * as React from 'react'
import { createRoot } from 'react-dom/client'

jest.mock('~/styles', () => ({}))

function deferred<T>(): { promise: Promise<T>; resolve: (value: T) => void } {
    let resolve!: (value: T) => void
    const promise = new Promise<T>((resolvePromise) => {
        resolve = resolvePromise
    })
    return { promise, resolve }
}

describe('app entry boot', () => {
    let modulesLoaded: string[]
    let bootApp: jest.Mock
    let stylesheet: ReturnType<typeof deferred<boolean>>
    let documentListeners: jest.SpyInstance<void, Parameters<typeof document.addEventListener>>

    beforeEach(() => {
        jest.resetModules()
        jest.useFakeTimers()
        modulesLoaded = []
        bootApp = jest.fn()
        stylesheet = deferred<boolean>()
        window.ESBUILD_CSS_READY = stylesheet.promise
        document.body.innerHTML = '<div id="root"></div>'
        window.__posthogAppRoot = createRoot(document.getElementById('root')!)
        jest.spyOn(document, 'readyState', 'get').mockReturnValue('complete')
        documentListeners = jest.spyOn(document, 'addEventListener')
        jest.doMock('react', () => React)
        jest.doMock('react-dom/client', () => ({ createRoot }))
        jest.doMock('scenes/App', () => {
            modulesLoaded.push('App')
            return {
                App: (): JSX.Element => {
                    if (bootApp.mock.calls.length !== 1) {
                        throw new Error('App rendered without one-time boot')
                    }
                    return <div data-attr="boot-test-app" />
                },
            }
        })
        jest.doMock('scenes/bootApp', () => {
            modulesLoaded.push('bootApp')
            return { bootApp }
        })
    })

    afterEach(async () => {
        for (const [type, listener, options] of documentListeners.mock.calls) {
            if (type === 'DOMContentLoaded') {
                document.removeEventListener(type, listener, options)
            }
        }
        await act(async () => window.__posthogAppRoot?.unmount())
        delete window.__posthogAppRoot
        delete window.ESBUILD_CSS_READY
        delete window.POSTHOG_APP_CONTEXT_REQUEST
        document.body.innerHTML = ''
        jest.clearAllTimers()
        jest.useRealTimers()
        jest.restoreAllMocks()
    })

    async function loadEntry(): Promise<void> {
        await act(async () => {
            await jest.isolateModulesAsync(async () => {
                await import('./index')
            })
        })
    }

    it.each(['complete', 'loading'] as const)(
        'loads modules while CSS is pending after the document is %s',
        async (readyState) => {
            const documentState = jest.spyOn(document, 'readyState', 'get').mockReturnValue(readyState)
            await loadEntry()

            expect(modulesLoaded).toEqual(readyState === 'complete' ? ['App', 'bootApp'] : [])
            if (readyState === 'loading') {
                documentState.mockReturnValue('complete')
                await act(async () => {
                    document.dispatchEvent(new Event('DOMContentLoaded'))
                })
            }

            expect(modulesLoaded).toEqual(['App', 'bootApp'])
            expect(bootApp).not.toHaveBeenCalled()
            expect(document.getElementById('root')).toBeEmptyDOMElement()

            await act(async () => stylesheet.resolve(true))

            expect(document.querySelector('[data-attr="boot-test-app"]')).toBeInTheDocument()
            expect(bootApp).toHaveBeenCalledTimes(1)
        }
    )

    it('loads modules while the user data is pending and boots once it arrives', async () => {
        const userData = deferred<{ app_context: object; feature_flags: null }>()
        window.POSTHOG_APP_CONTEXT_REQUEST = userData.promise
        await loadEntry()
        await act(async () => stylesheet.resolve(true))

        expect(modulesLoaded).toEqual(['App', 'bootApp'])
        expect(bootApp).not.toHaveBeenCalled()

        await act(async () => userData.resolve({ app_context: {}, feature_flags: null }))

        expect(document.querySelector('[data-attr="boot-test-app"]')).toBeInTheDocument()
        expect(bootApp).toHaveBeenCalledTimes(1)
    })

    it.each(['loaded', 'absent'] as const)('boots when the stylesheet is %s', async (stylesheetState) => {
        if (stylesheetState === 'absent') {
            delete window.ESBUILD_CSS_READY
        }
        await loadEntry()

        await act(async () => {
            if (stylesheetState === 'loaded') {
                stylesheet.resolve(true)
            }
        })

        expect(document.querySelector('[data-attr="boot-test-app"]')).toBeInTheDocument()
        expect(bootApp).toHaveBeenCalledTimes(1)
    })

    it.each([
        ['within the render gate', 0],
        ['after a stall let the app render', 6000],
    ])('offers a reload instead of an unstyled app when every stylesheet URL fails %s', async (_case, stallMs) => {
        await loadEntry()
        await act(async () => {
            await jest.advanceTimersByTimeAsync(stallMs)
        })

        await act(async () => stylesheet.resolve(false))

        expect(document.querySelector('[role="alert"]')).toHaveTextContent('PostHog failed to load its styles.')
        expect(document.querySelector('[data-attr="boot-test-app"]')).not.toBeInTheDocument()
    })

    it('boots after five seconds if the stylesheet stays pending', async () => {
        await loadEntry()
        await act(async () => {
            await jest.advanceTimersByTimeAsync(4999)
        })
        expect(bootApp).not.toHaveBeenCalled()
        expect(document.getElementById('root')).toBeEmptyDOMElement()

        await act(async () => {
            await jest.advanceTimersByTimeAsync(1)
        })
        expect(document.querySelector('[data-attr="boot-test-app"]')).toBeInTheDocument()
        expect(bootApp).toHaveBeenCalledTimes(1)
    })

    it.each(['scenes/App', 'scenes/bootApp'])(
        'shows an early %s failure through the boot error boundary after CSS is ready',
        async (failingModule) => {
            jest.spyOn(console, 'error').mockImplementation(() => {})
            const error = new Error('Boot dependency failed')
            jest.doMock(failingModule, () => {
                throw error
            })

            await loadEntry()
            await act(async () => {
                await jest.advanceTimersByTimeAsync(0)
            })
            expect(document.getElementById('root')).toBeEmptyDOMElement()
            expect(bootApp).not.toHaveBeenCalled()

            await act(async () => stylesheet.resolve(true))

            expect(document.querySelector('[role="alert"]')).toHaveTextContent('PostHog crashed while starting.')
            expect(bootApp).not.toHaveBeenCalled()
        }
    )
})