|
| 1 | +import { act, fireEvent } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | + |
| 4 | +import { renderApp, flushAnimationFrames } from "../../utils"; |
| 5 | + |
| 6 | +function fireTrackpadWheel( |
| 7 | + target: HTMLElement, |
| 8 | + deltaY: number, |
| 9 | + count: number, |
| 10 | +) { |
| 11 | + for (let i = 0; i < count; i++) { |
| 12 | + fireEvent( |
| 13 | + target, |
| 14 | + new WheelEvent("wheel", { |
| 15 | + bubbles: true, |
| 16 | + deltaY, |
| 17 | + ctrlKey: true, |
| 18 | + }), |
| 19 | + ); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function fireMouseWheel( |
| 24 | + target: HTMLElement, |
| 25 | + deltaY: number, |
| 26 | + count: number, |
| 27 | +) { |
| 28 | + for (let i = 0; i < count; i++) { |
| 29 | + fireEvent( |
| 30 | + target, |
| 31 | + new WheelEvent("wheel", { |
| 32 | + bubbles: true, |
| 33 | + deltaY, |
| 34 | + ctrlKey: false, |
| 35 | + }), |
| 36 | + ); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +describe("Zoom [Elastic bounds / rubberband]", () => { |
| 41 | + afterEach(() => { |
| 42 | + jest.useRealTimers(); |
| 43 | + }); |
| 44 | + |
| 45 | + describe("trackpad pinch (ctrlKey + wheel) allows elastic overshoot", () => { |
| 46 | + it("should allow scale to temporarily go below minScale during gesture", () => { |
| 47 | + const { content, ref } = renderApp({ |
| 48 | + minScale: 0.5, |
| 49 | + wheel: { step: 0.2 }, |
| 50 | + }); |
| 51 | + |
| 52 | + userEvent.hover(content); |
| 53 | + fireTrackpadWheel(content, 50, 30); |
| 54 | + |
| 55 | + expect(ref.current!.instance.state.scale).toBeLessThan(0.5); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should allow scale to temporarily go above maxScale during gesture", () => { |
| 59 | + const { content, ref } = renderApp({ |
| 60 | + maxScale: 3, |
| 61 | + wheel: { step: 0.2 }, |
| 62 | + }); |
| 63 | + |
| 64 | + userEvent.hover(content); |
| 65 | + fireTrackpadWheel(content, -50, 30); |
| 66 | + |
| 67 | + expect(ref.current!.instance.state.scale).toBeGreaterThan(3); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should snap back to minScale after gesture ends (zoom out)", () => { |
| 71 | + jest.useFakeTimers(); |
| 72 | + const { content, ref } = renderApp({ |
| 73 | + minScale: 0.5, |
| 74 | + wheel: { step: 0.2 }, |
| 75 | + }); |
| 76 | + |
| 77 | + userEvent.hover(content); |
| 78 | + fireTrackpadWheel(content, 50, 30); |
| 79 | + |
| 80 | + const scaleDuringGesture = ref.current!.instance.state.scale; |
| 81 | + expect(scaleDuringGesture).toBeLessThan(0.5); |
| 82 | + |
| 83 | + act(() => { |
| 84 | + jest.advanceTimersByTime(300); |
| 85 | + flushAnimationFrames(60); |
| 86 | + }); |
| 87 | + |
| 88 | + expect(ref.current!.instance.state.scale).toBeGreaterThanOrEqual(0.5); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should snap back to maxScale after gesture ends (zoom in)", () => { |
| 92 | + jest.useFakeTimers(); |
| 93 | + const { content, ref } = renderApp({ |
| 94 | + maxScale: 3, |
| 95 | + wheel: { step: 0.2 }, |
| 96 | + }); |
| 97 | + |
| 98 | + userEvent.hover(content); |
| 99 | + fireTrackpadWheel(content, -50, 30); |
| 100 | + |
| 101 | + const scaleDuringGesture = ref.current!.instance.state.scale; |
| 102 | + expect(scaleDuringGesture).toBeGreaterThan(3); |
| 103 | + |
| 104 | + act(() => { |
| 105 | + jest.advanceTimersByTime(300); |
| 106 | + flushAnimationFrames(60); |
| 107 | + }); |
| 108 | + |
| 109 | + expect(ref.current!.instance.state.scale).toBeLessThanOrEqual(3); |
| 110 | + }); |
| 111 | + |
| 112 | + it("overshoot is bounded by zoomAnimation.size padding", () => { |
| 113 | + const zoomPadding = 0.4; |
| 114 | + const { content, ref } = renderApp({ |
| 115 | + minScale: 1, |
| 116 | + maxScale: 3, |
| 117 | + wheel: { step: 0.2 }, |
| 118 | + zoomAnimation: { size: zoomPadding }, |
| 119 | + }); |
| 120 | + |
| 121 | + userEvent.hover(content); |
| 122 | + fireTrackpadWheel(content, 50, 100); |
| 123 | + |
| 124 | + const scale = ref.current!.instance.state.scale; |
| 125 | + expect(scale).toBeGreaterThanOrEqual(1 - zoomPadding); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe("regular mouse wheel does NOT allow elastic overshoot", () => { |
| 130 | + it("should hard-clamp at minScale during wheel zoom out", () => { |
| 131 | + const { content, ref } = renderApp({ |
| 132 | + minScale: 0.5, |
| 133 | + wheel: { step: 0.2 }, |
| 134 | + }); |
| 135 | + |
| 136 | + userEvent.hover(content); |
| 137 | + for (let i = 0; i < 30; i++) { |
| 138 | + fireMouseWheel(content, 50, 1); |
| 139 | + expect(ref.current!.instance.state.scale).toBeGreaterThanOrEqual(0.5); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + it("should hard-clamp at maxScale during wheel zoom in", () => { |
| 144 | + const { content, ref } = renderApp({ |
| 145 | + maxScale: 3, |
| 146 | + wheel: { step: 0.2 }, |
| 147 | + }); |
| 148 | + |
| 149 | + userEvent.hover(content); |
| 150 | + for (let i = 0; i < 30; i++) { |
| 151 | + fireMouseWheel(content, -50, 1); |
| 152 | + expect(ref.current!.instance.state.scale).toBeLessThanOrEqual(3); |
| 153 | + } |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe("disablePadding prevents elastic overshoot", () => { |
| 158 | + it("should hard-clamp at minScale even for trackpad pinch", () => { |
| 159 | + const { content, ref } = renderApp({ |
| 160 | + minScale: 0.5, |
| 161 | + disablePadding: true, |
| 162 | + wheel: { step: 0.2 }, |
| 163 | + }); |
| 164 | + |
| 165 | + userEvent.hover(content); |
| 166 | + for (let i = 0; i < 30; i++) { |
| 167 | + fireTrackpadWheel(content, 50, 1); |
| 168 | + expect(ref.current!.instance.state.scale).toBeGreaterThanOrEqual(0.5); |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + it("should hard-clamp at maxScale even for trackpad pinch", () => { |
| 173 | + const { content, ref } = renderApp({ |
| 174 | + maxScale: 3, |
| 175 | + disablePadding: true, |
| 176 | + wheel: { step: 0.2 }, |
| 177 | + }); |
| 178 | + |
| 179 | + userEvent.hover(content); |
| 180 | + for (let i = 0; i < 30; i++) { |
| 181 | + fireTrackpadWheel(content, -50, 1); |
| 182 | + expect(ref.current!.instance.state.scale).toBeLessThanOrEqual(3); |
| 183 | + } |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe("zoomAnimation.disabled prevents elastic overshoot", () => { |
| 188 | + it("should hard-clamp when zoomAnimation is disabled", () => { |
| 189 | + const { content, ref } = renderApp({ |
| 190 | + minScale: 0.5, |
| 191 | + zoomAnimation: { disabled: true }, |
| 192 | + wheel: { step: 0.2 }, |
| 193 | + }); |
| 194 | + |
| 195 | + userEvent.hover(content); |
| 196 | + for (let i = 0; i < 30; i++) { |
| 197 | + fireTrackpadWheel(content, 50, 1); |
| 198 | + expect(ref.current!.instance.state.scale).toBeGreaterThanOrEqual(0.5); |
| 199 | + } |
| 200 | + }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments