|
| 1 | +import { expect, it, vi } from "vite-plus/test"; |
| 2 | +import { createSidebarListMotion } from "./Sidebar.motion"; |
| 3 | + |
| 4 | +function mountList(names: readonly string[]) { |
| 5 | + const list = document.createElement("ul"); |
| 6 | + list.style.position = "relative"; |
| 7 | + const rows = new Map<string, HTMLLIElement>(); |
| 8 | + for (const name of names) { |
| 9 | + const row = document.createElement("li"); |
| 10 | + row.style.height = "20px"; |
| 11 | + row.textContent = name; |
| 12 | + rows.set(name, row); |
| 13 | + list.append(row); |
| 14 | + } |
| 15 | + document.body.append(list); |
| 16 | + const motion = createSidebarListMotion(list); |
| 17 | + motion.update(false); |
| 18 | + return { |
| 19 | + list, |
| 20 | + rows, |
| 21 | + motion, |
| 22 | + [Symbol.dispose]: () => { |
| 23 | + motion.dispose(); |
| 24 | + list.remove(); |
| 25 | + }, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +it("does not queue row motion while the document is hidden", () => { |
| 30 | + using fixture = mountList(["a", "b"]); |
| 31 | + using visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden"); |
| 32 | + fixture.list.prepend(fixture.rows.get("b")!); |
| 33 | + fixture.motion.update(true); |
| 34 | + expect(fixture.list.getAnimations({ subtree: true })).toHaveLength(0); |
| 35 | + |
| 36 | + visibility.mockReturnValue("visible"); |
| 37 | + fixture.list.prepend(fixture.rows.get("a")!); |
| 38 | + fixture.motion.update(true); |
| 39 | + expect(fixture.list.getAnimations({ subtree: true }).length).toBeGreaterThan(0); |
| 40 | +}); |
| 41 | + |
| 42 | +it("drops in-flight row motion when the document becomes hidden", () => { |
| 43 | + using fixture = mountList(["a", "b"]); |
| 44 | + using visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("visible"); |
| 45 | + fixture.list.prepend(fixture.rows.get("b")!); |
| 46 | + fixture.motion.update(true); |
| 47 | + expect(fixture.list.getAnimations({ subtree: true }).length).toBeGreaterThan(0); |
| 48 | + |
| 49 | + visibility.mockReturnValue("hidden"); |
| 50 | + document.dispatchEvent(new Event("visibilitychange")); |
| 51 | + expect(fixture.list.getAnimations({ subtree: true })).toHaveLength(0); |
| 52 | +}); |
0 commit comments