tooltip.client.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // @vitest-environment jsdom
  2. import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
  3. import { afterEach, describe, expect, it, vi } from 'vitest'
  4. import { Tooltip } from '@deepseek-ai/dsh-client-ui-primitives'
  5. afterEach(cleanup)
  6. describe('Tooltip', () => {
  7. it('resolves lazy labels only after the bubble becomes visible', () => {
  8. vi.useFakeTimers()
  9. try {
  10. const label = vi.fn(() => 'Timing details')
  11. render(
  12. <Tooltip label={label} delayMs={500}>
  13. <button type="button">anchor</button>
  14. </Tooltip>,
  15. )
  16. expect(label).not.toHaveBeenCalled()
  17. fireEvent.mouseEnter(screen.getByText('anchor'))
  18. act(() => { vi.advanceTimersByTime(499) })
  19. expect(label).not.toHaveBeenCalled()
  20. act(() => { vi.advanceTimersByTime(1) })
  21. expect(screen.getByRole('tooltip').textContent).toBe('Timing details')
  22. expect(label).toHaveBeenCalledOnce()
  23. } finally {
  24. vi.useRealTimers()
  25. }
  26. })
  27. it('can delay pointer hover without delaying keyboard focus', () => {
  28. vi.useFakeTimers()
  29. try {
  30. render(
  31. <Tooltip label="Timing details" delayMs={500}>
  32. <button type="button">anchor</button>
  33. </Tooltip>,
  34. )
  35. const anchor = screen.getByText('anchor')
  36. fireEvent.mouseEnter(anchor)
  37. act(() => { vi.advanceTimersByTime(499) })
  38. expect(screen.queryByRole('tooltip')).toBeNull()
  39. fireEvent.mouseLeave(anchor)
  40. act(() => { vi.advanceTimersByTime(1) })
  41. expect(screen.queryByRole('tooltip')).toBeNull()
  42. fireEvent.mouseEnter(anchor)
  43. act(() => { vi.advanceTimersByTime(500) })
  44. expect(screen.getByRole('tooltip').textContent).toBe('Timing details')
  45. fireEvent.mouseLeave(anchor)
  46. fireEvent.focus(anchor)
  47. expect(screen.getByRole('tooltip').textContent).toBe('Timing details')
  48. } finally {
  49. vi.useRealTimers()
  50. }
  51. })
  52. it('shows the bubble to the right on hover and hides it on leave', () => {
  53. render(
  54. <Tooltip label="Open sidebar">
  55. <button type="button">anchor</button>
  56. </Tooltip>,
  57. )
  58. const anchor = screen.getByText('anchor')
  59. fireEvent.mouseEnter(anchor)
  60. const bubble = screen.getByRole('tooltip')
  61. expect(bubble.textContent).toBe('Open sidebar')
  62. expect(bubble.getAttribute('data-side')).toBe('right')
  63. // jsdom rects are all-zero: right placement lands at the +10 gutter, then
  64. // the zero-width measured rect clamps to the 12px edge margin (10 + 12).
  65. expect(bubble.style.left).toBe('22px')
  66. expect(bubble.style.top).toBe('0px')
  67. fireEvent.mouseLeave(anchor)
  68. expect(screen.queryByRole('tooltip')).toBeNull()
  69. })
  70. it('supports bottom placement and the focus/blur channel', () => {
  71. render(
  72. <Tooltip label="Below" side="bottom">
  73. <button type="button">anchor</button>
  74. </Tooltip>,
  75. )
  76. const anchor = screen.getByText('anchor')
  77. fireEvent.focus(anchor)
  78. const bubble = screen.getByRole('tooltip')
  79. expect(bubble.getAttribute('data-side')).toBe('bottom')
  80. // Zero-width jsdom rect at x=0 clamps to the 12px edge margin.
  81. expect(bubble.style.left).toBe('12px')
  82. expect(bubble.style.top).toBe('8px')
  83. fireEvent.blur(anchor)
  84. expect(screen.queryByRole('tooltip')).toBeNull()
  85. })
  86. // jsdom's default rects are all-zero, so the clamp tests stub the measured
  87. // rect (anchor and bubble share the prototype stub) and derive expectations
  88. // from it: pos.x = anchor center, then shifted by the measured overflow.
  89. const rect = (left: number, right: number): DOMRect =>
  90. ({ left, right, top: 0, bottom: 20, width: right - left, height: 20, x: left, y: 0, toJSON: () => ({}) })
  91. it('caps the bubble width where the label would otherwise slab across the surface', () => {
  92. render(
  93. <Tooltip label="A description long enough to need a cap" side="bottom" maxWidth={360}>
  94. <button type="button">anchor</button>
  95. </Tooltip>,
  96. )
  97. fireEvent.mouseEnter(screen.getByText('anchor'))
  98. // The stylesheet's half-viewport cap stays the default; this one overrides it.
  99. expect(screen.getByRole('tooltip').style.maxWidth).toBe('360px')
  100. })
  101. it('clamps a bubble overflowing the right viewport edge back inside', () => {
  102. const spy = vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue(rect(900, 1100))
  103. try {
  104. render(
  105. <Tooltip label="Wide" side="bottom">
  106. <button type="button">anchor</button>
  107. </Tooltip>,
  108. )
  109. fireEvent.mouseEnter(screen.getByText('anchor'))
  110. // pos.x = 1000 (anchor center); measured right edge 1100 overflows the
  111. // 1024 viewport's 12px safe margin (limit 1012) by 88, so the clamp
  112. // shifts left to 912.
  113. expect(screen.getByRole('tooltip').style.left).toBe('912px')
  114. } finally {
  115. spy.mockRestore()
  116. }
  117. })
  118. it('reclamps after label and viewport width changes', () => {
  119. const originalWidth = window.innerWidth
  120. const spy = vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function (this: Element) {
  121. if (this.getAttribute('role') !== 'tooltip') return rect(900, 1000)
  122. return this.textContent === 'Wide' ? rect(900, 1100) : rect(850, 950)
  123. })
  124. try {
  125. const view = render(
  126. <Tooltip label="Wide" side="bottom">
  127. <button type="button">anchor</button>
  128. </Tooltip>,
  129. )
  130. fireEvent.mouseEnter(screen.getByText('anchor'))
  131. expect(screen.getByRole('tooltip').style.left).toBe('862px')
  132. view.rerender(
  133. <Tooltip label="Short" side="bottom">
  134. <button type="button">anchor</button>
  135. </Tooltip>,
  136. )
  137. expect(screen.getByRole('tooltip').style.left).toBe('950px')
  138. Object.defineProperty(window, 'innerWidth', { configurable: true, value: 900 })
  139. fireEvent(window, new Event('resize'))
  140. expect(screen.getByRole('tooltip').style.left).toBe('888px')
  141. } finally {
  142. Object.defineProperty(window, 'innerWidth', { configurable: true, value: originalWidth })
  143. spy.mockRestore()
  144. }
  145. })
  146. it('clamps a bubble past the left viewport edge back inside', () => {
  147. const spy = vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue(rect(-20, 80))
  148. try {
  149. render(
  150. <Tooltip label="Wide" side="bottom">
  151. <button type="button">anchor</button>
  152. </Tooltip>,
  153. )
  154. fireEvent.mouseEnter(screen.getByText('anchor'))
  155. // pos.x = 30 (anchor center); measured left edge -20 underflows the
  156. // 12px safe margin by 32, so the clamp shifts right to 62.
  157. expect(screen.getByRole('tooltip').style.left).toBe('62px')
  158. } finally {
  159. spy.mockRestore()
  160. }
  161. })
  162. /** Anchor and bubble rects, so a placement test measures real room rather than jsdom's all-zero boxes. */
  163. const placed = (anchorTop: number, anchorBottom: number, bubbleHeight: number) =>
  164. vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function (this: Element) {
  165. const [top, bottom] = this.getAttribute('role') === 'tooltip'
  166. ? [0, bubbleHeight]
  167. : [anchorTop, anchorBottom]
  168. return {
  169. left: 100, right: 200, top, bottom, width: 100, height: bottom - top, x: 100, y: top, toJSON: () => ({}),
  170. }
  171. })
  172. it('supports top placement for anchors at the viewport bottom', () => {
  173. const spy = placed(700, 720, 20)
  174. try {
  175. render(
  176. <Tooltip label="Above" side="top">
  177. <button type="button">anchor</button>
  178. </Tooltip>,
  179. )
  180. fireEvent.mouseEnter(screen.getByText('anchor'))
  181. const bubble = screen.getByRole('tooltip')
  182. // There is room above, so the requested side stands: the bubble's own
  183. // top sits at the anchor's top less the 8px gutter.
  184. expect(bubble.getAttribute('data-side')).toBe('top')
  185. expect(bubble.style.top).toBe('692px')
  186. expect(bubble.style.left).toBe('150px')
  187. } finally {
  188. spy.mockRestore()
  189. }
  190. })
  191. it('flips a bottom bubble above an anchor with no room below', () => {
  192. // jsdom's viewport is 768 tall: a 300px bubble under an anchor ending at
  193. // 700 would run off, and there is room for it above.
  194. const spy = placed(600, 700, 300)
  195. try {
  196. render(
  197. <Tooltip label="Tall" side="bottom">
  198. <button type="button">anchor</button>
  199. </Tooltip>,
  200. )
  201. fireEvent.mouseEnter(screen.getByText('anchor'))
  202. const bubble = screen.getByRole('tooltip')
  203. expect(bubble.getAttribute('data-side')).toBe('top')
  204. expect(bubble.style.top).toBe('592px')
  205. } finally {
  206. spy.mockRestore()
  207. }
  208. })
  209. it('flips a top bubble below an anchor with no room above', () => {
  210. const spy = placed(10, 40, 100)
  211. try {
  212. render(
  213. <Tooltip label="Tall" side="top">
  214. <button type="button">anchor</button>
  215. </Tooltip>,
  216. )
  217. fireEvent.mouseEnter(screen.getByText('anchor'))
  218. const bubble = screen.getByRole('tooltip')
  219. expect(bubble.getAttribute('data-side')).toBe('bottom')
  220. expect(bubble.style.top).toBe('48px')
  221. } finally {
  222. spy.mockRestore()
  223. }
  224. })
  225. it('keeps the requested side when neither side fits', () => {
  226. // A bubble taller than the viewport has no home; oscillating between the
  227. // two would be worse than honouring the request.
  228. const spy = placed(300, 400, 900)
  229. try {
  230. render(
  231. <Tooltip label="Huge" side="bottom">
  232. <button type="button">anchor</button>
  233. </Tooltip>,
  234. )
  235. fireEvent.mouseEnter(screen.getByText('anchor'))
  236. expect(screen.getByRole('tooltip').getAttribute('data-side')).toBe('bottom')
  237. } finally {
  238. spy.mockRestore()
  239. }
  240. })
  241. it('chains the anchor\'s own handlers ahead of the tooltip\'s', () => {
  242. const onMouseEnter = vi.fn()
  243. const onMouseLeave = vi.fn()
  244. const onFocus = vi.fn()
  245. const onBlur = vi.fn()
  246. render(
  247. <Tooltip label="Chained">
  248. <button type="button" onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} onFocus={onFocus} onBlur={onBlur}>anchor</button>
  249. </Tooltip>,
  250. )
  251. const anchor = screen.getByText('anchor')
  252. fireEvent.mouseEnter(anchor)
  253. fireEvent.mouseLeave(anchor)
  254. fireEvent.focus(anchor)
  255. fireEvent.blur(anchor)
  256. expect(onMouseEnter).toHaveBeenCalledOnce()
  257. expect(onMouseLeave).toHaveBeenCalledOnce()
  258. expect(onFocus).toHaveBeenCalledOnce()
  259. expect(onBlur).toHaveBeenCalledOnce()
  260. })
  261. it('suppresses the bubble while disabled without remounting the anchor', () => {
  262. const { rerender } = render(
  263. <Tooltip label="Rail" disabled>
  264. <button type="button">anchor</button>
  265. </Tooltip>,
  266. )
  267. const anchor = screen.getByText('anchor')
  268. fireEvent.mouseEnter(anchor)
  269. expect(screen.queryByRole('tooltip')).toBeNull()
  270. rerender(
  271. <Tooltip label="Rail">
  272. <button type="button">anchor</button>
  273. </Tooltip>,
  274. )
  275. // Same DOM node: toggling disabled never remounted the anchor.
  276. expect(screen.getByText('anchor')).toBe(anchor)
  277. fireEvent.mouseEnter(anchor)
  278. expect(screen.getByRole('tooltip')).toBeTruthy()
  279. })
  280. it('mouse leave hides the bubble immediately, even while the anchor stays focused', () => {
  281. render(
  282. <Tooltip label="Sticky">
  283. <button type="button">anchor</button>
  284. </Tooltip>,
  285. )
  286. const anchor = screen.getByText('anchor')
  287. // Focused AND hovered: leaving with the mouse drops the bubble at once.
  288. fireEvent.focus(anchor)
  289. fireEvent.mouseEnter(anchor)
  290. fireEvent.mouseLeave(anchor)
  291. expect(screen.queryByRole('tooltip')).toBeNull()
  292. // Re-entering shows it again; blurring while still hovered keeps it.
  293. fireEvent.mouseEnter(anchor)
  294. fireEvent.blur(anchor)
  295. expect(screen.getByRole('tooltip')).toBeTruthy()
  296. fireEvent.mouseLeave(anchor)
  297. expect(screen.queryByRole('tooltip')).toBeNull()
  298. })
  299. it('forwards the anchor element to the child ref (object and callback)', () => {
  300. const objectRef = { current: null as HTMLButtonElement | null }
  301. const callbackRef = vi.fn()
  302. const { rerender } = render(
  303. <Tooltip label="Add">
  304. <button type="button" ref={objectRef}>anchor</button>
  305. </Tooltip>,
  306. )
  307. expect(objectRef.current).toBe(screen.getByText('anchor'))
  308. // Tooltip's own positioning still works through the merged ref.
  309. fireEvent.mouseEnter(screen.getByText('anchor'))
  310. expect(screen.getByRole('tooltip')).toBeTruthy()
  311. rerender(
  312. <Tooltip label="Add">
  313. <button type="button" ref={callbackRef}>anchor</button>
  314. </Tooltip>,
  315. )
  316. expect(callbackRef).toHaveBeenCalledWith(screen.getByText('anchor'))
  317. })
  318. it('drops an already-visible bubble when disabled flips mid-hover', () => {
  319. const { rerender } = render(
  320. <Tooltip label="Rail">
  321. <button type="button">anchor</button>
  322. </Tooltip>,
  323. )
  324. fireEvent.mouseEnter(screen.getByText('anchor'))
  325. expect(screen.getByRole('tooltip')).toBeTruthy()
  326. // e.g. clicking a rail control expands the sidebar: no mouseleave fires.
  327. rerender(
  328. <Tooltip label="Rail" disabled>
  329. <button type="button">anchor</button>
  330. </Tooltip>,
  331. )
  332. expect(screen.queryByRole('tooltip')).toBeNull()
  333. })
  334. it('withdraws the enclosing bubble while a nested tooltip shows its own', () => {
  335. render(
  336. <Tooltip label="Open sidebar">
  337. <button type="button">
  338. anchor
  339. <Tooltip label="Update — V1.2.3">
  340. <span data-testid="badge" />
  341. </Tooltip>
  342. </button>
  343. </Tooltip>,
  344. )
  345. const anchor = screen.getByText('anchor')
  346. const badge = screen.getByTestId('badge')
  347. fireEvent.mouseEnter(anchor)
  348. expect(screen.getByRole('tooltip').textContent).toBe('Open sidebar')
  349. // Entering the nested anchor withdraws the enclosing bubble instead of
  350. // stacking both; the enclosing anchor stays hovered, so nothing is lost.
  351. fireEvent.mouseEnter(badge)
  352. expect(screen.getAllByRole('tooltip').map(bubble => bubble.textContent)).toEqual(['Update — V1.2.3'])
  353. // Leaving the nested anchor for the enclosing one restores its bubble;
  354. // the pointer never left the enclosing anchor, so only the badge is left.
  355. fireEvent.mouseLeave(badge, { relatedTarget: anchor })
  356. expect(screen.getByRole('tooltip').textContent).toBe('Open sidebar')
  357. fireEvent.mouseLeave(anchor)
  358. expect(screen.queryByRole('tooltip')).toBeNull()
  359. })
  360. it('releases the enclosing bubble when a shown nested tooltip unmounts', () => {
  361. const view = render(
  362. <Tooltip label="Open sidebar">
  363. <button type="button">
  364. anchor
  365. <Tooltip label="Update"><span data-testid="badge" /></Tooltip>
  366. </button>
  367. </Tooltip>,
  368. )
  369. fireEvent.mouseEnter(screen.getByText('anchor'))
  370. fireEvent.mouseEnter(screen.getByTestId('badge'))
  371. expect(screen.getAllByRole('tooltip').map(bubble => bubble.textContent)).toEqual(['Update'])
  372. view.rerender(
  373. <Tooltip label="Open sidebar">
  374. <button type="button">anchor</button>
  375. </Tooltip>,
  376. )
  377. expect(screen.getByRole('tooltip').textContent).toBe('Open sidebar')
  378. })
  379. })