useRunningBackgroundTaskElapsedClock.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { act, createElement } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { useRunningBackgroundTaskElapsedClock } from "@/hooks/useRunningBackgroundTaskElapsedClock.js";
  5. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  6. const element = {
  7. addEventListener: () => {},
  8. appendChild: (child: { parentNode?: unknown }) => {
  9. child.parentNode = element;
  10. return child;
  11. },
  12. childNodes: [] as unknown[],
  13. getAttribute: () => null,
  14. insertBefore: (child: { parentNode?: unknown }) => {
  15. child.parentNode = element;
  16. return child;
  17. },
  18. nodeName: tagName.toUpperCase(),
  19. nodeType: 1,
  20. ownerDocument,
  21. parentNode: null as unknown,
  22. removeChild: (child: { parentNode?: unknown }) => {
  23. child.parentNode = null;
  24. return child;
  25. },
  26. removeEventListener: () => {},
  27. removeAttribute: () => {},
  28. setAttribute: () => {},
  29. style: {},
  30. tagName: tagName.toUpperCase(),
  31. };
  32. return element as unknown as Element;
  33. }
  34. function installMinimalDom() {
  35. const documentMock = {
  36. addEventListener: () => {},
  37. createElement: (tagName: string) =>
  38. createMinimalElement(documentMock as unknown as Document, tagName),
  39. createTextNode: (nodeValue: string) => ({
  40. nodeType: 3,
  41. nodeValue,
  42. ownerDocument: documentMock,
  43. parentNode: null,
  44. }),
  45. nodeType: 9,
  46. removeEventListener: () => {},
  47. } as unknown as Document;
  48. const windowMock = {
  49. document: documentMock,
  50. HTMLIFrameElement: function HTMLIFrameElement() {},
  51. HTMLElement: function HTMLElement() {},
  52. Node: function Node() {},
  53. };
  54. Object.defineProperty(globalThis, "document", {
  55. configurable: true,
  56. value: documentMock,
  57. });
  58. Object.defineProperty(globalThis, "window", {
  59. configurable: true,
  60. value: windowMock,
  61. });
  62. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  63. configurable: true,
  64. value: true,
  65. });
  66. return createMinimalElement(documentMock);
  67. }
  68. afterEach(() => {
  69. vi.useRealTimers();
  70. delete (globalThis as { document?: unknown }).document;
  71. delete (globalThis as { window?: unknown }).window;
  72. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown })
  73. .IS_REACT_ACT_ENVIRONMENT;
  74. });
  75. describe("useRunningBackgroundTaskElapsedClock", () => {
  76. it("keeps the existing second boundary when one of several running tasks completes", () => {
  77. vi.useFakeTimers();
  78. vi.setSystemTime(100_000);
  79. const root: Root = createRoot(installMinimalDom());
  80. let latestNow = 0;
  81. function Harness({ runningTaskCount }: { runningTaskCount: number }) {
  82. latestNow = useRunningBackgroundTaskElapsedClock(runningTaskCount);
  83. return null;
  84. }
  85. act(() => {
  86. root.render(createElement(Harness, { runningTaskCount: 2 }));
  87. });
  88. expect(latestNow).toBe(100_000);
  89. act(() => {
  90. vi.advanceTimersByTime(900);
  91. root.render(createElement(Harness, { runningTaskCount: 1 }));
  92. });
  93. act(() => {
  94. vi.advanceTimersByTime(100);
  95. });
  96. expect(latestNow).toBe(101_000);
  97. act(() => {
  98. root.unmount();
  99. });
  100. });
  101. });