settings-root.spec.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { useEffect, useState } from 'react'
  4. import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
  5. import type { SettingsRootComponentProps } from '../src/client/contract/slots.ts'
  6. import { SettingsRoot } from '../src/client/SettingsRoot.tsx'
  7. afterEach(cleanup)
  8. type Row = { id: string; order: number; label: string }
  9. /** Slot-content stand-ins: the shell renders whatever the seats contribute. */
  10. const SEAT_CONTENT: Record<string, string> = {
  11. 'settings.trigger': 'Settings',
  12. 'settings.header': 'Settings Title',
  13. 'settings.close': 'Close',
  14. }
  15. function mount({
  16. wide = true,
  17. onboardingActive = true,
  18. rows = [
  19. { id: 'general', order: 0, label: 'General' },
  20. { id: 'models', order: 10, label: 'Models' },
  21. ],
  22. }: { wide?: boolean; onboardingActive?: boolean; rows?: Row[] } = {}) {
  23. // Mutable row source standing in for the bound useSections hook; bump()
  24. // plays a ledger change through the same observable contract.
  25. let current = rows
  26. const listeners = new Set<() => void>()
  27. const renderSlot = vi.fn(
  28. ((key: string, _owner: unknown, opts?: { only?: string }) => {
  29. if (key === 'settings.section') return <div data-testid={`section-${opts?.only ?? 'all'}`} />
  30. return SEAT_CONTENT[key]
  31. }) as SettingsRootComponentProps['renderSlot'],
  32. )
  33. const useSessions = ((select: (state: unknown) => unknown) => select(onboardingActive
  34. ? { phase: 'ready', current: undefined, byId: {} }
  35. : {
  36. phase: 'ready',
  37. current: 'active-session',
  38. byId: { 'active-session': { blank: false } },
  39. })) as never
  40. const unusedHook = (() => { throw new Error('unused by SettingsRoot') }) as never
  41. const props: SettingsRootComponentProps = {
  42. useSessions,
  43. useWorkspaces: unusedHook,
  44. wide,
  45. useSections: (select) => {
  46. const [, force] = useState(0)
  47. useEffect(() => {
  48. const listener = () => { force(n => n + 1) }
  49. listeners.add(listener)
  50. return () => { listeners.delete(listener) }
  51. }, [])
  52. return select(current)
  53. },
  54. renderSlot,
  55. }
  56. const view = render(<SettingsRoot {...props} />)
  57. const bump = (next: Row[]) => {
  58. act(() => {
  59. current = next
  60. for (const fn of [...listeners]) fn()
  61. })
  62. }
  63. return { view, renderSlot, bump, listeners }
  64. }
  65. function openPanel() {
  66. fireEvent.click(screen.getByRole('button', { name: 'Settings' }))
  67. }
  68. describe('SettingsRoot trigger', () => {
  69. it('renders the trigger seat content as the accessible name (no aria-label of its own)', () => {
  70. const { renderSlot } = mount()
  71. const trigger = screen.getByRole('button', { name: 'Settings' })
  72. expect(trigger.hasAttribute('aria-label')).toBe(false)
  73. expect(renderSlot).toHaveBeenCalledWith('settings.trigger', { wide: true })
  74. expect(trigger.getAttribute('aria-expanded')).toBe('false')
  75. fireEvent.click(trigger)
  76. expect(screen.getByRole('dialog')).toBeTruthy()
  77. expect(screen.getByRole('button', { name: 'Settings', expanded: true })).toBeTruthy()
  78. })
  79. it('hands the rail state to the trigger seat', () => {
  80. const { renderSlot } = mount({ wide: false })
  81. expect(renderSlot).toHaveBeenCalledWith('settings.trigger', { wide: false })
  82. })
  83. })
  84. describe('SettingsPanel chrome seats', () => {
  85. it('names the dialog via aria-labelledby pointing at the header seat node', () => {
  86. mount()
  87. openPanel()
  88. const dialog = screen.getByRole('dialog')
  89. const titleId = dialog.getAttribute('aria-labelledby')!
  90. expect(titleId).toBeTruthy()
  91. const title = document.getElementById(titleId)!
  92. expect(title.textContent).toBe('Settings Title')
  93. expect(screen.getByRole('dialog', { name: 'Settings Title' })).toBeTruthy()
  94. })
  95. it('names the close button through the visually-hidden close seat text', () => {
  96. mount()
  97. openPanel()
  98. const close = screen.getByRole('button', { name: 'Close' })
  99. expect(close.hasAttribute('aria-label')).toBe(false)
  100. expect(close.textContent).toContain('Close')
  101. })
  102. })
  103. describe('SettingsPanel close paths', () => {
  104. it('closes via the header button', () => {
  105. mount()
  106. openPanel()
  107. fireEvent.click(screen.getByRole('button', { name: 'Close' }))
  108. expect(screen.queryByRole('dialog')).toBeNull()
  109. })
  110. it('closes via a mask click', () => {
  111. mount()
  112. openPanel()
  113. const dialog = screen.getByRole('dialog')
  114. fireEvent.click(dialog.parentElement!.firstElementChild!)
  115. expect(screen.queryByRole('dialog')).toBeNull()
  116. })
  117. it('closes via document-level Escape and unhooks the listener with the panel', () => {
  118. mount()
  119. openPanel()
  120. fireEvent.keyDown(document, { key: 'Escape' })
  121. expect(screen.queryByRole('dialog')).toBeNull()
  122. // Ignored while closed (listener removed with the panel) and non-Escape
  123. // keys are ignored while open.
  124. fireEvent.keyDown(document, { key: 'Escape' })
  125. openPanel()
  126. fireEvent.keyDown(document, { key: 'Enter' })
  127. expect(screen.getByRole('dialog')).toBeTruthy()
  128. })
  129. it('lands focus on the close button when the dialog opens', () => {
  130. mount()
  131. openPanel()
  132. expect(document.activeElement).toBe(screen.getByRole('button', { name: 'Close' }))
  133. })
  134. })
  135. describe('SettingsPanel navigation', () => {
  136. it('projects rows, marks the first active, and renders only that section', () => {
  137. mount()
  138. openPanel()
  139. expect(screen.getByRole('button', { name: 'General' }).getAttribute('aria-current')).toBe('true')
  140. expect(screen.getByRole('button', { name: 'Models' }).getAttribute('aria-current')).toBeNull()
  141. expect(screen.getByTestId('section-general')).toBeTruthy()
  142. })
  143. it('switches the rendered section on nav click', () => {
  144. mount()
  145. openPanel()
  146. fireEvent.click(screen.getByRole('button', { name: 'Models' }))
  147. expect(screen.getByRole('button', { name: 'Models' }).getAttribute('aria-current')).toBe('true')
  148. expect(screen.getByTestId('section-models')).toBeTruthy()
  149. expect(screen.queryByTestId('section-general')).toBeNull()
  150. })
  151. it('hands Hero readiness and a direct section opener to onboarding registrants', () => {
  152. const { renderSlot } = mount()
  153. const onboardingCall = renderSlot.mock.calls.find(call => call[0] === 'settings.onboarding')
  154. expect(onboardingCall?.[1]).toMatchObject({ active: true })
  155. act(() => {
  156. (onboardingCall?.[1] as { openSection: (id: string) => void }).openSection('models')
  157. })
  158. expect(screen.getByRole('dialog')).toBeTruthy()
  159. expect(screen.getByTestId('section-models')).toBeTruthy()
  160. cleanup()
  161. const active = mount({ onboardingActive: false }).renderSlot.mock.calls
  162. .find(call => call[0] === 'settings.onboarding')
  163. expect(active?.[1]).toMatchObject({ active: false })
  164. })
  165. it('falls back to the first row when the active entry unregisters', () => {
  166. const { bump } = mount()
  167. openPanel()
  168. fireEvent.click(screen.getByRole('button', { name: 'Models' }))
  169. bump([{ id: 'general', order: 0, label: 'General' }])
  170. expect(screen.queryByRole('button', { name: 'Models' })).toBeNull()
  171. expect(screen.getByTestId('section-general')).toBeTruthy()
  172. })
  173. it('renders an empty content column when the ledger is empty', () => {
  174. const { renderSlot } = mount({ rows: [] })
  175. openPanel()
  176. expect(screen.getByRole('dialog')).toBeTruthy()
  177. const sectionCalls = renderSlot.mock.calls.filter(c => c[0] === 'settings.section')
  178. expect(sectionCalls).toHaveLength(0)
  179. })
  180. it('drops the ledger subscription on unmount', () => {
  181. const { view, listeners } = mount()
  182. expect(listeners.size).toBe(1)
  183. view.unmount()
  184. expect(listeners.size).toBe(0)
  185. })
  186. })