pointer-scrollbars.client.spec.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // @vitest-environment jsdom
  2. /**
  3. * Pointer-revealed scrollbars, the shell's half: which class state the column
  4. * carries as the pointer crosses it. The stylesheet rule that state drives is
  5. * asserted in scrollbar-quiet-styles.spec.ts (node environment — a jsdom spec
  6. * has no file: module URL to read the sheet through).
  7. */
  8. import { afterEach, describe, expect, it, vi } from 'vitest'
  9. import { act, cleanup, fireEvent, render } from '@testing-library/react'
  10. import type { SidebarRootComponentProps, SidebarSectionOwnerProps } from '../src/client/contract/slots.ts'
  11. import { SidebarRoot } from '../src/client/SidebarRoot.tsx'
  12. import { en } from '../src/client/locales.ts'
  13. /** Pinned column box; the shell compares pointer coordinates against it. */
  14. const COLUMN_WIDTH = 280
  15. const COLUMN_HEIGHT = 600
  16. const t: SidebarRootComponentProps['t'] = key => (en as Record<string, string>)[key] ?? key
  17. /** The shell never reads the global hooks; the props share carries them regardless. */
  18. const neverHook = (() => { throw new Error('shell must not read global hooks') }) as never
  19. type AttentionSnapshot = Parameters<Parameters<SidebarRootComponentProps['useSessionPendingInteraction']>[0]>[0]
  20. const noAttention: AttentionSnapshot = new Map()
  21. const useSessionPendingInteraction: SidebarRootComponentProps['useSessionPendingInteraction'] = selector => selector(noAttention)
  22. afterEach(() => {
  23. cleanup()
  24. vi.useRealTimers()
  25. })
  26. /**
  27. * Render the shell and expose its column element.
  28. * @returns the column element and whether it currently carries the quiet state.
  29. */
  30. function mountColumn(): { column: HTMLElement; quiet: () => boolean } {
  31. const view = render(
  32. <SidebarRoot
  33. collapsed={false} width={300}
  34. useSessions={neverHook} useSessionPendingInteraction={useSessionPendingInteraction} useWorkspaces={neverHook}
  35. startSession={vi.fn()} toggleSidebar={vi.fn()} t={t}
  36. renderSlot={((_key: string, owner: SidebarSectionOwnerProps) =>
  37. <div data-testid="region" data-wide={owner.wide} />) as SidebarRootComponentProps['renderSlot']}
  38. />,
  39. )
  40. const column = view.container.firstElementChild
  41. if (!(column instanceof HTMLElement)) throw new Error('sidebar column not rendered')
  42. // jsdom lays nothing out, and the leave decision is geometric: pin the box
  43. // the shell reads so a coordinate can be inside or outside it.
  44. Object.defineProperty(column, 'getBoundingClientRect', {
  45. value: () => ({
  46. left: 0, top: 0, right: COLUMN_WIDTH, bottom: COLUMN_HEIGHT,
  47. x: 0, y: 0, width: COLUMN_WIDTH, height: COLUMN_HEIGHT, toJSON: () => ({}),
  48. }),
  49. })
  50. // CSS-module locals are hashed in this bench, so the state is read as a
  51. // substring of the class list rather than as an exact local name.
  52. return { column, quiet: () => [...column.classList].some(name => name.includes('quietBars')) }
  53. }
  54. /**
  55. * Cross the pointer into or out of the column. React synthesizes
  56. * `pointerenter`/`pointerleave` from `pointerover`/`pointerout`, so the raw
  57. * enter and leave events it does not listen to would assert nothing.
  58. * @param column - the sidebar column element.
  59. * @param direction - `in` to enter the column, `out` to leave it.
  60. */
  61. function movePointer(column: HTMLElement, direction: 'in' | 'out'): void {
  62. const outside = document.body
  63. if (direction === 'in') fireEvent.pointerOver(column, { relatedTarget: outside })
  64. else fireEvent.pointerOut(column, { relatedTarget: outside })
  65. }
  66. /**
  67. * Move the pointer over the document, as a pointer crossing a fixed overlay
  68. * that is a DOM descendant of the column does.
  69. * @param x - client x coordinate.
  70. * @param y - client y coordinate.
  71. */
  72. function movePointerOverDocument(x: number, y: number): void {
  73. fireEvent.pointerMove(document, { clientX: x, clientY: y })
  74. }
  75. describe('SidebarRoot pointer-revealed scrollbars', () => {
  76. it('draws them only while the pointer is inside, and lingers on the way out', () => {
  77. vi.useFakeTimers()
  78. const { column, quiet } = mountColumn()
  79. // At rest — the pointer has never been over the column — the bars are off.
  80. expect(quiet()).toBe(true)
  81. movePointer(column, 'in')
  82. expect(quiet()).toBe(false)
  83. movePointer(column, 'out')
  84. // The linger: still drawn just before the window closes, gone just after.
  85. act(() => { vi.advanceTimersByTime(1999) })
  86. expect(quiet()).toBe(false)
  87. act(() => { vi.advanceTimersByTime(1) })
  88. expect(quiet()).toBe(true)
  89. })
  90. it('cancels a pending hide when the pointer comes back', () => {
  91. vi.useFakeTimers()
  92. const { column, quiet } = mountColumn()
  93. movePointer(column, 'in')
  94. movePointer(column, 'out')
  95. act(() => { vi.advanceTimersByTime(1000) })
  96. movePointer(column, 'in')
  97. // The first leave's timer would fire here; a cancelled one leaves the bars
  98. // drawn, which is what keeps a pointer skirting the edge from blinking them.
  99. act(() => { vi.advanceTimersByTime(5000) })
  100. expect(quiet()).toBe(false)
  101. })
  102. it('hides when the pointer moves outside the column box without leaving its subtree', () => {
  103. // ui-settings renders its full-viewport panel as a fixed-position
  104. // DESCENDANT of the column, so DOM containment reports the pointer as
  105. // still inside while it is visually somewhere else entirely.
  106. vi.useFakeTimers()
  107. const { column, quiet } = mountColumn()
  108. movePointer(column, 'in')
  109. expect(quiet()).toBe(false)
  110. movePointerOverDocument(COLUMN_WIDTH + 400, 300)
  111. act(() => { vi.advanceTimersByTime(2000) })
  112. expect(quiet()).toBe(true)
  113. })
  114. it('does not restart the window when the pointer keeps moving outside', () => {
  115. vi.useFakeTimers()
  116. const { column, quiet } = mountColumn()
  117. movePointer(column, 'in')
  118. movePointer(column, 'out')
  119. act(() => { vi.advanceTimersByTime(1500) })
  120. // A pending hide is left alone rather than re-armed: otherwise a pointer
  121. // resting outside the column would keep pushing the bars' disappearance
  122. // out, one move at a time.
  123. movePointerOverDocument(COLUMN_WIDTH + 400, 300)
  124. act(() => { vi.advanceTimersByTime(600) })
  125. expect(quiet()).toBe(true)
  126. })
  127. it('keeps them drawn while the pointer moves inside the column box', () => {
  128. vi.useFakeTimers()
  129. const { column, quiet } = mountColumn()
  130. movePointer(column, 'in')
  131. movePointer(column, 'out')
  132. // A move landing back inside the box cancels the pending hide, the same
  133. // way re-entering the element does.
  134. movePointerOverDocument(COLUMN_WIDTH - 10, 300)
  135. act(() => { vi.advanceTimersByTime(5000) })
  136. expect(quiet()).toBe(false)
  137. })
  138. it('drops the pending hide when the column unmounts', () => {
  139. vi.useFakeTimers()
  140. const { column } = mountColumn()
  141. movePointer(column, 'in')
  142. movePointer(column, 'out')
  143. cleanup()
  144. // A timer surviving the unmount would call setState on a dead component.
  145. expect(() => { vi.advanceTimersByTime(5000) }).not.toThrow()
  146. expect(vi.getTimerCount()).toBe(0)
  147. })
  148. })