|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { createElement } from "react"; |
| 3 | +import { createRoot, type Root } from "react-dom/client"; |
| 4 | +import { act } from "react-dom/test-utils"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import { |
| 7 | + flattenTree, |
| 8 | + useTree, |
| 9 | + type FlatTreeItem, |
| 10 | + type UseTreeStateOutput, |
| 11 | +} from "~/components/primitives/TreeView/TreeView"; |
| 12 | + |
| 13 | +type Data = { title: string }; |
| 14 | + |
| 15 | +const tree = flattenTree<Data>({ |
| 16 | + id: "root", |
| 17 | + data: { title: "root" }, |
| 18 | + children: [ |
| 19 | + { |
| 20 | + id: "a", |
| 21 | + data: { title: "a" }, |
| 22 | + children: [ |
| 23 | + { id: "a1", data: { title: "a1" } }, |
| 24 | + { id: "a2", data: { title: "a2" } }, |
| 25 | + ], |
| 26 | + }, |
| 27 | + { id: "b", data: { title: "b" } }, |
| 28 | + ], |
| 29 | +}); |
| 30 | + |
| 31 | +let container: HTMLDivElement | undefined; |
| 32 | +let root: Root | undefined; |
| 33 | + |
| 34 | +afterEach(() => { |
| 35 | + if (root) act(() => root!.unmount()); |
| 36 | + container?.remove(); |
| 37 | + container = undefined; |
| 38 | + root = undefined; |
| 39 | +}); |
| 40 | + |
| 41 | +function render() { |
| 42 | + let output: UseTreeStateOutput | undefined; |
| 43 | + const estimates = new Map<number, string>(); |
| 44 | + const parentRef = { current: null }; |
| 45 | + const estimatedRowHeight = ({ node, index }: { node: FlatTreeItem<Data>; index: number }) => { |
| 46 | + estimates.set(index, node.id); |
| 47 | + return 32; |
| 48 | + }; |
| 49 | + let filterCalls = 0; |
| 50 | + const filter = { |
| 51 | + value: "match-all", |
| 52 | + fn: () => { |
| 53 | + filterCalls++; |
| 54 | + return true; |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + function Harness({ tick: _tick }: { tick: number }) { |
| 59 | + output = useTree({ tree, parentRef, estimatedRowHeight, filter }); |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + let tick = 0; |
| 64 | + container = document.createElement("div"); |
| 65 | + document.body.appendChild(container); |
| 66 | + root = createRoot(container); |
| 67 | + act(() => root!.render(createElement(Harness, { tick }))); |
| 68 | + |
| 69 | + return { |
| 70 | + output: () => output!, |
| 71 | + estimates, |
| 72 | + filterCalls: () => filterCalls, |
| 73 | + rerender: () => { |
| 74 | + tick++; |
| 75 | + act(() => root!.render(createElement(Harness, { tick }))); |
| 76 | + }, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +describe("useTree virtualizer options", () => { |
| 81 | + it("keeps getItemKey identity across re-renders with unchanged rows", () => { |
| 82 | + const harness = render(); |
| 83 | + const before = harness.output().virtualizer.options.getItemKey; |
| 84 | + |
| 85 | + harness.rerender(); |
| 86 | + |
| 87 | + expect(harness.output().virtualizer.options.getItemKey).toBe(before); |
| 88 | + }); |
| 89 | + |
| 90 | + it("changes getItemKey when the visible rows change", () => { |
| 91 | + const harness = render(); |
| 92 | + const before = harness.output().virtualizer.options.getItemKey; |
| 93 | + |
| 94 | + act(() => harness.output().collapseNode("a")); |
| 95 | + |
| 96 | + expect(harness.output().virtualizer.options.getItemKey).not.toBe(before); |
| 97 | + }); |
| 98 | + |
| 99 | + it("doesn't rebuild the initial tree state on re-render", () => { |
| 100 | + const harness = render(); |
| 101 | + const before = harness.filterCalls(); |
| 102 | + |
| 103 | + harness.rerender(); |
| 104 | + |
| 105 | + expect(harness.filterCalls()).toBe(before); |
| 106 | + }); |
| 107 | + |
| 108 | + it("estimates each row from the node shown in that row", () => { |
| 109 | + const harness = render(); |
| 110 | + act(() => harness.output().collapseNode("a")); |
| 111 | + harness.estimates.clear(); |
| 112 | + |
| 113 | + harness.output().virtualizer.getTotalSize(); |
| 114 | + |
| 115 | + expect([...harness.estimates.entries()].sort(([a], [b]) => a - b)).toEqual([ |
| 116 | + [0, "root"], |
| 117 | + [1, "a"], |
| 118 | + [2, "b"], |
| 119 | + ]); |
| 120 | + }); |
| 121 | +}); |
0 commit comments