Skip to content

A full-featured download manager.

2.9 KB
Raw Blame
import '@testing-library/jest-dom/vitest'
import '@renderer/lib/i18n'
import { applyRendererLocale } from '@renderer/lib/i18n'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { StatusTitleMenu } from './status-title-menu'

beforeAll(() => {
  // jsdom lacks these; Radix dropdown touches them on open.
  Element.prototype.scrollIntoView = vi.fn()
  if (!Element.prototype.hasPointerCapture) {
    Element.prototype.hasPointerCapture = () => false
  }
})

const counts = { all: 12, active: 4, completed: 7, error: 1 }

describe('StatusTitleMenu', () => {
  beforeEach(async () => {
    await applyRendererLocale('en-US')
  })

  it.each([
    ['es', 'all', 'Todas las descargas'],
    ['es', 'active', 'Descargas activas'],
    ['es', 'completed', 'Descargas completadas'],
    ['es', 'error', 'Descargas fallidas'],
    ['fr', 'all', 'Tous les téléchargements'],
    ['de', 'active', 'Aktive Downloads'],
    ['zh-CN', 'all', '全部下载'],
    ['zh-TW', 'all', '全部下載'],
  ] as const)(
    'renders a complete %s heading for %s without joining standalone labels',
    async (locale, tab, heading) => {
      await applyRendererLocale(locale)
      render(
        <StatusTitleMenu tab={tab} onTabChange={vi.fn()} counts={counts} />
      )

      expect(screen.getByRole('heading', { name: heading })).toBeVisible()
      expect(screen.getByRole('button', { name: heading })).toBeVisible()
    }
  )

  it('renders the current status in the heading', () => {
    render(
      <StatusTitleMenu tab="active" onTabChange={vi.fn()} counts={counts} />
    )
    const heading = screen.getByRole('heading')
    expect(heading).toHaveTextContent(/active/i)
    expect(heading).toHaveTextContent(/downloads/i)
  })

  it('shows the inline count', () => {
    render(
      <StatusTitleMenu tab="active" onTabChange={vi.fn()} counts={counts} />
    )
    // Inline badge renders `${counts[tab]}/${counts.all}` → active 4 of 12.
    expect(screen.getByText('4/12')).toBeInTheDocument()
  })

  // The trigger carries the `app-no-drag` utility (-webkit-app-region: no-drag)
  // so it stays clickable inside the Electron frameless-window drag region.
  // jsdom does not implement the non-standard `-webkit-app-region` property, so
  // the region behavior can't be asserted here — it's covered by the manual
  // Electron smoke check in the plan's final task.

  it('selecting a menu item calls onTabChange', async () => {
    const onTabChange = vi.fn()
    const user = userEvent.setup()
    render(
      <StatusTitleMenu tab="all" onTabChange={onTabChange} counts={counts} />
    )
    await user.click(screen.getByRole('button', { name: /downloads/i }))
    await user.click(
      await screen.findByRole('menuitem', { name: /completed/i })
    )
    expect(onTabChange).toHaveBeenCalledWith('completed')
  })
})