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.

902 B
Raw Blame
import { newInternalTab } from 'lib/utils/newInternalTab'

describe('newInternalTab', () => {
    let clickedHrefs: string[]

    beforeEach(() => {
        clickedHrefs = []
        jest.spyOn(HTMLAnchorElement.prototype, 'dispatchEvent').mockImplementation(function (this: HTMLAnchorElement) {
            clickedHrefs.push(this.getAttribute('href') ?? '')
            return true
        })
    })

    afterEach(() => {
        jest.restoreAllMocks()
    })

    it('opens an external link as given', () => {
        newInternalTab('https://posthog.com/docs')

        expect(clickedHrefs).toEqual(['https://posthog.com/docs'])
    })

    it.each(['javascript:alert(1)', 'javascript:/api/,alert(document.cookie)', 'JaVaScRiPt:/login/,alert(1)'])(
        'opens nothing for %p',
        (path) => {
            newInternalTab(path)

            expect(clickedHrefs).toEqual([])
        }
    )
})