components.spec.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { cleanup, render, screen } from '@testing-library/react'
  4. import type { GeneralSectionComponentProps } from '../src/client/GeneralSection.tsx'
  5. import { GeneralSection } from '../src/client/GeneralSection.tsx'
  6. import { CloseLabel, HeaderContent, TriggerContent } from '../src/client/chrome.tsx'
  7. import type { TriggerContentProps } from '../src/client/chrome.tsx'
  8. import { en } from '../src/client/locales.ts'
  9. afterEach(cleanup)
  10. // The seat's key domain is settings ∪ common; the stub answers from the
  11. // package dictionary and falls back to the key like the real chain.
  12. const t: TriggerContentProps['t'] = key => (en as Record<string, string>)[key] ?? key
  13. // Global standard kit stubs: none of these components consume the hooks.
  14. const unusedHook = (() => { throw new Error('unused by settings-general components') }) as never
  15. const kit = { useSessions: unusedHook, useWorkspaces: unusedHook }
  16. describe('chrome content', () => {
  17. it('TriggerContent renders the icon with the label in the wide column', () => {
  18. const { container } = render(<TriggerContent {...kit} wide t={t} />)
  19. expect(container.querySelector('svg')).toBeTruthy()
  20. expect(screen.getByText('Settings')).toBeTruthy()
  21. })
  22. it('TriggerContent drops the label in the rail state', () => {
  23. const { container } = render(<TriggerContent {...kit} wide={false} t={t} />)
  24. expect(container.querySelector('svg')).toBeTruthy()
  25. expect(screen.queryByText('Settings')).toBeNull()
  26. })
  27. it('HeaderContent and CloseLabel render their translated text', () => {
  28. render(<HeaderContent {...kit} t={t} />)
  29. render(<CloseLabel {...kit} t={t} />)
  30. expect(screen.getByText('Settings')).toBeTruthy()
  31. expect(screen.getByText('Close')).toBeTruthy()
  32. })
  33. })
  34. describe('GeneralSection', () => {
  35. function mount() {
  36. const renderSlot = vi.fn(
  37. ((key: string) => <div data-testid={`slot-${key}`} />) as GeneralSectionComponentProps['renderSlot'],
  38. )
  39. const props: GeneralSectionComponentProps = { ...kit, renderSlot }
  40. const view = render(<GeneralSection {...props} />)
  41. return { view, renderSlot }
  42. }
  43. it('renders the item slot as the section body', () => {
  44. const { renderSlot } = mount()
  45. expect(renderSlot).toHaveBeenCalledWith('settings.general.item', {})
  46. expect(screen.getByTestId('slot-settings.general.item')).toBeTruthy()
  47. })
  48. })