FilamentHoverCard.test.tsx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /**
  2. * Tests for the FilamentHoverCard component.
  3. * Focuses on fill level display and Spoolman source indicator.
  4. */
  5. import { describe, it, expect, vi, beforeEach } from 'vitest';
  6. import { render, screen, fireEvent, waitFor } from '../utils';
  7. import { act } from '@testing-library/react';
  8. import { FilamentHoverCard, EmptySlotHoverCard } from '../../components/FilamentHoverCard';
  9. import { setColorCatalog, __resetColorCatalogForTests } from '../../utils/colors';
  10. const baseFilamentData = {
  11. vendor: 'Bambu Lab' as const,
  12. profile: 'PLA Basic',
  13. colorName: 'Red',
  14. colorHex: 'FF0000',
  15. kFactor: '0.030',
  16. fillLevel: 75,
  17. trayUuid: 'A1B2C3D4E5F6A1B2C3D4E5F6A1B2C3D4',
  18. };
  19. function renderWithHover(ui: React.ReactElement) {
  20. const result = render(ui);
  21. // Trigger hover to show the card
  22. const trigger = result.container.firstElementChild as HTMLElement;
  23. fireEvent.mouseEnter(trigger);
  24. return result;
  25. }
  26. describe('FilamentHoverCard', () => {
  27. beforeEach(() => {
  28. vi.useFakeTimers({ shouldAdvanceTime: true });
  29. });
  30. describe('fill level display', () => {
  31. it('shows fill percentage when fillLevel is set', async () => {
  32. renderWithHover(
  33. <FilamentHoverCard data={{ ...baseFilamentData, fillLevel: 75 }}>
  34. <div>trigger</div>
  35. </FilamentHoverCard>
  36. );
  37. vi.advanceTimersByTime(100);
  38. await waitFor(() => {
  39. expect(screen.getByText('75%')).toBeInTheDocument();
  40. });
  41. });
  42. it('shows dash when fillLevel is null', async () => {
  43. renderWithHover(
  44. <FilamentHoverCard data={{ ...baseFilamentData, fillLevel: null }}>
  45. <div>trigger</div>
  46. </FilamentHoverCard>
  47. );
  48. vi.advanceTimersByTime(100);
  49. await waitFor(() => {
  50. expect(screen.getByText('—')).toBeInTheDocument();
  51. });
  52. });
  53. it('shows 0% when fillLevel is zero', async () => {
  54. renderWithHover(
  55. <FilamentHoverCard data={{ ...baseFilamentData, fillLevel: 0 }}>
  56. <div>trigger</div>
  57. </FilamentHoverCard>
  58. );
  59. vi.advanceTimersByTime(100);
  60. await waitFor(() => {
  61. expect(screen.getByText('0%')).toBeInTheDocument();
  62. });
  63. });
  64. });
  65. describe('fill source badge transparency (#11)', () => {
  66. it('never shows a Spoolman-source badge — UI stays mode-agnostic', async () => {
  67. renderWithHover(
  68. <FilamentHoverCard data={{ ...baseFilamentData, fillLevel: 80, fillSource: 'spoolman' }}>
  69. <div>trigger</div>
  70. </FilamentHoverCard>
  71. );
  72. vi.advanceTimersByTime(100);
  73. await waitFor(() => {
  74. expect(screen.getByText('80%')).toBeInTheDocument();
  75. expect(screen.queryByText('(Spoolman)')).not.toBeInTheDocument();
  76. });
  77. });
  78. it('never shows an inventory-source badge — UI stays mode-agnostic', async () => {
  79. renderWithHover(
  80. <FilamentHoverCard data={{ ...baseFilamentData, fillLevel: 80, fillSource: 'inventory' }}>
  81. <div>trigger</div>
  82. </FilamentHoverCard>
  83. );
  84. vi.advanceTimersByTime(100);
  85. await waitFor(() => {
  86. expect(screen.getByText('80%')).toBeInTheDocument();
  87. expect(screen.queryByText('(Inv)')).not.toBeInTheDocument();
  88. });
  89. });
  90. it('does not render an empty source-label span when fillLevel is null', async () => {
  91. renderWithHover(
  92. <FilamentHoverCard data={{ ...baseFilamentData, fillLevel: null, fillSource: 'spoolman' }}>
  93. <div>trigger</div>
  94. </FilamentHoverCard>
  95. );
  96. vi.advanceTimersByTime(100);
  97. await waitFor(() => {
  98. expect(screen.getByText('—')).toBeInTheDocument();
  99. expect(screen.queryByText('(Spoolman)')).not.toBeInTheDocument();
  100. expect(screen.queryByText('(Inv)')).not.toBeInTheDocument();
  101. });
  102. });
  103. });
  104. describe('hover behavior', () => {
  105. it('does not show card when disabled', () => {
  106. renderWithHover(
  107. <FilamentHoverCard data={baseFilamentData} disabled>
  108. <div>trigger</div>
  109. </FilamentHoverCard>
  110. );
  111. vi.advanceTimersByTime(100);
  112. // Card should not be visible
  113. expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument();
  114. });
  115. it('shows filament details on hover', async () => {
  116. renderWithHover(
  117. <FilamentHoverCard data={baseFilamentData}>
  118. <div>trigger</div>
  119. </FilamentHoverCard>
  120. );
  121. vi.advanceTimersByTime(100);
  122. await waitFor(() => {
  123. expect(screen.getByText('Red')).toBeInTheDocument();
  124. expect(screen.getByText('PLA Basic')).toBeInTheDocument();
  125. expect(screen.getByText('0.030')).toBeInTheDocument();
  126. });
  127. });
  128. });
  129. // The inventory section was previously hidden for `vendor === 'Bambu Lab'`
  130. // because BL spools were assumed to be managed entirely via RFID. #1133
  131. // removed that gate so users who don't want to scan via SpoolBuddy NFC
  132. // can still pick a BL spool from inventory the same way they pick a
  133. // third-party one.
  134. // Paired with the EmptySlotHoverCard assertion below (#2791) — together
  135. // they pin the two render paths to the same Assign-then-Configure order.
  136. it('lists Assign Spool above Configure (#2791)', async () => {
  137. renderWithHover(
  138. <FilamentHoverCard
  139. data={baseFilamentData}
  140. inventory={{ assignedSpool: null, onAssignSpool: vi.fn() }}
  141. configureSlot={{ enabled: true, onConfigure: vi.fn() }}
  142. >
  143. <div>trigger</div>
  144. </FilamentHoverCard>
  145. );
  146. vi.advanceTimersByTime(100);
  147. await waitFor(() => expect(screen.getByText(/assign spool/i)).toBeInTheDocument());
  148. const assign = screen.getByText(/assign spool/i);
  149. const configure = screen.getByText(/^configure$/i);
  150. expect(assign.compareDocumentPosition(configure)).toBe(
  151. Node.DOCUMENT_POSITION_FOLLOWING
  152. );
  153. });
  154. describe('inventory section vendor visibility (#1133)', () => {
  155. it('shows the assign-spool button on a Bambu Lab slot when the spool is unassigned', async () => {
  156. const onAssign = vi.fn();
  157. renderWithHover(
  158. <FilamentHoverCard
  159. data={{ ...baseFilamentData, vendor: 'Bambu Lab' }}
  160. inventory={{ assignedSpool: null, onAssignSpool: onAssign }}
  161. >
  162. <div>trigger</div>
  163. </FilamentHoverCard>
  164. );
  165. vi.advanceTimersByTime(100);
  166. await waitFor(() => {
  167. expect(screen.getByText(/assign/i)).toBeInTheDocument();
  168. });
  169. });
  170. it('shows the unassign button on a Bambu Lab slot when an inventory spool is already assigned', async () => {
  171. // Regression guard: the original gate hid BOTH the assign and unassign
  172. // buttons for BL slots. A user who'd already assigned an inventory
  173. // spool to a BL slot couldn't undo it without dropping into the
  174. // inventory page directly.
  175. const onUnassign = vi.fn();
  176. renderWithHover(
  177. <FilamentHoverCard
  178. data={{ ...baseFilamentData, vendor: 'Bambu Lab' }}
  179. inventory={{
  180. assignedSpool: {
  181. id: 1,
  182. material: 'PLA',
  183. subtype: null,
  184. brand: 'Devil Design',
  185. color_name: 'Black',
  186. },
  187. onUnassignSpool: onUnassign,
  188. }}
  189. >
  190. <div>trigger</div>
  191. </FilamentHoverCard>
  192. );
  193. vi.advanceTimersByTime(100);
  194. await waitFor(() => {
  195. expect(screen.getByText(/unassign/i)).toBeInTheDocument();
  196. });
  197. });
  198. it('still shows the assign-spool button for a non-Bambu vendor (no behaviour change)', async () => {
  199. const onAssign = vi.fn();
  200. renderWithHover(
  201. <FilamentHoverCard
  202. data={{ ...baseFilamentData, vendor: 'Polymaker' as unknown as 'Bambu Lab' }}
  203. inventory={{ assignedSpool: null, onAssignSpool: onAssign }}
  204. >
  205. <div>trigger</div>
  206. </FilamentHoverCard>
  207. );
  208. vi.advanceTimersByTime(100);
  209. await waitFor(() => {
  210. expect(screen.getByText(/assign/i)).toBeInTheDocument();
  211. });
  212. });
  213. it('shows the assign-spool button as disabled when isAssigned=true', async () => {
  214. const onAssign = vi.fn();
  215. renderWithHover(
  216. <FilamentHoverCard
  217. data={{ ...baseFilamentData, vendor: 'Bambu Lab' }}
  218. inventory={{ assignedSpool: null, onAssignSpool: onAssign, isAssigned: true }}
  219. >
  220. <div>trigger</div>
  221. </FilamentHoverCard>
  222. );
  223. vi.advanceTimersByTime(100);
  224. await waitFor(() => {
  225. expect(screen.getByText(/assign/i)).toBeInTheDocument();
  226. expect(screen.getByText(/assign/i).closest('button')).toBeDisabled();
  227. });
  228. });
  229. it('does not call onAssignSpool when the button is disabled via isAssigned', async () => {
  230. const onAssign = vi.fn();
  231. renderWithHover(
  232. <FilamentHoverCard
  233. data={{ ...baseFilamentData, vendor: 'Bambu Lab' }}
  234. inventory={{ assignedSpool: null, onAssignSpool: onAssign, isAssigned: true }}
  235. >
  236. <div>trigger</div>
  237. </FilamentHoverCard>
  238. );
  239. vi.advanceTimersByTime(100);
  240. await waitFor(() => expect(screen.getByText(/assign/i)).toBeInTheDocument());
  241. const btn = screen.getByText(/assign/i).closest('button')!;
  242. btn.click();
  243. expect(onAssign).not.toHaveBeenCalled();
  244. });
  245. });
  246. // For RFID-synced BL spools, both spoolman.linkedSpoolId and
  247. // inventory.assignedSpool.id point to the same Spoolman spool. Rendering
  248. // both branches gave two identical "Open in Inventory" buttons. The
  249. // inventory-side button is suppressed when it would duplicate the
  250. // spoolman-side link.
  251. describe('"Open in Inventory" deduplication', () => {
  252. const inventorySpool = {
  253. id: 42,
  254. material: 'PLA',
  255. subtype: null,
  256. brand: 'eSun',
  257. color_name: 'Black',
  258. };
  259. it('shows only one Open in Inventory button when spoolman linkedSpoolId matches assignedSpool id', async () => {
  260. renderWithHover(
  261. <FilamentHoverCard
  262. data={baseFilamentData}
  263. spoolman={{ enabled: true, linkedSpoolId: 42 }}
  264. inventory={{ assignedSpool: inventorySpool }}
  265. >
  266. <div>trigger</div>
  267. </FilamentHoverCard>
  268. );
  269. vi.advanceTimersByTime(100);
  270. await waitFor(() => {
  271. expect(screen.getByText(/assigned/i)).toBeInTheDocument();
  272. });
  273. expect(screen.queryAllByTitle('Open in Inventory')).toHaveLength(1);
  274. });
  275. it('shows two Open in Inventory buttons when spoolman and inventory point to different spools', async () => {
  276. renderWithHover(
  277. <FilamentHoverCard
  278. data={baseFilamentData}
  279. spoolman={{ enabled: true, linkedSpoolId: 99 }}
  280. inventory={{ assignedSpool: inventorySpool }}
  281. >
  282. <div>trigger</div>
  283. </FilamentHoverCard>
  284. );
  285. vi.advanceTimersByTime(100);
  286. await waitFor(() => {
  287. expect(screen.getByText(/assigned/i)).toBeInTheDocument();
  288. });
  289. expect(screen.queryAllByTitle('Open in Inventory')).toHaveLength(2);
  290. });
  291. it('shows one Open in Inventory button when spoolman is absent but inventory spool is assigned', async () => {
  292. renderWithHover(
  293. <FilamentHoverCard
  294. data={baseFilamentData}
  295. inventory={{ assignedSpool: inventorySpool }}
  296. >
  297. <div>trigger</div>
  298. </FilamentHoverCard>
  299. );
  300. vi.advanceTimersByTime(100);
  301. await waitFor(() => {
  302. expect(screen.getByText(/assigned/i)).toBeInTheDocument();
  303. });
  304. expect(screen.queryAllByTitle('Open in Inventory')).toHaveLength(1);
  305. });
  306. it('shows the spool ID in the assigned-spool block', async () => {
  307. renderWithHover(
  308. <FilamentHoverCard
  309. data={baseFilamentData}
  310. inventory={{ assignedSpool: inventorySpool }}
  311. >
  312. <div>trigger</div>
  313. </FilamentHoverCard>
  314. );
  315. vi.advanceTimersByTime(100);
  316. await waitFor(() => {
  317. expect(screen.getByText('#42')).toBeInTheDocument();
  318. });
  319. });
  320. });
  321. // The card is portaled at z-[60] — above ConfigureAmsSlotModal and
  322. // LinkSpoolModal at z-50 — so a card left standing draws OVER the dialog its
  323. // own button just opened. Mouseleave is the only thing that used to hide it,
  324. // and a touch device never sends one after the tap that opened the card, so on
  325. // a tablet it hung there indefinitely: two overlapping layers, competing focus.
  326. describe('dismissal when an action opens a dialog (#2631)', () => {
  327. it('closes the card when Configure is pressed, and still configures', async () => {
  328. const onConfigure = vi.fn();
  329. renderWithHover(
  330. <FilamentHoverCard
  331. data={baseFilamentData}
  332. configureSlot={{ enabled: true, onConfigure }}
  333. >
  334. <div>trigger</div>
  335. </FilamentHoverCard>
  336. );
  337. vi.advanceTimersByTime(100);
  338. await waitFor(() => expect(screen.getByText(/configure/i)).toBeInTheDocument());
  339. fireEvent.click(screen.getByText(/configure/i));
  340. expect(onConfigure).toHaveBeenCalledTimes(1);
  341. await waitFor(() => expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument());
  342. });
  343. it('stays closed with no mouseleave, which is all a tablet ever gives us', async () => {
  344. renderWithHover(
  345. <FilamentHoverCard
  346. data={baseFilamentData}
  347. configureSlot={{ enabled: true, onConfigure: vi.fn() }}
  348. >
  349. <div>trigger</div>
  350. </FilamentHoverCard>
  351. );
  352. vi.advanceTimersByTime(100);
  353. await waitFor(() => expect(screen.getByText(/configure/i)).toBeInTheDocument());
  354. fireEvent.click(screen.getByText(/configure/i));
  355. await waitFor(() => expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument());
  356. // A pending show timer would resurrect the card on top of the dialog.
  357. vi.advanceTimersByTime(1000);
  358. expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument();
  359. });
  360. it('closes the card when Assign Spool is pressed', async () => {
  361. const onAssignSpool = vi.fn();
  362. renderWithHover(
  363. <FilamentHoverCard
  364. data={baseFilamentData}
  365. inventory={{ assignedSpool: null, onAssignSpool }}
  366. >
  367. <div>trigger</div>
  368. </FilamentHoverCard>
  369. );
  370. vi.advanceTimersByTime(100);
  371. await waitFor(() => expect(screen.getByText(/assign/i)).toBeInTheDocument());
  372. fireEvent.click(screen.getByText(/assign/i));
  373. expect(onAssignSpool).toHaveBeenCalledTimes(1);
  374. await waitFor(() => expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument());
  375. });
  376. it('closes the card when Unassign Spool is pressed', async () => {
  377. const onUnassignSpool = vi.fn();
  378. renderWithHover(
  379. <FilamentHoverCard
  380. data={baseFilamentData}
  381. inventory={{
  382. assignedSpool: { id: 7, material: 'PLA', subtype: null, brand: 'eSun', color_name: 'Black' },
  383. onUnassignSpool,
  384. }}
  385. >
  386. <div>trigger</div>
  387. </FilamentHoverCard>
  388. );
  389. vi.advanceTimersByTime(100);
  390. await waitFor(() => expect(screen.getByText(/unassign/i)).toBeInTheDocument());
  391. fireEvent.click(screen.getByText(/unassign/i));
  392. expect(onUnassignSpool).toHaveBeenCalledTimes(1);
  393. await waitFor(() => expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument());
  394. });
  395. });
  396. });
  397. // EmptySlotHoverCard is the hover wrapper rendered for a physically empty
  398. // AMS slot. #1133 removed its inventory affordance: a slot with nothing
  399. // loaded has no spool to attach an inventory record to, and offering the
  400. // action there only led to users assigning the wrong spool to a slot the
  401. // printer hadn't actually loaded yet. The configure-slot affordance is
  402. // kept, since "preset for the next spool to land here" is still a sensible
  403. // thing to do on an empty slot.
  404. describe('EmptySlotHoverCard (#1133)', () => {
  405. beforeEach(() => {
  406. vi.useFakeTimers({ shouldAdvanceTime: true });
  407. });
  408. it('does not render an assign-spool button when onAssignSpool is not provided', async () => {
  409. const result = render(
  410. <EmptySlotHoverCard configureSlot={{ enabled: true, onConfigure: vi.fn() }}>
  411. <div>trigger</div>
  412. </EmptySlotHoverCard>
  413. );
  414. fireEvent.mouseEnter(result.container.firstElementChild as HTMLElement);
  415. vi.advanceTimersByTime(100);
  416. await waitFor(() => {
  417. // The card itself is showing — guard the negative assertion against
  418. // a card that simply never opened.
  419. expect(screen.getByText(/empty/i)).toBeInTheDocument();
  420. });
  421. expect(screen.queryByText(/assign spool/i)).not.toBeInTheDocument();
  422. });
  423. it('still shows the configure button on an empty slot', async () => {
  424. const onConfigure = vi.fn();
  425. const result = render(
  426. <EmptySlotHoverCard configureSlot={{ enabled: true, onConfigure }}>
  427. <div>trigger</div>
  428. </EmptySlotHoverCard>
  429. );
  430. fireEvent.mouseEnter(result.container.firstElementChild as HTMLElement);
  431. vi.advanceTimersByTime(100);
  432. await waitFor(() => {
  433. expect(screen.getByText(/configure/i)).toBeInTheDocument();
  434. });
  435. });
  436. it('shows Assign Spool button when onAssignSpool is provided', async () => {
  437. const onAssign = vi.fn();
  438. const result = render(
  439. <EmptySlotHoverCard onAssignSpool={onAssign}>
  440. <div>trigger</div>
  441. </EmptySlotHoverCard>
  442. );
  443. fireEvent.mouseEnter(result.container.firstElementChild as HTMLElement);
  444. vi.advanceTimersByTime(100);
  445. await waitFor(() => {
  446. expect(screen.getByText(/assign spool/i)).toBeInTheDocument();
  447. });
  448. });
  449. it('calls onAssignSpool when Assign Spool button is clicked', async () => {
  450. const onAssign = vi.fn();
  451. const result = render(
  452. <EmptySlotHoverCard onAssignSpool={onAssign}>
  453. <div>trigger</div>
  454. </EmptySlotHoverCard>
  455. );
  456. fireEvent.mouseEnter(result.container.firstElementChild as HTMLElement);
  457. vi.advanceTimersByTime(100);
  458. await waitFor(() => expect(screen.getByText(/assign spool/i)).toBeInTheDocument());
  459. fireEvent.click(screen.getByText(/assign spool/i));
  460. expect(onAssign).toHaveBeenCalledTimes(1);
  461. });
  462. // #2791: the empty-slot and filled-slot cards are separate render paths
  463. // that had drifted into opposite orders, so the menu reshuffled itself
  464. // depending on whether the slot happened to hold filament. Both now put
  465. // the spool action above the slot action; assert it on both paths so the
  466. // two can't drift apart again.
  467. it('lists Assign Spool above Configure, matching the filled-slot card (#2791)', async () => {
  468. const result = render(
  469. <EmptySlotHoverCard
  470. configureSlot={{ enabled: true, onConfigure: vi.fn() }}
  471. onAssignSpool={vi.fn()}
  472. >
  473. <div>trigger</div>
  474. </EmptySlotHoverCard>
  475. );
  476. fireEvent.mouseEnter(result.container.firstElementChild as HTMLElement);
  477. vi.advanceTimersByTime(100);
  478. await waitFor(() => expect(screen.getByText(/assign spool/i)).toBeInTheDocument());
  479. const assign = screen.getByText(/assign spool/i);
  480. const configure = screen.getByText(/^configure$/i);
  481. expect(assign.compareDocumentPosition(configure)).toBe(
  482. Node.DOCUMENT_POSITION_FOLLOWING
  483. );
  484. });
  485. // Same z-[60]-over-a-z-50-dialog problem as FilamentHoverCard (#2631).
  486. describe('dismissal when an action opens a dialog (#2631)', () => {
  487. it('closes the card when Configure is pressed, and still configures', async () => {
  488. const onConfigure = vi.fn();
  489. const result = render(
  490. <EmptySlotHoverCard configureSlot={{ enabled: true, onConfigure }}>
  491. <div>trigger</div>
  492. </EmptySlotHoverCard>
  493. );
  494. fireEvent.mouseEnter(result.container.firstElementChild as HTMLElement);
  495. vi.advanceTimersByTime(100);
  496. await waitFor(() => expect(screen.getByText(/configure/i)).toBeInTheDocument());
  497. fireEvent.click(screen.getByText(/configure/i));
  498. expect(onConfigure).toHaveBeenCalledTimes(1);
  499. await waitFor(() => expect(screen.queryByText(/empty/i)).not.toBeInTheDocument());
  500. });
  501. it('closes the card when Assign Spool is pressed', async () => {
  502. const onAssign = vi.fn();
  503. const result = render(
  504. <EmptySlotHoverCard onAssignSpool={onAssign}>
  505. <div>trigger</div>
  506. </EmptySlotHoverCard>
  507. );
  508. fireEvent.mouseEnter(result.container.firstElementChild as HTMLElement);
  509. vi.advanceTimersByTime(100);
  510. await waitFor(() => expect(screen.getByText(/assign spool/i)).toBeInTheDocument());
  511. fireEvent.click(screen.getByText(/assign spool/i));
  512. expect(onAssign).toHaveBeenCalledTimes(1);
  513. await waitFor(() => expect(screen.queryByText(/empty/i)).not.toBeInTheDocument());
  514. });
  515. });
  516. });
  517. describe('FilamentHoverCard colour name (#2875)', () => {
  518. beforeEach(() => {
  519. vi.useFakeTimers({ shouldAdvanceTime: true });
  520. __resetColorCatalogForTests();
  521. });
  522. const whiteMatte = {
  523. ...baseFilamentData,
  524. profile: 'Bambu PLA Matte',
  525. colorHex: 'FFFFFFFF',
  526. // What PrintersPage now resolves with the slot's own tray_sub_brands.
  527. colorName: 'Ivory White',
  528. };
  529. async function showCard(ui: React.ReactElement) {
  530. renderWithHover(ui);
  531. vi.advanceTimersByTime(100);
  532. }
  533. it('shows the resolved catalogue name for the slot', async () => {
  534. await showCard(
  535. <FilamentHoverCard data={whiteMatte}>
  536. <div>trigger</div>
  537. </FilamentHoverCard>
  538. );
  539. await waitFor(() => expect(screen.getByText('Ivory White')).toBeInTheDocument());
  540. expect(screen.queryByText('Jade White')).not.toBeInTheDocument();
  541. });
  542. it('prefers the assigned spool name, which is what the user put in the slot', async () => {
  543. await showCard(
  544. <FilamentHoverCard
  545. data={{ ...whiteMatte, colorName: 'Jade White' }}
  546. inventory={{
  547. isAssigned: true,
  548. assignedSpool: { id: 17, material: 'PLA', subtype: null, brand: 'Bambu Lab', color_name: 'Matte Ivory White' },
  549. }}
  550. >
  551. <div>trigger</div>
  552. </FilamentHoverCard>
  553. );
  554. await waitFor(() => expect(screen.getByText('Matte Ivory White')).toBeInTheDocument());
  555. expect(screen.queryByText('Jade White')).not.toBeInTheDocument();
  556. });
  557. it('ignores a Bambu internal colour code on the assigned spool', async () => {
  558. // "A06-D0" is not a name, and is not unique across material families
  559. // (#857) -- the catalogue answer stands.
  560. await showCard(
  561. <FilamentHoverCard
  562. data={whiteMatte}
  563. inventory={{
  564. isAssigned: true,
  565. assignedSpool: { id: 17, material: 'PLA', subtype: null, brand: 'Bambu Lab', color_name: 'A06-D0' },
  566. }}
  567. >
  568. <div>trigger</div>
  569. </FilamentHoverCard>
  570. );
  571. await waitFor(() => expect(screen.getByText('Ivory White')).toBeInTheDocument());
  572. expect(screen.queryByText('A06-D0')).not.toBeInTheDocument();
  573. });
  574. it.each([
  575. ['an empty colour name', ''],
  576. ['a whitespace-only colour name', ' '],
  577. ])('keeps the catalogue answer for a spool with %s', async (_label, colorName) => {
  578. await showCard(
  579. <FilamentHoverCard
  580. data={whiteMatte}
  581. inventory={{
  582. isAssigned: true,
  583. assignedSpool: { id: 17, material: 'PLA', subtype: null, brand: 'Bambu Lab', color_name: colorName },
  584. }}
  585. >
  586. <div>trigger</div>
  587. </FilamentHoverCard>
  588. );
  589. await waitFor(() => expect(screen.getByText('Ivory White')).toBeInTheDocument());
  590. });
  591. it('keeps the catalogue answer when a spool is assigned with no colour recorded', async () => {
  592. setColorCatalog({ ffffff: 'Jade White' }, { 'pla matte|ffffff': 'Ivory White' });
  593. await showCard(
  594. <FilamentHoverCard
  595. data={whiteMatte}
  596. inventory={{
  597. isAssigned: true,
  598. assignedSpool: { id: 17, material: 'PLA', subtype: null, brand: 'Bambu Lab', color_name: null },
  599. }}
  600. >
  601. <div>trigger</div>
  602. </FilamentHoverCard>
  603. );
  604. await waitFor(() => expect(screen.getByText('Ivory White')).toBeInTheDocument());
  605. });
  606. });
  607. // A spool's subtype is part of its name. Dropping it made a wood-filled roll
  608. // read as plain PLA on the slot card, which is the display-side version of
  609. // the mistake #2902 fixed on the backend -- and the printer, the inventory
  610. // page and Studio all named it correctly at the same time, so the card was
  611. // the only thing saying otherwise.
  612. describe('FilamentHoverCard assigned spool name', () => {
  613. beforeEach(() => {
  614. vi.useFakeTimers({ shouldAdvanceTime: true });
  615. __resetColorCatalogForTests();
  616. });
  617. function showAssigned(assignedSpool: {
  618. id: number;
  619. material: string;
  620. subtype: string | null;
  621. brand: string | null;
  622. color_name: string | null;
  623. }) {
  624. renderWithHover(
  625. <FilamentHoverCard data={baseFilamentData} inventory={{ isAssigned: true, assignedSpool }}>
  626. <div>trigger</div>
  627. </FilamentHoverCard>
  628. );
  629. vi.advanceTimersByTime(100);
  630. }
  631. it('names a filled filament by its subtype, not by its base material', async () => {
  632. showAssigned({
  633. id: 77,
  634. material: 'PLA',
  635. subtype: 'Wood',
  636. brand: 'Bambu Lab',
  637. color_name: 'Classic Birch',
  638. });
  639. await waitFor(() =>
  640. expect(screen.getByText('Bambu Lab PLA Wood - Classic Birch')).toBeInTheDocument()
  641. );
  642. });
  643. it('omits the subtype entirely for a spool that has none', async () => {
  644. showAssigned({
  645. id: 78,
  646. material: 'PLA',
  647. subtype: null,
  648. brand: 'Bambu Lab',
  649. color_name: 'Jade White',
  650. });
  651. await waitFor(() =>
  652. expect(screen.getByText('Bambu Lab PLA - Jade White')).toBeInTheDocument()
  653. );
  654. });
  655. });
  656. /**
  657. * The header swatch (#2967).
  658. *
  659. * A tray record carries one `tray_color` hex and nothing else, so telemetry can
  660. * never describe a gradient or a surface effect. The reporter's Ziro "Colorful
  661. * Mist" -- yellow, cyan and pink, effect Tri Color -- painted the header as one
  662. * flat band of the single hex the slot happened to be configured with. The
  663. * spool knows better, and the header now paints the spool's own swatch through
  664. * the same builder the Inventory swatches use.
  665. */
  666. describe('FilamentHoverCard — assigned spool swatch (#2967)', () => {
  667. beforeEach(() => {
  668. vi.useFakeTimers({ shouldAdvanceTime: true });
  669. });
  670. const triColorSpool = {
  671. id: 191,
  672. material: 'PLA',
  673. subtype: 'Matte',
  674. brand: 'Ziro',
  675. color_name: 'Colorful Mist',
  676. rgba: 'FFB6C1FF',
  677. extra_colors: 'ffff00,00ffff,ffb6c1',
  678. effect_type: 'tri-color',
  679. };
  680. // The swatch header is the card's first child: a fixed-height block carrying
  681. // the colour and the name. Queried off `document` rather than the render
  682. // container because the card is portaled into document.body.
  683. function header(): HTMLElement {
  684. const el = document.querySelector('.h-12') as HTMLElement | null;
  685. expect(el, 'header block not found').not.toBeNull();
  686. return el as HTMLElement;
  687. }
  688. it('paints the gradient for a multi-colour spool', async () => {
  689. renderWithHover(
  690. <FilamentHoverCard data={baseFilamentData} inventory={{ assignedSpool: triColorSpool }}>
  691. <div>trigger</div>
  692. </FilamentHoverCard>
  693. );
  694. vi.advanceTimersByTime(100);
  695. await waitFor(() => expect(screen.getByText('Colorful Mist')).toBeInTheDocument());
  696. expect(header().style.backgroundImage).toContain('gradient');
  697. });
  698. it('carries every stop the spool declared', async () => {
  699. renderWithHover(
  700. <FilamentHoverCard data={baseFilamentData} inventory={{ assignedSpool: triColorSpool }}>
  701. <div>trigger</div>
  702. </FilamentHoverCard>
  703. );
  704. vi.advanceTimersByTime(100);
  705. await waitFor(() => expect(screen.getByText('Colorful Mist')).toBeInTheDocument());
  706. const image = header().style.backgroundImage.toLowerCase();
  707. expect(image).toContain('#ffff00');
  708. expect(image).toContain('#00ffff');
  709. expect(image).toContain('#ffb6c1');
  710. });
  711. it('leaves a plain single-colour spool on the flat slot colour', async () => {
  712. // The common case must not go anywhere near the gradient machinery.
  713. renderWithHover(
  714. <FilamentHoverCard
  715. data={baseFilamentData}
  716. inventory={{
  717. assignedSpool: { ...triColorSpool, extra_colors: null, effect_type: null },
  718. }}
  719. >
  720. <div>trigger</div>
  721. </FilamentHoverCard>
  722. );
  723. vi.advanceTimersByTime(100);
  724. await waitFor(() => expect(screen.getByText('Colorful Mist')).toBeInTheDocument());
  725. expect(header().style.backgroundImage).toBe('');
  726. });
  727. it('paints the swatch for an effect with no extra colours', async () => {
  728. // A silk roll is one colour with a surface the hex cannot express.
  729. renderWithHover(
  730. <FilamentHoverCard
  731. data={baseFilamentData}
  732. inventory={{ assignedSpool: { ...triColorSpool, extra_colors: null, effect_type: 'silk' } }}
  733. >
  734. <div>trigger</div>
  735. </FilamentHoverCard>
  736. );
  737. vi.advanceTimersByTime(100);
  738. await waitFor(() => expect(screen.getByText('Colorful Mist')).toBeInTheDocument());
  739. expect(header().style.backgroundImage).not.toBe('');
  740. });
  741. it('puts the name on a scrim when the background has several bands', async () => {
  742. // One hex cannot decide legibility across yellow, cyan and pink, and the
  743. // label sits dead centre where the background is most likely to change.
  744. renderWithHover(
  745. <FilamentHoverCard data={baseFilamentData} inventory={{ assignedSpool: triColorSpool }}>
  746. <div>trigger</div>
  747. </FilamentHoverCard>
  748. );
  749. vi.advanceTimersByTime(100);
  750. await waitFor(() => expect(screen.getByText('Colorful Mist')).toBeInTheDocument());
  751. expect(screen.getByText('Colorful Mist').className).toContain('bg-black/60');
  752. });
  753. it('does not scrim a single-colour spool', async () => {
  754. renderWithHover(
  755. <FilamentHoverCard
  756. data={baseFilamentData}
  757. inventory={{
  758. assignedSpool: { ...triColorSpool, extra_colors: null, effect_type: null },
  759. }}
  760. >
  761. <div>trigger</div>
  762. </FilamentHoverCard>
  763. );
  764. vi.advanceTimersByTime(100);
  765. await waitFor(() => expect(screen.getByText('Colorful Mist')).toBeInTheDocument());
  766. expect(screen.getByText('Colorful Mist').className).not.toContain('bg-black/60');
  767. });
  768. it('keeps a slot with no assigned spool exactly as it was', async () => {
  769. renderWithHover(
  770. <FilamentHoverCard data={baseFilamentData} inventory={{ assignedSpool: null }}>
  771. <div>trigger</div>
  772. </FilamentHoverCard>
  773. );
  774. vi.advanceTimersByTime(100);
  775. await waitFor(() => expect(screen.getByText('Red')).toBeInTheDocument());
  776. expect(header().style.backgroundImage).toBe('');
  777. });
  778. it('tolerates a spool that predates the swatch fields', async () => {
  779. // Older callers send neither rgba nor extra_colors; the optional fields
  780. // must not turn that into a blank or a crash.
  781. renderWithHover(
  782. <FilamentHoverCard
  783. data={baseFilamentData}
  784. inventory={{
  785. assignedSpool: { id: 1, material: 'PLA', subtype: null, brand: 'Ziro', color_name: 'Mist' },
  786. }}
  787. >
  788. <div>trigger</div>
  789. </FilamentHoverCard>
  790. );
  791. vi.advanceTimersByTime(100);
  792. await waitFor(() => expect(screen.getByText('Mist')).toBeInTheDocument());
  793. expect(header().style.backgroundImage).toBe('');
  794. });
  795. });
  796. // A phone has no hover: the tile is a button that opens its card on a tap
  797. // (#3577). jsdom has no PointerEvent, so the press carries its device by hand.
  798. function press(el: Element, pointerType: 'touch' | 'mouse' | 'pen') {
  799. const ev = new Event('pointerdown', { bubbles: true, cancelable: true });
  800. Object.defineProperty(ev, 'pointerType', { value: pointerType });
  801. el.dispatchEvent(ev);
  802. }
  803. function tap(el: Element, pointerType: 'touch' | 'mouse' | 'pen' = 'touch') {
  804. press(el, pointerType);
  805. fireEvent.click(el);
  806. }
  807. describe('slot cards on a touch screen (#3577)', () => {
  808. beforeEach(() => {
  809. vi.useFakeTimers({ shouldAdvanceTime: true });
  810. });
  811. function renderSlot(props: Partial<React.ComponentProps<typeof FilamentHoverCard>> = {}) {
  812. render(
  813. <FilamentHoverCard data={baseFilamentData} configureSlot={{ enabled: true, onConfigure: vi.fn() }} {...props}>
  814. <div>PLA</div>
  815. </FilamentHoverCard>,
  816. );
  817. return screen.getByTestId('filament-slot');
  818. }
  819. it('is a button for the whole tile, closed at first', () => {
  820. const tile = renderSlot();
  821. expect(tile).toHaveAttribute('role', 'button');
  822. expect(tile).toHaveAttribute('tabindex', '0');
  823. expect(tile).toHaveAttribute('aria-expanded', 'false');
  824. });
  825. it('opens on a tap on the material text and closes on a second tap', async () => {
  826. const tile = renderSlot();
  827. tap(screen.getByText('PLA'));
  828. expect(screen.getByText('PLA Basic')).toBeInTheDocument();
  829. expect(tile).toHaveAttribute('aria-expanded', 'true');
  830. tap(screen.getByText('PLA'));
  831. expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument();
  832. });
  833. it("is not reopened by the mouseenter a browser emulates for the tap", async () => {
  834. const tile = renderSlot();
  835. fireEvent.mouseEnter(tile);
  836. tap(tile);
  837. vi.advanceTimersByTime(200);
  838. expect(screen.getByText('PLA Basic')).toBeInTheDocument();
  839. tap(tile);
  840. vi.advanceTimersByTime(200);
  841. expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument();
  842. });
  843. it('toggles on a pen too', () => {
  844. const tile = renderSlot();
  845. tap(tile, 'pen');
  846. expect(screen.getByText('PLA Basic')).toBeInTheDocument();
  847. tap(tile, 'pen');
  848. expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument();
  849. });
  850. it('stays open for a mouse click on a card its hover opened', async () => {
  851. const tile = renderSlot();
  852. fireEvent.mouseEnter(tile);
  853. vi.advanceTimersByTime(100);
  854. await waitFor(() => expect(screen.getByText('PLA Basic')).toBeInTheDocument());
  855. tap(tile, 'mouse');
  856. expect(screen.getByText('PLA Basic')).toBeInTheDocument();
  857. });
  858. it('closes on a tap outside, not on a tap inside the card', async () => {
  859. renderSlot();
  860. tap(screen.getByText('PLA'));
  861. press(screen.getByText('PLA Basic'), 'touch');
  862. // The close would land asynchronously, from a document listener.
  863. await act(async () => {
  864. vi.advanceTimersByTime(50);
  865. });
  866. expect(screen.getByText('PLA Basic')).toBeInTheDocument();
  867. press(document.body, 'touch');
  868. await waitFor(() => expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument());
  869. });
  870. it('keeps one card open at a time', () => {
  871. render(
  872. <>
  873. <FilamentHoverCard data={baseFilamentData}>
  874. <div>first</div>
  875. </FilamentHoverCard>
  876. <FilamentHoverCard data={{ ...baseFilamentData, profile: 'PETG HF' }}>
  877. <div>second</div>
  878. </FilamentHoverCard>
  879. </>,
  880. );
  881. tap(screen.getByText('first'));
  882. tap(screen.getByText('second'));
  883. expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument();
  884. expect(screen.getByText('PETG HF')).toBeInTheDocument();
  885. });
  886. it('opens with Enter, moves into the card, and Escape returns to the tile', async () => {
  887. const tile = renderSlot();
  888. tile.focus();
  889. fireEvent.keyDown(tile, { key: 'Enter' });
  890. expect(screen.getByText('PLA Basic')).toBeInTheDocument();
  891. vi.advanceTimersByTime(50);
  892. await waitFor(() => expect(document.activeElement?.closest('.fixed')).not.toBeNull());
  893. fireEvent.keyDown(document.activeElement!, { key: 'Escape' });
  894. expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument();
  895. expect(document.activeElement).toBe(tile);
  896. });
  897. it('closes with Space', () => {
  898. const tile = renderSlot();
  899. fireEvent.keyDown(tile, { key: ' ' });
  900. expect(screen.getByText('PLA Basic')).toBeInTheDocument();
  901. fireEvent.keyDown(tile, { key: ' ' });
  902. expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument();
  903. });
  904. it('is no button when disabled, and a tap opens nothing', () => {
  905. const tile = renderSlot({ disabled: true });
  906. expect(tile).not.toHaveAttribute('role');
  907. tap(tile);
  908. fireEvent.keyDown(tile, { key: 'Enter' });
  909. expect(screen.queryByText('PLA Basic')).not.toBeInTheDocument();
  910. });
  911. it('opens an empty slot on a tap too, and closes it on a tap outside', async () => {
  912. render(
  913. <EmptySlotHoverCard configureSlot={{ enabled: true, onConfigure: vi.fn() }}>
  914. <div>Empty</div>
  915. </EmptySlotHoverCard>,
  916. );
  917. const tile = screen.getByTestId('empty-slot');
  918. expect(tile).toHaveAttribute('role', 'button');
  919. tap(screen.getByText('Empty'));
  920. expect(screen.getByText(/configure/i)).toBeInTheDocument();
  921. press(document.body, 'touch');
  922. await waitFor(() => expect(screen.queryByText(/configure/i)).not.toBeInTheDocument());
  923. });
  924. });