|
8 | 8 | // by the Apache License, Version 2.0. |
9 | 9 |
|
10 | 10 | import { act, renderHook, waitFor } from "@testing-library/react"; |
11 | | -import { describe, expect, it, vi } from "vitest"; |
| 11 | +import React from "react"; |
| 12 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
12 | 13 |
|
| 14 | +import { THRESHOLD_DRAG_SETTLE_MS } from "./constants"; |
13 | 15 | import { useThresholdControl } from "./useThresholdControl"; |
14 | 16 |
|
15 | 17 | describe("useThresholdControl", () => { |
@@ -90,6 +92,52 @@ describe("useThresholdControl", () => { |
90 | 92 | expect(result.current.inputProps.valueMs).toBe(9_000); |
91 | 93 | }); |
92 | 94 |
|
| 95 | + describe("with a commit that lands in a transition", () => { |
| 96 | + // `BrowserRouter` navigates inside `startTransition`, so this is how a |
| 97 | + // search-param `commit` behaves on the page. |
| 98 | + const renderWithTransitionCommit = () => { |
| 99 | + const shown: number[] = []; |
| 100 | + const hook = renderHook(() => { |
| 101 | + const [committed, setCommitted] = React.useState(2_000); |
| 102 | + const control = useThresholdControl(committed, (value) => |
| 103 | + React.startTransition(() => setCommitted(value)), |
| 104 | + ); |
| 105 | + const threshold = control.graphProps.threshold; |
| 106 | + React.useLayoutEffect(() => { |
| 107 | + if (shown.at(-1) !== threshold) shown.push(threshold); |
| 108 | + }); |
| 109 | + return control; |
| 110 | + }); |
| 111 | + return { ...hook, shown }; |
| 112 | + }; |
| 113 | + |
| 114 | + afterEach(() => vi.useRealTimers()); |
| 115 | + |
| 116 | + it("never shows the old value when a drag settles", () => { |
| 117 | + vi.useFakeTimers(); |
| 118 | + const { result, shown } = renderWithTransitionCommit(); |
| 119 | + |
| 120 | + act(() => result.current.graphProps.onThresholdChange(5_000)); |
| 121 | + act(() => vi.advanceTimersByTime(THRESHOLD_DRAG_SETTLE_MS)); |
| 122 | + |
| 123 | + expect(shown).toEqual([2_000, 5_000]); |
| 124 | + }); |
| 125 | + |
| 126 | + it("keeps a new drag that starts before the last one lands", () => { |
| 127 | + vi.useFakeTimers(); |
| 128 | + const { result, shown } = renderWithTransitionCommit(); |
| 129 | + |
| 130 | + act(() => result.current.graphProps.onThresholdChange(5_000)); |
| 131 | + act(() => { |
| 132 | + vi.advanceTimersByTime(THRESHOLD_DRAG_SETTLE_MS); |
| 133 | + result.current.graphProps.onThresholdChange(7_000); |
| 134 | + }); |
| 135 | + |
| 136 | + expect(shown.at(-1)).toBe(7_000); |
| 137 | + expect(shown.slice(1)).not.toContain(2_000); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
93 | 141 | it("keeps each prop object stable while nothing changes", () => { |
94 | 142 | const commit = vi.fn(); |
95 | 143 | const { result, rerender } = renderHook(() => |
|
0 commit comments