import { screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, expect, test, vi } from 'vitest'
import { DocsButton } from './DocsButton'
import { customRender } from '@/tests/lib/custom-render'
const { track } = vi.hoisted(() => ({ track: vi.fn() }))
vi.mock('@/lib/telemetry/track', () => ({ useTrack: () => track }))
beforeEach(() => {
track.mockClear()
})
test('clicking tracks once with the topic', async () => {
const user = userEvent.setup()
customRender()
await user.click(screen.getByRole('link', { name: /auth documentation/i }))
expect(track).toHaveBeenCalledTimes(1)
expect(track).toHaveBeenCalledWith('docs_button_clicked', { topic: 'Auth' })
})
test('clicking without a topic omits the topic property', async () => {
const user = userEvent.setup()
customRender()
await user.click(screen.getByRole('link', { name: 'Docs' }))
expect(track).toHaveBeenCalledTimes(1)
expect(track).toHaveBeenCalledWith('docs_button_clicked', {})
})
test('clicking does not bubble to a clickable parent', async () => {
const user = userEvent.setup()
const onParentClick = vi.fn()
customRender(
)
await user.click(screen.getByRole('link', { name: 'Docs' }))
expect(onParentClick).not.toHaveBeenCalled()
})
test('icon-only keeps the topic in the accessible name and still tracks clicks', async () => {
const user = userEvent.setup()
customRender()
const link = screen.getByRole('link', { name: 'Auth documentation (opens in new tab)' })
expect(link).not.toHaveTextContent('Docs')
await user.click(link)
expect(track).toHaveBeenCalledWith('docs_button_clicked', { topic: 'Auth' })
})