useHighspeedComposerEntrance.test.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // @vitest-environment jsdom
  2. import { renderHook } from "@testing-library/react";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { useHighspeedComposerEntrance } from "@/highspeed/useHighspeedComposerEntrance.js";
  5. type EntranceProps = {
  6. cardId: string | null;
  7. activationCardId: string | null;
  8. };
  9. function renderEntrance(initialProps: EntranceProps) {
  10. const onActivationApplied = vi.fn();
  11. const hook = renderHook(
  12. (props: EntranceProps) => useHighspeedComposerEntrance({ ...props, onActivationApplied }),
  13. { initialProps },
  14. );
  15. return { ...hook, onActivationApplied };
  16. }
  17. describe("useHighspeedComposerEntrance", () => {
  18. it("draw 新命中播放一次激活,锁定后立即消费请求,请求清空后仍保持本次激活", () => {
  19. const { result, rerender, onActivationApplied } = renderEntrance({
  20. cardId: null,
  21. activationCardId: null,
  22. });
  23. rerender({ cardId: "card-1", activationCardId: "card-1" });
  24. expect(result.current).toBe("activation");
  25. expect(onActivationApplied).toHaveBeenCalledTimes(1);
  26. expect(onActivationApplied).toHaveBeenCalledWith("card-1");
  27. // 宿主消费后清空请求:同一张卡的这次入场不能因此被改判为 restore,否则动效会中途跳到终态。
  28. rerender({ cardId: "card-1", activationCardId: null });
  29. expect(result.current).toBe("activation");
  30. expect(onActivationApplied).toHaveBeenCalledTimes(1);
  31. });
  32. it("没有激活请求时呈现的卡(切回 Task、快照恢复)直接按 restore 入场", () => {
  33. const { result, rerender, onActivationApplied } = renderEntrance({
  34. cardId: null,
  35. activationCardId: null,
  36. });
  37. rerender({ cardId: "card-1", activationCardId: null });
  38. expect(result.current).toBe("restore");
  39. expect(onActivationApplied).not.toHaveBeenCalled();
  40. });
  41. it("激活过的卡切走再切回时不重播", () => {
  42. const { result, rerender } = renderEntrance({ cardId: null, activationCardId: null });
  43. rerender({ cardId: "card-1", activationCardId: "card-1" });
  44. rerender({ cardId: "card-1", activationCardId: null });
  45. rerender({ cardId: null, activationCardId: null });
  46. rerender({ cardId: "card-1", activationCardId: null });
  47. expect(result.current).toBe("restore");
  48. });
  49. it("组件重挂载(如退出分享模式)时同一张卡按 restore 入场", () => {
  50. const { result, onActivationApplied } = renderEntrance({
  51. cardId: "card-1",
  52. activationCardId: null,
  53. });
  54. expect(result.current).toBe("restore");
  55. expect(onActivationApplied).not.toHaveBeenCalled();
  56. });
  57. it("激活请求与当前卡不一致时不播放也不消费", () => {
  58. const { result, onActivationApplied } = renderEntrance({
  59. cardId: "card-2",
  60. activationCardId: "card-1",
  61. });
  62. expect(result.current).toBe("restore");
  63. expect(onActivationApplied).not.toHaveBeenCalled();
  64. });
  65. it("挂载时已有匹配的待消费请求(draw 命中时 composer 未挂载)仍播放一次", () => {
  66. const { result, onActivationApplied } = renderEntrance({
  67. cardId: "card-1",
  68. activationCardId: "card-1",
  69. });
  70. expect(result.current).toBe("activation");
  71. expect(onActivationApplied).toHaveBeenCalledTimes(1);
  72. expect(onActivationApplied).toHaveBeenCalledWith("card-1");
  73. });
  74. });