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.3 KB
Raw Blame
import {
    isLibraryType,
    libraryListHref,
    libraryObjectHref,
    libraryObjectName,
    libraryTypeForPath,
} from './libraryUtils'

describe('libraryUtils', () => {
    test.each([
        ['insight', false],
        ['feature_flag', true],
        ['dashboard', false],
        ['notebook', false],
        ['task', false],
    ])('lists %s in Library: %s', (type, listed) => {
        expect(isLibraryType(type as string)).toBe(listed)
    })
    test.each([
        ['Unfiled/Insights/Checkout funnel', 'Checkout funnel'],
        ['Unfiled/Insights/Signups a\\/b test', 'Signups a/b test'],
        ['Unfiled/Dashboards/go\\\\\\/revenue', 'go\\/revenue'],
        ['Unfiled/Folder\\\\/Revenue', 'Revenue'],
        ['Top level', 'Top level'],
    ])('names %s as %s', (path, name) => {
        expect(libraryObjectName({ path })).toBe(name)
    })

    test.each([
        ['its own href first', { href: '/custom', type: 'insight', ref: 'abc' }, '/custom'],
        ['the registered page for its type', { type: 'feature_flag', ref: '7' }, '/feature-flags/7'],
        ['no link without a ref', { type: 'feature_flag' }, null],
        ['no link for an unknown type', { type: 'not_a_type', ref: '7' }, null],
    ])('links an object to %s', (_, entry, href) => {
        expect(libraryObjectHref(entry)).toBe(href)
    })

    test.each([
        ['/feature-flags/920847', 'feature_flag'],
        ['/workflows/abc/workflow', 'workflows'],
        ['/replay/playlists/abc', 'session_recording_playlist'],
        ['/feature-flags', 'feature_flag'],
        ['/dashboard', null],
        ['/data-management/actions', 'action'],
        ['/feature-flags/templates', null],
        ['/workflows/library/templates/new', null],
        ['/notebooks/abc', null],
    ])('finds the object type of %s', (path, type) => {
        expect(libraryTypeForPath(path)).toBe(type)
    })

    test.each([
        ['feature_flag', '/feature-flags'],
        ['cohort', '/cohorts'],
        ['session_recording_playlist', '/replay/playlists'],
        ['not_a_type', null],
    ])('opens the list page of %s', (type, href) => {
        const listHref = libraryListHref(type)
        expect(listHref).toBe(href)
        if (listHref) {
            expect(libraryTypeForPath(listHref)).toBe(type)
        }
    })
})