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.

2.2 KB
Raw Blame
import { shouldWarnBeforeLeavingNotebook } from './notebookBeforeUnload'

describe('shouldWarnBeforeLeavingNotebook', () => {
    const baseInput = {
        isLocalOnly: false,
        isEditable: true,
        syncStatus: 'unsaved' as const,
        hasMountedEditor: true,
        currentPathname: '/project/1/notebooks/abc',
    }

    it.each([
        ['unsaved', 'unsaved' as const, true],
        ['saving (in flight, including 409 retry loop)', 'saving' as const, true],
        ['synced', 'synced' as const, false],
        ['local', 'local' as const, false],
    ])('warns when syncStatus is %s -> %s', (_label, syncStatus, expected) => {
        expect(shouldWarnBeforeLeavingNotebook({ ...baseInput, syncStatus })).toBe(expected)
    })

    it('does not warn for local-only notebooks (scratchpad / canvas / template)', () => {
        expect(shouldWarnBeforeLeavingNotebook({ ...baseInput, isLocalOnly: true })).toBe(false)
    })

    it('does not warn when the notebook is not editable (viewer access, history preview, shared/exported view)', () => {
        expect(shouldWarnBeforeLeavingNotebook({ ...baseInput, isEditable: false })).toBe(false)
    })

    it('does not warn for in-page URL updates (same pathname)', () => {
        expect(
            shouldWarnBeforeLeavingNotebook({
                ...baseInput,
                newPathname: '/project/1/notebooks/abc',
            })
        ).toBe(false)
    })

    it('warns when navigating to a different pathname', () => {
        expect(
            shouldWarnBeforeLeavingNotebook({
                ...baseInput,
                newPathname: '/project/1/dashboard',
            })
        ).toBe(true)
    })

    it('warns when no newPathname is provided (browser tab close)', () => {
        expect(shouldWarnBeforeLeavingNotebook(baseInput)).toBe(true)
    })

    it.each([
        [false, '/project/1/dashboard', false],
        [true, '/project/1/dashboard', true],
        [false, undefined, true],
    ])('guards navigation with mounted editor %s to %s: %s', (hasMountedEditor, newPathname, expected) => {
        expect(shouldWarnBeforeLeavingNotebook({ ...baseInput, hasMountedEditor, newPathname })).toBe(expected)
    })
})